How to use VBA to reference a column letter instead of number in worksheet function

Excellor_From_London

Board Regular
Joined
Aug 5, 2007
Messages
50
Hi,

I've set up dynamic ranges that feed into a formula inserted via VBA below. Most of this works fine but I'm struggling to how reference the last column in the formula.

The last column is determined dynamically via the following.

Missing = Range("B1").End(xlToRight).column

However it returns the column number, not the actual column letter, e.g. if the last column is column G, it returns 7 and instead of the letter. But I need the letter of the column for the function to work. Currently all the other parameters work fine, it is just missing part that is cause the code to fall over.

Is that a way to translate a column number into the equivalent column letter?

Hope someone can suggest something.

Thanks,

Fellow Excellor

Set Regout = Range("g3") 'cell to start populating values
Lastrow = Range("B1").End(xlToRight).End(xlDown).Value
startrange = Range("A11").Value
Missing = Range("B1").End(xlToRight).column

Regout.FormulaArray = "=linest(" & "B" & startrange & ":" & "B" & lastrow & "," & "c" & startrange & ":" & MISSING & lastrow & ",true,true)"
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You could either Enum columns A through IV as 1 through 255 or you could get the cell address with Range("B1").End(xlToRight).Address and use Excel's Split function to extract the column letter.

For example:

If Range("B1").End(xlToRight).Address returns $G$1, then you could use Split(Range("B1").End(xlToRight).Address, "$")(1) to return the 'G'.
 
Upvote 0
How to return relative referance via the Address Function in a formula

Hi,

I'm trying to use the address function to insert a formula. But the following line only returns an absolute reference to the underlying cells, not a relative.

i.e U14 returns the following underlying formula: =$B$14-$S$14

Cells(startrange, "U").Formula = "=" & Cells(startrange, 2).Address & "-" & Cells(startrange, 19).Address

I want to return a relative referance instead: =B14-S14. How can I change the formula to accomodate this?

Thanks,

FE
 
Upvote 0
Try

Code:
Cells(startrange, "U").Formula = "=" & Cells(startrange, 2).Address(False, False) & "-" & Cells(startrange, 19).Address(False, False)
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,722
Members
452,939
Latest member
WCrawford

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