Referencing last column and row

jpj31

New Member
Joined
Apr 26, 2022
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
I've defined the last row and column as follows:
Dim lastcolumn As Long
Dim totalrow As Long

' Initialize to start at bottom of column D
totalrow = ws1.Cells(Rows.Count, "D").End(xlUp).Row

' Define last column in row 16
lastcolumn = ws1.Cells(16, Columns.Count).End(xlToLeft).Column

I'm trying to set a range from K16 through the last column and last row and filldown. Columns are being added and causing me to manually change the last column reference from my original code. How can I reference this? Below is what I tried, but it's not working.

ws1.Range("k16:" & lastcolumn & totalrow).Select
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
The issue is that lastcolumn is returning a number, not a letter.
So you need to use CELLS (which allows you to use numbers for columns) instead of RANGE.

Try:
VBA Code:
ws1.Range(ws1.Cells(16, 11), ws1.Cells(totalrow, lastcolumn)).Select
 
Upvote 0
I personnally don't like hardcoding cells in VBA code. Although I'm no expert, I think it's best to fully proceed with automated function capable of finding these coordinates.
This being said, I'd go with the address function. such as

Dim lastcolumn As Long, totalrow As Long, startcel as string, endcel as string
totalrow = ws1.Cells(Rows.Count, "D").End(xlUp).Row
lastcolumn = ws1.Cells(16, Columns.Count).End(xlToLeft).Column
startcel="K16"

endcel= ws.Cells(totalrow,lastcolumn).address

ws.1.Range(startcel & ":" & address").Select
 
Upvote 0
I personnally don't like hardcoding cells in VBA code. Although I'm no expert, I think it's best to fully proceed with automated function capable of finding these coordinates.
This being said, I'd go with the address function. such as

Dim lastcolumn As Long, totalrow As Long, startcel as string, endcel as string
totalrow = ws1.Cells(Rows.Count, "D").End(xlUp).Row
lastcolumn = ws1.Cells(16, Columns.Count).End(xlToLeft).Column
startcel="K16"

endcel= ws.Cells(totalrow,lastcolumn).address

ws.1.Range(startcel & ":" & address").Select
I made a mistake in the last line: ws.1.Range(startcel & ":" & endcel).Select
 
Upvote 0
I personnally don't like hardcoding cells in VBA code. Although I'm no expert, I think it's best to fully proceed with automated function capable of finding these coordinates.
This being said, I'd go with the address function. such as

Dim lastcolumn As Long, totalrow As Long, startcel as string, endcel as string
totalrow = ws1.Cells(Rows.Count, "D").End(xlUp).Row
lastcolumn = ws1.Cells(16, Columns.Count).End(xlToLeft).Column
startcel="K16"

endcel= ws.Cells(totalrow,lastcolumn).address

ws.1.Range(startcel & ":" & address").Select
Yeah, the more I'm utilizing VBA the more I agree with you. Thanks for the help, I'm learning a ton!
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,142
Members
452,615
Latest member
bogeys2birdies

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