VBA to change as per column

shiva_reshs

Board Regular
Joined
Sep 5, 2012
Messages
68
Hi,

I wrote below code which helps me to find header and update the formula in the cell value. But i need little tweak where formula be changed if the header is found in Column J instead of column I. Please advose

Code:
Sub CopyColumnByTitle()'Find "Name" in Row 1
  With Sheets("Week1").Rows(12)
   Set t = .Find("Total", lookat:=xlPart)


     If Not t Is Nothing Then
     
    Dim i As Long
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Range("B" & i).Value <> "" Then
         Range("t" & i).Formula = "=RC[-3]+RC[-2]+RC[-1]"
           End If


  
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
"t" is the range where it found the word "Total".
So, after you find it, you can use "t.Column" to return which column it is found in.
.Column returns the column number, so "I" is 9, "J" is 10.
 
Upvote 0
I am guessing that maybe you are having issues with this part here:
Code:
Range("t" & i)
When using ".Column" to get the column, it is returning the column number, not the letter.
But when accessing ranges using "Range(...)", it is looking for the column letter, not the number.
So we cannot use that method of referencing the range. Use Cells(row,column) instead, where you can use the Column number or Column letter.

So that reference would look like:
Code:
Cells(i,t.Column)
 
Upvote 0

Forum statistics

Threads
1,215,215
Messages
6,123,668
Members
449,114
Latest member
aides

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