mikenelena
Board Regular
- Joined
- Mar 5, 2018
- Messages
- 139
- Office Version
- 365
- Platform
- Windows
We use alternating background fill color in columns A-G as a visual reference to distinguish between rows with certain payroll date values stored in G. Currently, we have 2 macro buttons that each apply 1 of the colors. I'd like to incorporate this functionality into the vba language, so we can eliminate another manual task, and clean up the appearance by removing 2 buttons.
I struggled for a while, but finally decided that the solution might lie in the unique numeric codes assigned to dates by Excel. If we subtract the current payroll date from a known, and constant first payroll date, then divide by 2, we will get alternating odd and even numbers, which I figured could serve as the logic for determining which color to apply.
But how to weave that logic into the code?? It seems everything I read wants to push us towards conditional formatting. For a lot of reasons, I don't want to get involved with conditional formatting. I am looking for an automated VBA solution to this.
Many thanks to anyone who can lend a hand!
If it helps, the two colors are:
.ThemeColor = xlThemeColorAccent1.TintAndShade = 0.799981688894314
and
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.799981688894314
I struggled for a while, but finally decided that the solution might lie in the unique numeric codes assigned to dates by Excel. If we subtract the current payroll date from a known, and constant first payroll date, then divide by 2, we will get alternating odd and even numbers, which I figured could serve as the logic for determining which color to apply.
But how to weave that logic into the code?? It seems everything I read wants to push us towards conditional formatting. For a lot of reasons, I don't want to get involved with conditional formatting. I am looking for an automated VBA solution to this.
Many thanks to anyone who can lend a hand!
If it helps, the two colors are:
.ThemeColor = xlThemeColorAccent1.TintAndShade = 0.799981688894314
and
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.799981688894314
Code:
Sub Copy_PasteDataToMainTab()
Dim LR As LongDim ws As Worksheet, ws1 As Worksheet
Set ws = Worksheets("Query2")Set ws1 = Worksheets("Main")
ws1.Activate
LR = ws.Cells(Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
ws.Activate
Range("A2:G2" & LR).Copy ws1.Range("A" & Rows.Count).End(3)(2).PasteSpecial
Paste:=xlPasteValuesAndNumberFormats '----> Copies values and number formats only to Main starting in Column A
ws1.Activate
Application.ScreenUpdating = TrueApplication.CutCopyMode = FalseRange("D1").Select
End Sub
Last edited: