I’m pretty new to VBA and created a fairly basic macro that ran earlier. It was tied to a button that was on the wrong sheet, so I copied the macro text into Notepad, deleted the button, and have created a new button on the correct tab. I pasted the previously-working macro text into the new button’s macro, but nothing happens when I hit the button or run the macro through the macros menu. Could someone please explain what looks wrong with the code? Thank you for your time!
Sub TestMacro()
Application.ScreenUpdating = False
Dim wsPopulation As Worksheet
Set wsPopulation = Worksheets("Population")
Dim n As Integer
n = wsPopulation.Application.WorksheetFunction.Count(Range("A:A"))
Dim i As Integer
For i = 2 To n + 1
'Define inputs and calculations worksheet.
Dim wsCalculations As Worksheet
Set wsCalculations = Worksheets("Calculations")
Dim EID As Range
Set EID = wsPopulation.Cells(i, 4)
Dim InputEID As Range
Set InputEID = wsCalculations.Cells(2, 2)
EID.Copy
InputEID.PasteSpecial xlPasteValues
'Define output fields we'll want to pull.
Dim EmpName As Range
Set EmpName = wsCalculations.Cells(3, 2)
Dim FICA_Due As Range
Set FICA_Due = wsCalculations.Cells(13, 2)
'Define output space.
Dim OutputEID As Range
Set OutputEID = wsCalculations.Cells(i + 4, 6)
Dim OutputName As Range
Set OutputName = wsCalculations.Cells(i + 4, 7)
Dim OutputFICA_Due As Range
Set OutputFICA_Due = wsCalculations.Cells(i + 4, 8)
'Copy/paste key output values.
InputEID.Copy
OutputEID.PasteSpecial xlPasteValues
EmpName.Copy
OutputName.PasteSpecial xlPasteValues
FICA_Due.Copy
OutputFICA_Due.PasteSpecial xlPasteValues
Next i
Application.ScreenUpdating = True
End Sub
With no errors, it’s tough to debug. I consulted a coworker and he didn’t see anything wrong jump out at him. I’ve been reading on StackExchange, but I can’t tell what could’ve happened when the macro had worked before. I have confirmed that macros are still enabled in this workbook in the settings. I’ve reinitiated Excel multiple times.
4
Are you sure the code is executed when you push the button, did you also try it in the VB editor using F5?
I tried the code using guessed worksheets and things do happen, could you add the workbook (or if it is confidential just the 2 sheets with fake data).
tnx