retrieve the column letter for use in a formula

grapevine

Board Regular
Joined
May 23, 2007
Messages
208
I have been successfully using the following to retrieve the row number to use for formulas as in the following example
Code:
Dim rw As Long
rw = Range("a65536").End(xlUp).row
Range("R2:R" & rw).FormulaR1C1 = "=trim(rc[-17])"
but want to now retreive a column letter. I have adapted the following but when I hover my mouse over the line of code it is coming up with the number 13 rather than M
Code:
Dim cw As String
cw = Range("IV1").End(xlToLeft).Column
Range("A1:" & cw & "1").Select
In the last line of code if I replace the cw with the letter M in quotes the macro works so I think it must be how I have set the Dim statement, but I have tried different options but none seem to help as I keep getting runtime error 1004

I would be really grateful if someone could point out where I am going wrong
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
unfortunately the VBA code doesn't return the column letter but the column number...
thats why you are getting 7

use this code instead

Dim cw As Integer
cw = Range("IV1").End(xlToLeft).Column
ActiveSheet.Range(Cells(1, 1), Cells(1, cw)).Select
 
Upvote 0

Forum statistics

Threads
1,203,674
Messages
6,056,680
Members
444,881
Latest member
Stu2407

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