select cell in row 3 of (lastColumn)+1

Shakeable_Drip

Board Regular
Joined
May 30, 2023
Messages
50
Office Version
  1. 365
Platform
  1. Windows
I'm trouble shooting a portion of my VBA and I recieve the error: method 'range' of object'_worksheet' failed.
I'm trying to select the cell after last column, but I seem to select the cell in the last column.
VBA Code:
Sub LastColumn()
Dim sht As Worksheet
Dim LastColumn As Long

Set sht = ActiveSheet

LastColumn = Cells(3, Columns.Count).End(xlToLeft).Column
sht.Range(LastColumn & "3").Select
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What I usually do is use relative reference here. Try recording the macro. Select A1 (whatever cell), ctrl down to get to the last cell, turn on relative reference, go down one cell. Then if you need to write something in it you can use ActiveCell
 
Upvote 0
I'm trying to select the cell after last column


I am adding +1 to select the next cell after the last cell with data.

VBA Code:
Sub LastColumn()
  Dim sht As Worksheet
  Dim LastColumn As Long
  
  Set sht = ActiveSheet
  
  LastColumn = sht.Cells(3, Columns.Count).End(xlToLeft).Column + 1
  sht.Cells(3, LastColumn).Select
End Sub
 
Upvote 0
Solution
Change this line:
VBA Code:
sht.Range(LastColumn & "3").Select
to this:
VBA Code:
sht.Cells(3, LastColumn + 1).Select
 
Upvote 0
You are welcome. Glad we were able to help.

Yep, the two code answers are very similar. It is just a matter of when you want to add the one - in the calculation, or in the select statement.
It doesn't really make a difference which one you use.
If you were to use this "LastColumn" variable field in multiple lines of code, then it might save you a little bit of typing to add it to the calculation, like Dante shows. It will save you a few keystrokes.
 
Upvote 0
You are welcome. Glad we were able to help.

Yep, the two code answers are very similar. It is just a matter of when you want to add the one - in the calculation, or in the select statement.
It doesn't really make a difference which one you use.
If you were to use this "LastColumn" variable field in multiple lines of code, then it might save you a little bit of typing to add it to the calculation, like Dante shows. It will save you a few keystrokes.
Thanks! and yes my intent is to carry the same variable in multiple places. I'm posting another question about organization in a few moments. I think I have all the Pieces for my project, but I think my organization is wack, as I'm a super Newbie. I can probably bash my way to the goal from here but I'd love it to be clean and organized.
 
Upvote 0

Forum statistics

Threads
1,215,123
Messages
6,123,183
Members
449,090
Latest member
bes000

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