Cells instead of range

bobkap

Active Member
Joined
Nov 22, 2009
Messages
323
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I would like to refer to cells with the Columns function but cannot figure out how to do it. Example: Rather than Columns("A:O").Select I'd prefer to use a variable so I can say Columns("A":lastcolumn") or something like that.

Maybe I should be asking how I can use variables with the Columns function?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Maybe:
Code:
Sub LastColumn()
Dim lastCol As Long
lastCol = Cells.Find("*", searchdirection:=xlPrevious).Column
Range("A:" & Replace(Split(Columns(lastCol).Address, "$")(1), ":", "")).Select
End Sub
 
Upvote 0
.. or perhaps this is what you mean?

Code:
Dim LastCol As String

LastCol = "P"
Columns("A:" & LastCol).Select
 
Upvote 0
You can also use the Cells call instead of Range. The difference is that Cells calls the ROW and then the COLUMN NUMBER instead of the Range style of COLUMN LETTER and then the ROW NUMBER.

Code:
Dim str As String
Dim i As Integer
i = 1
str = ActiveSheet.Range("A" & i).Value 'this uses the Range as Column Letter followed by Row number (A1) - or - Columns followed by row (A1:B20)
str = ActiveSheet.Cells(i, i).Value 'this uses Cells as Row Number followed by Column number (1, 1)
 
Upvote 0
The difference is that Cells calls the ROW and then the COLUMN NUMBER
Just so readers have the complete picture, the Cells object's column argument can be either the COLUMN NUMBER or the COLUMN LETTER (quoted if supplied as a text constant). So either of these would reference the same cell...

Cells(2, 5)

or

Cells(2, "E")
 
Last edited:
Upvote 0
Just so readers have the complete picture, the Cells object's column argument can be either the COLUMN NUMBER or the COLUMN LETTER (quoted if supplied as a text constant). So either of these would reference the same cell...

Cells(2, 5)

or

Cells(2, "E")

I did not realize that.
Many thanks :)
Learn something new every day!
 
Upvote 0
Thanks. Presumably, you could also use a variable instead of a column number or letter? Is that correct?
 
Upvote 0
As long as it is a predefined variable, yes. Much like the example above with the 'i' being defined as '1'. Example:

Code:
Dim str As String
Dim Col as String
Col = "C"
Dim RowN As Integer
RowN = 10
str = ActiveSheet.Range(Col & RowN).Value
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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