(First post, hope I'm doing this right) I use this code to convert a spreadsheet produced by another department into a more usable format and speed up a manual data entry process. It works perfectly except that unless I have saved first, any button in the sheet only runs the desired macro on row A. However, after saving it runs on the appropriate and adjacent row. Not sure what went wrong/can be done to fix.
Here is the code (it's a bit long).
Sub PayrollEntry()
Columns("F").Cut 'Moves "Total hours worked" column
Columns("E").Insert Shift:=xlToRight
'----------------------------------------------------------------------------------------
Columns("H").Cut 'Moves "Payroll Comments" column
Columns("F").Insert Shift:=xlToRight
'----------------------------------------------------------------------------------------
Columns("E:H").Select 'Deletes formatting and borders from columns E through G
With Selection.Interior
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
.Pattern = xlNone
End With
'----------------------------------------------------------------------------------------
Columns("G:G").Select 'Removes "$" from the pay amount
Selection.Style = "Comma"
'----------------------------------------------------------------------------------------
Columns("E:E").Select 'Converts Column E format to "General"
Selection.NumberFormat = "General"
'----------------------------------------------------------------------------------------
Columns("A:H").Select 'Converts font of work area to 10 point Arial to match budget formatting
With Selection.Font
.Name = "Arial"
.Size = 10
.Underline = xlUnderlineStyleNone
.ThemeFont = xlThemeFontNone
End With
'----------------------------------------------------------------------------------------
Columns("H").Select 'Applies conditional formatting (Red) to any cell in column H with value less than -0.05
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=-0.05"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorAccent2
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.599963377788629
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False
'----------------------------------------------------------------------------------------
Columns("H:H").Select 'Applies conditional formatting (Blue) to any cell in column H with value greater than 0.05
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual _
, Formula1:="=0.05"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorAccent1
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False
'----------------------------------------------------------------------------------------
Columns("I:I").ColumnWidth = 55.71 'Adjusts and applies borders to "Comments" Column
Columns("I:I").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Columns("A:A").EntireColumn.Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'----------------------------------------------------------------------------------------
Columns("A:H").EntireColumn.AutoFit 'Autofits entire work area
'----------------------------------------------------------------------------------------
Dim xbutton As Range, clm As Range 'Applies buttons to cell "A" of all rows where cell "B" contains a value
Set clm = Range("B:B")
For Each xbutton In clm
If xbutton > 0 Then
Dim t As Range
Dim btn As Button
Set t = xbutton.Offset(0, -1)
Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
.OnAction = "EnterPayroll"
.Caption = "Entered"
.Name = "Entered"
End With
End If
Next
End Sub
Bookmarks