VBA - Find two columns, divide them and put the results in a new third column

eclondon

New Member
Joined
Mar 10, 2016
Messages
1
Hello everyone

I have a worksheet that has multiple columns, e.g.:

a b c d e
1 2 3 4 5
6 7 8 9 0

What I need to do is 'find' columns B and D and create a new column:

a b c d e f
1 2 3 4 5 =2/4
6 7 8 9 0 =7/9

I am trying to use this macro to find each column (one is called MtM/D):

Code:
 Dim rngAddress As Range
  Set rngAddress = Range("A3:Z3").Find("MtM/D")
  If rngAddress Is Nothing Then
    MsgBox "MtM/D column was not found."
    Exit Sub
  End If
  Range(rngAddress, rngAddress.End(xlDown)).Select
  ActiveWorkbook.Names.Add Name:="MtMD", RefersTo:=Selection

[Problem: XlDown stops when there is a gap in the column]

And this one to divide the two ranges, which creates a new column after the last (MTM Date):

Code:
Dim Found As Range
Dim LR As Long
Set Found = Rows(3).Find(what:="MtM Date", LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then Exit Sub
LR = Cells(Rows.Count, Found.Column).End(xlUp).Row
Found.Offset(, 1).EntireColumn.Insert
Cells(3, Found.Column + 1).Value = "Percentage"
Range(Cells(4, Found.Column + 1), Cells(LR, Found.Column + 1)).Formula = "<named ranges>"
End Sub

I am stuck with the range selection and how to divide the two ranges. Can anyone help please!?

Thank you!!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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