(VBA) Having trouble assigning relative cells (from an InputBox) to variables in order to subtract them.

exitSandMan

New Member
Joined
Sep 14, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello all! I'm running into an error at the 'Set aa' line: "Run-time error '5': Invalid procedure call or argument".

What I'm trying to do is have the Month variable be used to find the corresponding cell in two separate sections of the worksheet (prior month "BU2:CG411" vs current month "CI2:CU411") to then subtract them and paste the formula down a variance column('DY').

My apologies if this is a silly question/error I'm running into... I just recently started playing around with VBA after realizing the huge potential for automation! Thank you in advance!!!

VBA Code:
Sub test()

    Dim Month As Variant
    
    Month = InputBox("Enter current month abbreviation (e.g. Jan)", "Historical/Actual/CE Column Formatting")
    
    With ActiveSheet.Range("CI2:CU411")
        Set a = .Find(Month, LookIn:=xlValues)
        Set aa = ActiveSheet.Cells(a.Address).Offset(1, 0)
    End With

    With ActiveSheet.Range("BU2:CG411")
        Set b = .Find(Month, LookIn:=xlValues)
        aa = ActiveSheet.Cells(a.Address).Offset(1, 0)
    End With
    
        Range("DY3").Select
        ActiveCell.FormulaR1C1 = aa - bb

        Selection.Copy
        ActiveCell.Columns("A:A").EntireColumn.Select
        Selection.SpecialCells(xlCellTypeFormulas, 23).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hello all! I'm running into an error at the 'Set aa' line: "Run-time error '5': Invalid procedure call or argument".

What I'm trying to do is have the Month variable be used to find the corresponding cell in two separate sections of the worksheet (prior month "BU2:CG411" vs current month "CI2:CU411") to then subtract them and paste the formula down a variance column('DY').

My apologies if this is a silly question/error I'm running into... I just recently started playing around with VBA after realizing the huge potential for automation! Thank you in advance!!!

VBA Code:
Sub test()

    Dim Month As Variant
   
    Month = InputBox("Enter current month abbreviation (e.g. Jan)", "Historical/Actual/CE Column Formatting")
   
    With ActiveSheet.Range("CI2:CU411")
        Set a = .Find(Month, LookIn:=xlValues)
        Set aa = ActiveSheet.Cells(a.Address).Offset(1, 0)
    End With

    With ActiveSheet.Range("BU2:CG411")
        Set b = .Find(Month, LookIn:=xlValues)
        bb = ActiveSheet.Cells(b.Address).Offset(1, 0)
    End With
   
        Range("DY3").Select
        ActiveCell.FormulaR1C1 = aa - bb

        Selection.Copy
        ActiveCell.Columns("A:A").EntireColumn.Select
        Selection.SpecialCells(xlCellTypeFormulas, 23).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False

End Sub
Apologies, was in the middle of tinkering with the code and didn't fix variable names.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top