How do I combine both SUBs into a single SUB and create a dynamic subtraction formula of column J - column K

Justin8506

New Member
Joined
Apr 7, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Capture.JPG



How do I

1. combine both SUBs into a single SUB
2. create a dynamic subtraction formula of column J - column K

VBA Code:
Sub DynamicLookup()
Dim wb As Workbook
Workbooks.Open ("V:\Mar22\Top Segments & Top All_Mar22.xlsx")

Set wb = ActiveWorkbook
ThisWorkbook.Activate
Dim i As Long
For i = 2 To 22

Range("K" & i).Value = WorksheetFunction.Vlookup(Range("F" & i).Value, wb.Sheets("TopSegments").Range("E:I"), 5, 0)
Next i

End Sub

Sub InsertColumn()

Dim LastRow As Long
Dim LastCol As Long
Dim iRow As Long

Set ws = Sheet3

With ws
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

.Cells(1, LastCol + 1).Value = "Variance"

End With

End Sub

Apologies for the redacted info due to confidentiality.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
View attachment 61987


How do I

1. combine both SUBs into a single SUB
2. create a dynamic subtraction formula of column J - column K

VBA Code:
Sub DynamicLookup()
Dim wb As Workbook
Workbooks.Open ("V:\Mar22\Top Segments & Top All_Mar22.xlsx")

Set wb = ActiveWorkbook
ThisWorkbook.Activate
Dim i As Long
For i = 2 To 22

Range("K" & i).Value = WorksheetFunction.Vlookup(Range("F" & i).Value, wb.Sheets("TopSegments").Range("E:I"), 5, 0)
Next i

End Sub

Sub InsertColumn()

Dim LastRow As Long
Dim LastCol As Long
Dim iRow As Long

Set ws = Sheet3

With ws
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

.Cells(1, LastCol + 1).Value = "Variance"

End With

End Sub

Apologies for the redacted info due to confidentiality.
Hello,

Combine the Subs simply

Sub Combine
Call DynamicLookup
Call InsertColumn()
End Sub

Jamie
 
Upvote 0
Solution

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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