Regards,
Please help
What I'm trying to do is this: (and I can't use reports since I must export to Excel)
I export some queries to different tabs in an excel workbook
I then loop through each tab and apply autowidth to columns and apply autofilter to the first row of every tab in the workbook. I've this down
However, I need to apply conditional formatting in the workbook, I'd like to do so by looping through each of the tabs, apply the conditional formatting to
the cell in second row for each column of each tab (since the first one has the headings from Access) and then copy it to the rest of the column of that tab, and then loop through each of the columns in that tab and apply the conditional formatting likewise (I've to do so since the columns contain different formats)
I've this so far:
This is a code placed on a onclick event of a button in a form, it runs after the file has been saved
The frustration works fine in Excel, any ideas? I'd appreciate any help
Best regards,
Afromanam
Please help
What I'm trying to do is this: (and I can't use reports since I must export to Excel)
I export some queries to different tabs in an excel workbook
I then loop through each tab and apply autowidth to columns and apply autofilter to the first row of every tab in the workbook. I've this down
However, I need to apply conditional formatting in the workbook, I'd like to do so by looping through each of the tabs, apply the conditional formatting to
the cell in second row for each column of each tab (since the first one has the headings from Access) and then copy it to the rest of the column of that tab, and then loop through each of the columns in that tab and apply the conditional formatting likewise (I've to do so since the columns contain different formats)
I've this so far:
This is a code placed on a onclick event of a button in a form, it runs after the file has been saved
Code:
Private Sub Command1_Click()
Dim WB As Object
Dim objexcelapp As Object
Dim SH As Object
Dim FILE As String
Dim lastrow, lastcol, col As Integer
FILEPATH = "C:\DOCUMENTS AND SETTINGS\user\"
FILE2 = CStr(Format(Date, "ddmmmyyyy"))
FILE = FILEPATH & " dummy " & FILE2 & ".XLS"
Set objexcelapp = CreateObject("Excel.Application")
objexcelapp.Visible = "False"
Set WB = objexcelapp.Workbooks.Open(FILE)
Set WS = WB.Worksheets(1) '<- i added this line when implementing the
'conditional formatting code
For Each SH In WB.Sheets
SH.Activate
SH.Columns("A:I").EntireColumn.AutoFit
SH.Rows("1:1").Select
objexcelapp.Selection.AutoFilter
'then the frustration begins
lastrow = SH.UsedRange.Rows.Count
lastcol = SH.UsedRange.Columns.Count
For col = 1 To lastcol
objexcelapp.Range("Cells(2, col)", "Cells(lastrow, col)").Select
objexcelapp.Cells(2, col).Select.FormatConditions.Add Type:=xlExpression, Formula1:="=$G2=""Not yet"""
Selection.FormatConditions(1).Interior.ColorIndex = 40
SH.Selection.Copy
SH.Range("Cells(2, col)", "Cells(lastrow, col)").Select
SH.Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next col
*end of frustration
Next
objexcelapp.ActiveWorkbook.Save
objexcelapp.Workbooks.Close
objexcelapp.Quit
Set objexcelapp = Nothing
End Sub
Best regards,
Afromanam
Comment