variable location for a formula

spacecaptainsuperguy

Board Regular
Joined
Dec 30, 2004
Messages
202
Office Version
  1. 365
Platform
  1. Windows
I am trying to automate a report that is calculating a payroll expense and whether or not it is over budget or under budget.

The way I envision this working is 2 input boxes collecting information about the particular day or week's payroll budget to then put in cell H40 (in this example...H40 is variable). In this example the formula in H40 is =190.29 x 6 which is the daily payroll number (input #1) times the number of days in the pay period (input #2). Where this formula will end up is variable based on how many people worked and the number of days gone by etc. What I assume would be the easiest way would be to have the code look in column B for 'Totals Department: FB' and then generate that formula (input#1 x input#2) six cells to the right in column H. I am not yet familiar enough with VBA to figure out how to go about setting something like that up. Any help you can offer is greatly appreciated.

SpaceCaptainSuperGuy
F&B Current Payroll.xls
ABCDEFGHIJ
34TSSmith,Taylor-JeanFBFWAIT6.2525.00Apr15/2008
35FBFWAIT7.5030.00Apr17/2008
36FBFWAIT8.2533.00Apr18/2008
37FBFWAIT10.0040.00Apr19/2008
38FBFWAIT4.2517.00Apr20/2008
39TotalsJob/Function:FWAIT218.501,163.92
40TotalsDepartment:FB218.501,163.921,141.74-22.18OverBudget
JNS5252
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
Sub CreateFormula()
Dim inp1 As Double, inp2 As Double

    With ActiveSheet
    
        Set cell = .Columns(2).Find("Totals Department: FB", LookIn:=xlValues)
        If Not cell Is Nothing Then
        
            inp1 = Application.InputBox("Supply value #1", Type:=1)
            If inp1 <> 0 Then
        
                inp2 = Application.InputBox("Supply value #2", Type:=1)
                If inp2 <> 0 Then
                
                    cell.Offset(0, 6).Formula = "=" & inp1 & "*" & inp2
                End If
            End If
        End If
    End With
End Sub
 
Upvote 0
Don't set the formula in code. Set the (H40) formula to reference two cells and set the two cells in the code. Let H40 move up and down as inserting and deleting rows dictate, and the code need not worry about it.

If you name cell I1 as Input1 and J1 as Input2 then your formula will be
=Input1 * Input2
and the code will essentially go
range("Input1") = inputbox("Enter first parameter")

I recommend error checking/handling.
 
Upvote 0
Wow. Short and sweet. It works great. Thank you very much XLD. Also thank you Gates. I will check to see if your method works as well.

SpaceCap.
 
Upvote 0
Good. I'm not disagreeing with XLD's answer, as he did exactly what you asked for, and accurately. I'm just suggesting that another approach may be more versatile. With the range names, you can insert and delete rows to your heart's content, but the ranges will still work - or they may never move at all. AAR you won't have to care where they are after you initially create them.

Meanwhile, you won't care where H40 is either now. It won't be "bound in matrimony" to being 5 columns away from the text string. And the text string can be changed, even if just to add whitespace. In each case, the code won't break with this approach.

But XLD's answer is still fine :)
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,693
Members
449,117
Latest member
Aaagu

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