Leo_Polla_Psemata
Registered User.
- Local time
- Yesterday, 18:26
- Joined
- Mar 24, 2014
- Messages
- 364
This is a VBA script from which i export data from access database to an excel.
The excel looks like this and may contain 2000 or more lines.
I have removed several lines from the code, those lines that format and calculate, just to make it shorter.
My "goal" now is to apply an alternative background color every time data in column "A" change.
If you could help me by adding extra vba code pieces, it would be just great.
The excel looks like this and may contain 2000 or more lines.
I have removed several lines from the code, those lines that format and calculate, just to make it shorter.
My "goal" now is to apply an alternative background color every time data in column "A" change.
Code:
Private Sub Fr_Click()
On Error GoTo SubError
DoCmd.SetWarnings False
DoCmd.RunSQL strUP1
DoCmd.RunSQL strUP2
DoCmd.SetWarnings True
Me.Refresh
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim SQL As String
Dim rsFR As DAO.Recordset
Dim i As Integer
'Show user work is being performed
DoCmd.Hourglass (True)
'*********************************************
' RETRIEVE DATA
'*********************************************
'SQL statement to retrieve data from database
SQL = " SELECT AAV.Acti, FREIGHT.BL, .... " ' SQL statement
'Execute query and populate recordset
Set rsFR = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)
'If no data, don't bother opening Excel, just quit
If rsFR.RecordCount = 0 Then
MsgBox "No data selected for export", vbInformation + vbOKOnly, "No data exported"
GoTo SubExit
End If
'*********************************************
' BUILD SPREADSHEET
'*********************************************
'Create an instance of Excel and start building a spreadsheet
'Early Binding
Set xlApp = Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
Range("A10").Select
ActiveWindow.FreezePanes = True
xlSheet.Activate
ActiveWindow.DisplayGridlines = False
With xlSheet
.Name = "Freight List " & IRISvoy
.Cells.Font.Name = "Calibri"
.Cells.Font.Size = 10
'Set column widths
.Columns("A").ColumnWidth = 14
.Columns("B").ColumnWidth = 14
.Rows("9:9").RowHeight = 50
.Range("A9:Q9").Interior.Color = RGB(207, 207, 207)
.Range("D9", "I9").Orientation = 90
.Range("K10:K2500").NumberFormat = "@"
' Extended lines cuted
.Range("C8").Formula = "=SUM(I10:I" & i - 1 & ")"
' format the table , lines cuted
.Range("D2:D6").Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("C2:C6").Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous
.Range("C2", "D2").Merge
'Format columns , lines cuted
.Range("A2").Value = "VESSEL "
.Range("B2").Value = vesselName
.Range("B4").NumberFormat = "[$-en-US]d-mmm-yyyy;@"
.Range("E2", "L2").NumberFormat = "$#,##0.00"
.Range("E3", "L3").NumberFormat = "[$€‚¬-x-euro2] #,##0.00"
.Range("E4", "L4").NumberFormat = "[$£-en-GB]#,##0.00"
.Range("E5", "L5").NumberFormat = "[$¥-zh-CN]#,##0.00"
'provide initial value to row counter
i = 10
' 'Loop through recordset and copy data from recordset to sheet
Do While Not rsFR.EOF
.Range("A" & i).Value = Nz(rsFR!bl, "")
.Range("B" & i).Value = Nz(rsFR!bk, "")
.Range("C" & i).Value = Nz(rsFR!BLline, "")
' extended lines removed
i = i + 1
rsFR.MoveNext
'
Loop
.Range("B5").Formula = "=SUMPRODUCT(1/COUNTIF(A10" & ":A" & i - 1 & " ,A10" & ":A" & i - 1 & " ))"
.Range("B6").Formula = "=SUMPRODUCT(1/COUNTIF(C10" & ":C" & i - 1 & " ,C10" & ":C" & i - 1 & " ))"
.Range("L10" & ":O" & i).Cells.Font.Size = 7
.Range("E2").Formula = "=SUMIFS(I10" & ":I" & i - 1 & " ,D10" & ":D" & i - 1 & " ," & """OFT""" & ",F10" & ":F" & i - 1 & " ," & """USD""" & ", J10" & ":J" & i - 1 & " ," & """P"")"
.Range("E3").Formula = "=SUMIFS(I10" & ":I" & i - 1 & " ,D10" & ":D" & i - 1 & " ," & """OFT""" & ",F10" & ":F" & i - 1 & " ," & """EUR""" & ", J10" & ":J" & i - 1 & " ," & """P"")"
.Range("E4").Formula = "=SUMIFS(I10" & ":I" & i - 1 & " ,D10" & ":D" & i - 1 & " ," & """OFT""" & ",F10" & ":F" & i - 1 & " ," & """GBP""" & ", J10" & ":J" & i - 1 & " ," & """P"")"
.Range("A" & i - 1 & ":O" & i - 1).Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlDouble
.Range("A10" & ":O" & i - 1).Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Range("A10" & ":O" & i - 1).Borders(xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
.Range("A10" & ":O" & i - 1).Borders(xlEdgeRight).LineStyle = XlLineStyle.xlContinuous
.Range("B9" & ":B" & i - 1).HorizontalAlignment = xlCenter
With .Range("J10:J" & i).FormatConditions.Add(xlCellValue, xlEqual, "<>""F""")
.Interior.Color = RGB(150, 150, 50)
End With
End With
SubExit:
On Error Resume Next
DoCmd.Hourglass False
xlApp.Visible = True
rsFR.Close
Set rsFR = Nothing
Exit Sub
SubError:
MsgBox "Error Number: " & Err.Number & "= " & Err.Description, vbCritical + vbOKOnly, _
"An error occurred"
GoTo SubExit
End Sub
If you could help me by adding extra vba code pieces, it would be just great.