Hello all,
I have a giant procedure that exports a set of queries from access to excel and formats the excel sheet. A lot of code parts are repeated several times throughout the code and I would like to know if someone could explain how to take these bits out and call them somehow like if they were a function.
My code looks more or less like this:
I would like to have the loops somewhere else to make my code lighter, how can I do it?
Would it be enough with creating a module for each one in the database and call it from the code? How can I do this?
Thanks
I have a giant procedure that exports a set of queries from access to excel and formats the excel sheet. A lot of code parts are repeated several times throughout the code and I would like to know if someone could explain how to take these bits out and call them somehow like if they were a function.
My code looks more or less like this:
Code:
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim db As DAO.Database
Dim qdf1, qdf2, qdf3, qdf4, qdf5 As DAO.QueryDef
Dim rs1, rs2, rs3, rs4, rs5 As DAO.Recordset
Dim nrow, rowcount, lastrow As Long 'And other strings like x,y,adr
On Error GoTo SubError
DoCmd.Hourglass True
Set db = CurrentDb()
'Event name:
Set qdf1 = db.QueryDefs("QueryEvent")
qdf1!ParEvent = [Forms]![EventForm]![Event]
Set rs1 = qdf1.OpenRecordset
If rs1.RecordCount = 0 Then
MsgBox "No data available for export", vbInformation + vbOKOnly, "Excel not launched"
GoTo SubExit
End If
Set xlApp = Excel.Application
xlApp.Visible = False
xlApp.DisplayAlerts = False
Set xlWorkbook = xlApp.Workbooks.Add
Set xlSheet = xlWorkbook.Worksheets(1)
With xlSheet
'GENERAL FORMATTING
.Name = "Event Summary"
.Cells.Font.Name = "Calibri"
.Cells.Font.Size = 10
.Cells.VerticalAlignment = xlCenter
.Columns.WrapText = False
.Columns.ColumnWidth = 10
.Cells.Interior.Color = RGB(255, 255, 255)
'Print setup
.PageSetup.Orientation = xlLandscape
.PageSetup.FitToPagesWide = 1
.PageSetup.PaperSize = xlPaperA4
.PageSetup.Zoom = 40
'With loop that gets repeated
With .Columns("K")
.ColumnWidth = 17
.HorizontalAlignment = xlCenter
.FormatConditions.Add(xlCellValue, xlEqual, "O").Interior.Color = RGB(38, 250, 58)
.FormatConditions.Add(xlCellValue, xlEqual, "OG").Interior.Color = RGB(0, 176, 80)
.FormatConditions.Add(xlCellValue, xlEqual, "F").Interior.Color = RGB(192, 0, 0)
.FormatConditions.Add(xlCellValue, xlEqual, "T").Interior.Color = RGB(246, 134, 206)
.FormatConditions.Add(xlCellValue, xlEqual, "N").Interior.Color = RGB(191, 191, 191)
.FormatConditions.Add(xlCellValue, xlEqual, "T").Interior.Color = RGB(255, 255, 0)
.FormatConditions.Add(xlCellValue, xlEqual, "NT").Interior.Color = RGB(255, 192, 0)
.FormatConditions.Add(xlCellValue, xlEqual, "NO").Interior.Color = RGB(255, 0, 0)
.FormatConditions.Add(xlCellValue, xlEqual, "W").Interior.Color = RGB(0, 176, 240)
.FormatConditions.Add(xlCellValue, xlEqual, "Ng").Interior.Color = RGB(0, 0, 0)
.FormatConditions.Add(xlCellValue, xlEqual, "Ng").Font.Bold = True
.FormatConditions.Add(xlCellValue, xlEqual, "Ng").Font.Color = RGB(255, 0, 0)
End With
'For loop that gets repeated
If .Range("I" & rowcount).Value = "" Then
.Range("C" & rowcount & ":O" & rowcount).Value = "No items"
.Range("C" & rowcount & ":O" & rowcount).Merge
.Range("C" & rowcount & ":O" & rowcount).HorizontalAlignment = xlLeft
Else
For nrow = rowcount To lastrow
ye = .Cells(nrow, 2) & ""
ype = .Cells(nrow, 3) & ""
ypu= .Cells(nrow, 4) & ""
ys = .Cells(nrow, 5) & ""
yi = .Cells(nrow, 6) & ""
yt = .Cells(nrow, 13) & ""
If Len(ye) Then
If ye = xe Then
adre = .Cells(nrow - 1, 2).Address & ":" & .Cells(nrow, 2).Address
.Range(adre).Merge
End If
End If
If ype = xpe And ye = xe Then
adrpe = .Cells(nrow - 1, 3).Address & ":" & .Cells(nrow, 3).Address
.Range(adrpe).Merge
End If
If ype = xpe And ye = xe And ypu = xpu Then
adrpu = .Cells(nrow - 1, 4).Address & ":" & .Cells(nrow, 4).Address
.Range(adrpu).Merge
End If
If Len(ys) Then
If ys = xs And ype = xpe And ye = xe And ypu = xpu Then
adrs = .Cells(nrow - 1, 5).Address & ":" & .Cells(nrow, 5).Address
.Range(adrs).Merge
End If
End If
If Len(yi) And ype = xpe And ye = xe And ypu = xpu And ys = xs Then
If yi = xi Then
'Merge index
adri = .Cells(nrow - 1, 6).Address & ":" & .Cells(nrow, 6).Address
.Range(adri).Merge
'Merge ti
adrti = .Cells(nrow - 1, 7).Address & ":" & .Cells(nrow, 7).Address
.Range(adrti).Merge
'Merge tr
adrtr = .Cells(nrow - 1, 8).Address & ":" & .Cells(nrow, 8).Address
.Range(adrtr).Merge
'Merge fi
adrfi = .Cells(nrow - 1, 9).Address & ":" & .Cells(nrow, 9).Address
.Range(adrfi).Merge
'Merge av
adrav = .Cells(nrow - 1, 10).Address & ":" & .Cells(nrow, 10).Address
.Range(adrav).Merge
'Merge vs
adrvs = .Cells(nrow - 1, 11).Address & ":" & .Cells(nrow, 11).Address
.Range(adrvs).Merge
'Merge n
adrn = .Cells(nrow - 1, 12).Address & ":" & .Cells(nrow, 12).Address
.Range(adrn).Merge
End If
End If
If Len(yt) And ype = xpe And ye = xe And ypu = xpu And ys = xs And yi = xi Then
If yt = xt Then
'Merge t
adrt = .Cells(nrow - 1, 13).Address & ":" & .Cells(nrow, 13).Address
.Range(adrt).Merge
'Merge e
adre = .Cells(nrow - 1, 14).Address & ":" & .Cells(nrow, 14).Address
.Range(adre).Merge
'Merge t
adrt = .Cells(nrow - 1, 15).Address & ":" & .Cells(nrow, 15).Address
.Range(adrt).Merge
End If
End If
xt = yt
xi = yi
xs = ys
xpu = ypu
xpe = ype
xe = ye
Next nrow
End If
'Other code'
.Range("B2").Select
End With
xlApp.ActiveWindow.Zoom = 75
xlApp.DisplayAlerts = True
xlApp.Visible = True
MsgBox "File exported successfully", vbInformation + vbOKOnly, "Export success"
SubExit:
DoCmd.Hourglass False
On Error Resume Next
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing
Set db = Nothing
Set rs1 = Nothing
Set rs2 = Nothing
Set rs3 = Nothing
Set rs4 = Nothing
Set rs5 = Nothing
Set qdf1 = Nothing
Set qdf2 = Nothing
Set qdf3 = Nothing
Set qdf4 = Nothing
Set qdf5 = Nothing
rs1.Close
rs2.Close
rs3.Close
rs4.Close
rs5.Close
qdf1.Close
qdf2.Close
qdf3.Close
qdf4.Close
qdf5.Close
Exit Sub
SubError:
MsgBox "Error number: " & Err.Number & "*" & Err.Description, vbCritical + vbOKOnly, _
"An error occurred"
Err.Clear
Resume SubExit
End Sub
I would like to have the loops somewhere else to make my code lighter, how can I do it?
Would it be enough with creating a module for each one in the database and call it from the code? How can I do this?
Thanks