I have a 2010 Access vba procedure that creates a workbook and adds sheets. I then create a sheet (sheet1) that is a "menu" for the user the use for hyperlinking to the appropriate sheet. The code looks like this:
with ws
iLeft = 350
iTop = 140
iWidth = 160
iHeight = 30
.Shapes.AddShape(msoShapeRoundedRectangle, iLeft, iTop, iWidth, iHeight).Select
i = wks.Shapes.Count
.Shapes.Range(i).ShapeStyle = msoShapeStylePreset33
.Shapes.Range(i).TextFrame2.VerticalAnchor = msoAnchorMiddle
.Shapes.Range(i).TextFrame2.TextRange.Characters.Text = "All Data"
.Shapes.Range(i).TextFrame2.TextRange.Characters.Font.Bold = msoTrue
.Shapes.Range(i).TextFrame2.TextRange.Characters(1, 8). _
ParagraphFormat.Alignment = msoAlignCenter
With .Shapes.Range(i).TextFrame2.TextRange.Characters(1, 8).Font
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(255, 255, 255)
.Fill.ForeColor.TintAndShade = 0
.Fill.ForeColor.Brightness = 0
.Fill.Transparency = 0
.Fill.Solid
.Size = 11
.Name = "+mn-lt"
End With
Set HyperLinkShape = wks.Shapes(i)
HyperSubAddress = "'All Data'!A1"
.Hyperlinks.Add Anchor:=HyperLinkShape, Address:="", SubAddress:=HyperSubAddress
end with
There are hundreds of other lines of code, but I have narrowed down that the
.Hyperlinks.Add...
statement is causing a second occurrence of Excel to start. Normally, that happens because I should be referencing the worksheet somewhere in the statement. I tried:
.Hyperlinks.Add Anchor:=ws.HyperLinkShape, Address:="", SubAddress:=HyperSubAddress
and
.Hyperlinks.Add Anchor:=HyperLinkShape, Address:="", SubAddress:=ws.HyperSubAddress
but both of these gave me a compiler error.
Any suggestions on how I can fix this issue?
I cannot reproduce this.
The following standalone procedure runs in Access 2010. Both hyperlinks work.
After disposing of all objects (and oExcel.Quit), there is no more Excel process.
Note: you can do without all the Set obj = Nothing, then the Excel process closes after the procedure is finished, when all local variables go out of scope.
Public Sub TestExcelShapeHyperlink()
Dim oExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim shp As Excel.Shape
Set oExcel = CreateObject("Excel.Application")
With oExcel
.UserControl = True
.Visible = True
End With
Set wkb = oExcel.Workbooks.Add
Set wks = wkb.ActiveSheet
' Note: there is no reason to select the shape
Set shp = wks.Shapes.AddShape(msoShapeRoundedRectangle, 10, 10, 200, 100)
' Use the created object to set its properties
With shp
.ShapeStyle = msoShapeStylePreset10
.TextFrame2.TextRange.Font.Bold = msoTrue
.TextFrame2.VerticalAnchor = msoAnchorMiddle
.TextFrame2.TextRange.Characters.Text = "TEST TEST TEST"
.TextFrame2.TextRange.Characters.ParagraphFormat.Alignment = msoAlignCenter
.TextFrame2.TextRange.Characters.Font.Bold = msoTrue
End With
' Name 2nd sheet for hyperlink target
wkb.Worksheets(2).Name = "All Data"
' Hyperlink 1 with anchor = shape
wks.Hyperlinks.Add Anchor:=shp, Address:="", SubAddress:="'All Data'!A1"
' Hyperlink 2 with anchor = a random cell
wks.Hyperlinks.Add Anchor:=wks.Range("B11"), Address:="", SubAddress:="'All Data'!D5", TextToDisplay:="Click me, I'm a link!"
Stop ' 1 Excel process
Set shp = Nothing
Set wks = Nothing
wkb.SaveAs "D:\foo.xlsx"
wkb.Close
Set wkb = Nothing
oExcel.Quit
Set oExcel = Nothing
Stop ' 0 Excel processes
End Sub
Related
I have made 2 functions "WrongEntries" & "Duplicates". When I run it individually it runs perfectly. But when I run it one after the other it reflects Method 'range' of object '_global' failed.
I am not sure is it because of the objects still in the memory of the previous run function but i did use different objects & variable in both functions still same error was there. Also tried putting objects as nothing.
Stange part is if I shuffle their run-first one always runs perfectly doesn't matter which one it is.
Function WrongEntries()
Dim dbs As Database Dim lRow As Long Dim lCol As Long Dim rg As Range
Set dbs = CurrentDb
Set rgQuery = dbs.OpenRecordset("5G High Cycle Times")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.Workbooks.Open("\5G Wrong
Entries_Blank.xlsx")
targetWorkbook.Worksheets("Cycle Times >5
weeks").Range("A2").CopyFromRecordset rgQuery
targetWorkbook.Worksheets("Cycle Times >5 weeks").Activate
lRow = Range("b" & Rows.Count).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
excelApp.Quit
Set excelApp = Nothing
End Function
''' Problems is when I run second one together with first'''
Function Duplicates()
Dim dbs As Database Dim lastRw As Long Dim lastCl As Long Dim rnge As
Range Dim wks As Worksheet
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set dbs = CurrentDb
Set rdQuery = dbs.OpenRecordset("5G Duplicates Check")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.Workbooks.Open("\5G Duplicates_Blank.xlsx")
Set wks = targetWorkbook.Worksheets("Duplicates")
targetWorkbook.Worksheets("Duplicates").Range("A2").CopyFromRecordset
rdQuery
Here the error is reflecting while calculating last row count
lastRw = wks.Range("a" & Rows.Count).End(xlUp).Row
lastCl = wks.Cells(1, Columns.Count).End(xlToLeft).Column
End Function
You close the application:
excelApp.Quit
Set excelApp = Nothing
leaving the workbook and all objects orphaned. So:
targetWorkbook.Close
Set targetWorkbook = Nothing
excelApp.Quit
Set excelApp = Nothing
Further: Always use specific objects. Not
Range("b" & Rows.Count).End(xlUp).Row
Cells(1, Columns.Count).End(xlToLeft).Column
but:
Set range = SomeWorksheet.Range ...
set cell = SomeWorksheet.Cells ...
and terminate these as the first:
Set cell = Nothing
Set range = Nothing
Set wks = nothing
targetWorkbook.Close
Set targetWorkbook = Nothing
excelApp.Quit
Set excelApp = Nothing
I have an Access DB to open, edit and save an Excel doc that works fine the first time run but if I try to alter more than one file (or the same file twice) it fails with "Run-time error '1004': Method 'Cells' of object '_Global' failed"
If I close the DB then re-open it, it again works fine for the first file altered.
Although I am not new to VBA I would say that I am a novice. Here is a snippit of the code I am using:
Code:
'Open spreadsheet and make it visible
Set xl = CreateObject("Excel.Application")
strInputFile = varItem
xl.Workbooks.Open strInputFile
xl.Visible = True
'Trying to get row count here but not working yet
'Set myRange = xl.Sheets("Sheet1").Range("C:C")
'lRowCount = Excel.Application.WorksheetFunction.CountA("Sheet1").Range("C:C")
'lRowCount = xl.WorksheetFunction.CountA(Worksheets("Sheet1").Cells(C, C))
'Debug.Print lRowCount
'strMyRange = "C:C"
'lRowCount = xl.WorksheetFunction.CountA(strMyRange)
'Debug.Print lRowCount
'lRowCount = Excel.Application.WorksheetFunction.CountA(Workbooks(strInputFile).Sheets("Sheet1").Range("C:C"))
'Debug.Print lRowCount
'Make the changes
j = 0
If Left(strFile, 4) = "xxxx" Then
myPath = "\\a\path\for\xxxx"
If InStr(1, strFile, "IQ") Then
For i = 1 To 500 'Row count not working yet
If InStr(1, Cells(i, "C").Value, myVariable) > 0 Then
Cells(i, "B") = "New Value"
j = j + 1
End If
Next
End If
End If
'Clean up
xl.Quit
Set xl = Nothing
Set objInputFile = Nothing
The Excel VBA code for using in Access should be modified. You cannot use direct calls of Excel library methods like Cell. Declare variables for Excel.Application, Workbook and Worksheet and use them for referencing worksheet cells. Avoid using Activate methods. So, in your case the code will be like this:
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim lRowCount As Long
Dim myRange As Excel.Range
Set xl = CreateObject("Excel.Application")
strInputFile = varItem
Set wb = xl.Workbooks.Open(strInputFile)
Set ws = wb.Sheets("Sheet1")
lRowCount = ws.UsedRange.Rows.Count
'Make the changes
j = 0
If Left(strFile, 4) = "xxxx" Then
myPath = "\\a\path\for\xxxx"
If InStr(1, strFile, "IQ") Then
For i = 1 To lRowCount
If InStr(1, ws.Cells(i, "C").Value, myVariable) > 0 Then
ws.Cells(i, "B") = "New Value"
j = j + 1
End If
Next
End If
End If
wb.Save
'Clean up
xl.Quit
Set xl = Nothing
Don't forget to add a reference to Microsoft Excel library
So I have a query in access that I want to send back to excel. While using the export wizard is fine and dandy I want to add more automation to the exporting process. So far I am working on code so during the export the final excel sheet will have some formatting. As far as basic formatting I am fine, I found many resources to help me with this.
My problem is that I want to set up conditional formatting so that if a specific column(G) has a value, then the whole row is highlighted. I am a bit lost on how to set up conditional formatting for Excel through vba code in Access
Here is what I have
Dim appExcel As Variant
Dim MyStr As String
Dim rng As Excel.Range
' Creates Excel object and Adds a Workbook to it
Set appExcel = CreateObject("Excel.application")
appExcel.Visible = False
appExcel.Workbooks.Add
Set wksNew = appExcel.Worksheets("Sheet1")
appExcel.Visible = True
' The first thing I do to the worksheet is to set the font.
' Not all are required, but I included them as examples.
With appExcel
.Cells.Font.Name = "Calbri"
.Cells.Font.Size = 11
.Cells.NumberFormat = "#" 'all set to Text Fields
' My first row will contain column names, so I want to freeze it
.Rows("2:2").Select
.ActiveWindow.FreezePanes = True
' ... and I want the header row to be bold
.Rows("1:1").Font.Bold = True
.Rows("1:1").Font.ColorIndex = 1
.Rows("1:1").Interior.ColorIndex = 15
' Adds conditional formatting based on Values in the G column
rng = .Range("A2:J20").Select
rng.FormatConditions.Add Type:=xlExpression, Formula1:="=NOT($G2 = 0)"
rng.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With appExcel.Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
End With
Currently the code executes until my conditional formatting block and then it tells me that the Object Variable or With block is not set.
I checked that the following code runs until the end:
Dim appExcel As Variant
Dim MyStr As String
Dim rng As Excel.Range
Dim wksNew
' Creates Excel object and Adds a Workbook to it
Set appExcel = CreateObject("Excel.application")
appExcel.Visible = False
appExcel.Workbooks.Add
' Set wksNew = appExcel.Worksheets("Sheet1")
Set wksNew = appExcel.Worksheets(1)
appExcel.Visible = True
' The first thing I do to the worksheet is to set the font.
' Not all are required, but I included them as examples.
With appExcel
.Cells.Font.Name = "Calbri"
.Cells.Font.Size = 11
.Cells.NumberFormat = "#" 'all set to Text Fields
' My first row will contain column names, so I want to freeze it
.Rows("2:2").Select
.ActiveWindow.FreezePanes = True
' ... and I want the header row to be bold
.Rows("1:1").Font.Bold = True
.Rows("1:1").Font.ColorIndex = 1
.Rows("1:1").Interior.ColorIndex = 15
' Adds conditional formatting based on Values in the G column
Set rng = .Range("A2:J20")
rng.Select
rng.FormatConditions.Add Type:=xlExpression, Formula1:="=NOT($G2 = 0)"
rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
End With
Good luck.
In Access, I have a table which contain a field like this:
Part Number
A/B/C
A/B/R
T/Y/V
D/A/I
I want to change the color of the all the third part to red. So in this case C,R,V,I will be colored red. But I can't do change the color of part of the text field in Access 2007. If I use Font Change under Home tab it change the Font of entire Table. I'm so disappointed about Microsoft. Is there any way to change the color would be great :D You can recommend VBA , Macro in Form, Query, Report ...
P/S: I use Access 2007
if you can use an Access report, you can add a TextBox to the report. In the textbox, you can have a formula like this:
="<font color=""blue"">" & [ColumnA] & "</font> <u>" & [ColumnB] & "</u>"
See Access Rich-Text: Which subset of HTML is supported? for more details.
ok I think the only way is to export automatically to Excel. Finally I can do this
Private Sub CommandExport_Click()
Dim db As Database
Dim rec1 As Recordset
Dim xlFile As Object
Dim xlWorkBook As Object
Dim xlActiveWkb As Object
Dim xlActiveSheet As Object
Dim iCols, iRows, flag As Integer
Set db = CurrentDb
Set xlFile = CreateObject("Excel.Application")
Set xlWorkBook = xlFile.Workbooks.Add
Set xlActiveWkb = xlFile.Application.ActiveWorkBook
xlFile.Visible = True
xlActiveWkb.Sheets.Add
xlActiveWkb.Worksheets(1).Name = "My_Report"
Set xlActiveSheet = xlActiveWkb.Worksheets("My_Report")
Set rec1 = db.OpenRecordset("Report")
For iCols = 0 To rec1.Fields.Count - 1
xlActiveSheet.Cells(1, iCols + 1).Value = rec1.Fields(iCols).Name
If rec1.Fields(iCols).Name = "FS Number" Then
flag = iCols
End If
Next
xlActiveSheet.Range(xlActiveSheet.Cells(1, 1), xlActiveSheet.Cells(1, rec1.Fields.Count)).Font.Bold = True
xlActiveSheet.Range(xlActiveSheet.Cells(1, 1), xlActiveSheet.Cells(1, rec1.Fields.Count)).Interior.ColorIndex = 15
xlActiveSheet.Cells(2, 1).CopyFromRecordset rec1
xlActiveSheet.Columns("A:AD").EntireColumn.AutoFit
iRows = 1
rec1.MoveFirst
While Not rec1.EOF
xlActiveSheet.Cells(iRows + 1, flag + 1).Characters(InStr(rec1![FS Number], "*")).Font.ColorIndex = 3
iRows = iRows + 1
rec1.MoveNext
Wend
Set xlSheet = Nothing
Set xlWorkBook = Nothing
Set xlActiveWkb = Nothing
rec1.Close
db.Close
Set rec1 = Nothing
Set db = Nothing
End Sub
The magic is here
xlActiveSheet.Cells(iRows + 1, flag + 1).Characters(InStr(rec1![FS Number], "*")).Font.ColorIndex = 3
I have created a data base that comes in an installer that runs as an epos system.
On installing it on other computers, I get a large number of errors all saying that something is missing. the file runs perfectly on my computer, but the errors stop anything from working on other computers....
the errors are as follows. each has its own popup box.
broken reference to excel.exe version 1.7 or missing.
acwztool.accde missing
npctrl.dll v4.1 missing
contactpicker.dll v1.0 missing
cddbcontolwinamp.dll v1.0 missing
cddbmusicidwinamp.dll v1.0 missing
colleagueimport.dll v1.0 missing
srstsh64.dll missing
I feel like this may because I altered the module vba library referencing so that I could run a vba code that uses excel, unfortunatly i have forgotten which librarys i have added
if it helps, the code that I added which required new references is below
Public Sub SalesImage_Click()
Dim rst As ADODB.Recordset
' Excel object variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlChart As Excel.Chart
Dim i As Integer
On Error GoTo HandleErr
' excel aplication created
Set xlApp = New Excel.Application
' workbook created
Set xlBook = xlApp.Workbooks.Add
' set so only one worksheet exists
xlApp.DisplayAlerts = False
For i = xlBook.Worksheets.Count To 2 Step -1
xlBook.Worksheets(i).Delete
Next i
xlApp.DisplayAlerts = True
' reference the first worksheet
Set xlSheet = xlBook.ActiveSheet
' naming the worksheet
xlSheet.name = conSheetName
' recordset creation
Set rst = New ADODB.Recordset
rst.Open _
Source:=conQuery, _
ActiveConnection:=CurrentProject.Connection
With xlSheet
' the field names are imported into excel and bolded
With .Cells(1, 1)
.Value = rst.Fields(0).name
.Font.Bold = True
End With
With .Cells(1, 2)
.Value = rst.Fields(1).name
.Font.Bold = True
End With
' Copy all the data from the recordset into the spreadsheet.
.Range("A2").CopyFromRecordset rst
' Format the data the numbering system has been extended to encompas up to 9,999,999 sales to cover all posibilities of sales since the last stock take
.Columns(1).AutoFit
With .Columns(2)
.NumberFormat = "#,###,###"
.AutoFit
End With
End With
' Create the chart.
Set xlChart = xlApp.Charts.Add
With xlChart
.ChartType = xl3DBarClustered
.SetSourceData xlSheet.Cells(1, 1).CurrentRegion
.PlotBy = xlColumns
.Location _
Where:=xlLocationAsObject, _
name:=conSheetName
End With
'the reference must be regotten as it is lost
With xlBook.ActiveChart
.HasTitle = True
.HasLegend = False
With .ChartTitle
.Characters.Text = conSheetName & " Chart"
.Font.Size = 16
.Shadow = True
.Border.LineStyle = xlSolid
End With
With .ChartGroups(1)
.GapWidth = 20
.VaryByCategories = True
End With
.Axes(xlCategory).TickLabels.Font.Size = 8
.Axes(xlCategoryScale).TickLabels.Font.Size = 8
End With
With xlBook.ActiveChart
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Product"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Sales"
End With
'format the size and possition of the chart
With xlBook.ActiveChart
.Parent.Width = 800
.Parent.Height = 550
.Parent.Left = 0
.Parent.Top = 0
End With
'this displays the chart in excel. excel must be closed by the user to return to the till system
xlApp.Visible = True
ExitHere:
On Error Resume Next
'this cleans the excel file
rst.Close
Set rst = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Exit Sub
HandleErr:
MsgBox Err & ": " & Err.Description, , "There has been an error!"
Resume ExitHere
End Sub
Deployment should be less troublesome if you remove your project's Excel reference and use late binding for Excel objects.
A downside is you lose the benefit of Intellisense during development with late binding. However it's very easy to switch between early binding during development and late binding for production. Simply change the value of a compiler constant.
In the module's Declarations section ...
#Const DevStatus = "PROD" 'PROD or DEV
Then within the body of a procedure ...
#If DevStatus = "DEV" Then
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
#Else ' assume PROD (actually anything other than DEV)
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
#End If
With late binding your code will need to use the values of Excel constants rather than the constants names. Or you can define the named constants in the #Else block for production use then continue to use them by name in your code.
I don't know what all those other reference are. Suggest you take a copy of your project, remove all those references and see what happens when you run Debug->Compile from the VB Editor's main menu. Leave any unneeded references unchecked. And try late binding for the rest. I use only 3 references in production versions of Access applications: VBA; Access; and DAO.