VBA Formula Help

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
143
Office Version
  1. 365
Platform
  1. Windows
I have the below spreadsheet where column M can have different values. I need to insert a formula using VBA into column N based on the value in column M.

If M29 = "JTO/HEN002" then (K2+L2)/'Locations'!D2 but if M29 = "JTO/KIL003" then (K2+L2)/'Locations'!D3. I have the list of 'Locations' below. To complicate this, the number of rows in my spreadsheet can vary from week to week.

Any ideas?

Screenshot 2023-02-22 141232.png


Screenshot 2023-02-22 141256.png
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Chaddres,

Give this a try and see if it works for what you are looking to do:

VBA Code:
Sub InsertFormulas()
    Dim lastRow As Long
    Dim i As Long
    
    lastRow = Cells(Rows.Count, "M").End(xlUp).row
    
    For i = 2 To lastRow
        If Cells(i, "M").Value = "JTO/HEN002" Then
            Cells(i, "N").Formula = "=(" & Cells(i, "K").Address & "+" & Cells(i, "L").Address & ")/'Locations'!D2"
        ElseIf Cells(i, "M").Value = "JTO/KIL003" Then
            Cells(i, "N").Formula = "=(" & Cells(i, "K").Address & "+" & Cells(i, "L").Address & ")/'Locations'!D3"
        End If
    Next i
End Sub
 
Upvote 0
Unfortunately this will not work because my list of locations (shown in post) has quite a few options. And, it could even grow in the future. Thank you though.
 
Upvote 0
Sorry, let me ponder this a bit. In the meantime, someone else may come along and solve it before my brain comes up with it. :)
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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