Range that Changes

karentx09

New Member
Joined
Feb 9, 2023
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Hi y'all!

The range I am referencing changes length because it is in a pivot table. What I did in the macro recording was clicked cell M4, control+down and then shift+up (since the last cell is the total and I don't need that). This is the code it spits out:
Range("M4").Select
Range(Selection, Selection.End(xlDown)).Select
Range("M4:M30").Select
Selection.Copy

However, since the range will vary from month to month, I do not want it to only reference M4:M30; I would really like to rely on the key strokes. This issue flows throughout my macros, so looking to implement multiple places, but this is simply the start. Thank you in advance for your help!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi karentx09,

maybe

VBA Code:
Sub MrE1229538_1702817()
'https://www.mrexcel.com/board/threads/range-that-changes.1229538/
  Dim lngLastRowDR As Long
  
  Const cstrColLetter As String = "M"
  Const clngStartROw As Long = 4

  lngLastRowDR = Cells(clngStartROw, cstrColLetter).End(xlDown).Row - 1
  Range(Cells(clngStartROw, cstrColLetter), Cells(lngLastRowDR, cstrColLetter)).Copy
End Sub

Ciao,
Holger
 
Upvote 0
If you are just worried about the last row in the column changing you might consider the following:

VBA Code:
    Range("M4:M" & Range("M" & Rows.Count).End(xlUp).Row - 1).Copy
 
Upvote 0
Solution
If you are just worried about the last row in the column changing you might consider the following:

VBA Code:
    Range("M4:M" & Range("M" & Rows.Count).End(xlUp).Row - 1).Copy
Hi @johnnyL, thanks for the reply! I'm working my way through the first macro implementing this, but curious, why the "-1"? Shouldn't End(x1Up) take it to the last cell in the list and back up one?

Will touch base once I update and run.
 
Upvote 0
Hi karentx09,

maybe

VBA Code:
Sub MrE1229538_1702817()
'https://www.mrexcel.com/board/threads/range-that-changes.1229538/
  Dim lngLastRowDR As Long
 
  Const cstrColLetter As String = "M"
  Const clngStartROw As Long = 4

  lngLastRowDR = Cells(clngStartROw, cstrColLetter).End(xlDown).Row - 1
  Range(Cells(clngStartROw, cstrColLetter), Cells(lngLastRowDR, cstrColLetter)).Copy
End Sub

Ciao,
Holger
Hi @HaHoBe, Thanks so much for the reply! Not sure I am reading the logic correctly, but are you defining the location and then telling it to run from the location? Unfortunately, that would not work for my scenario because this occurs throughout my macro, but in different places. The solution @johnnyL provided (minus the -1) worked well. I did go look up LngLastRow, so I learned something new! Thanks!
 
Upvote 0
If you are just worried about the last row in the column changing you might consider the following:

VBA Code:
    Range("M4:M" & Range("M" & Rows.Count).End(xlUp).Row - 1).Copy
@johnnyL That did the trick! Took out the -1 and was able to tweak it to work in maybe 8 different places and an additional 6 places by taking out the "End(x1Up)." Thank you!
 
Upvote 0
Hi karentx09,

but are you defining the location and then telling it to run from the location?

Actuallay the code starts at the location which johnnyL hadrcoded to be "M4" (my equal to this is Cells(clngStartROw, cstrColLetter)) but this is only the start for the area to copy as the range property awaits two addresses so

VBA Code:
    Range("M4:M" & Range("M" & Rows.Count).End(xlUp).Row - 1).Copy


and if the constants and variables are filled

VBA Code:
  Range(Cells(clngStartRow, cstrColLetter), Cells(Rows.Count, cstrColLetter).End(xlup).Offset(-1)).Copy

and

VBA Code:
  Range(Cells(clngStartROw, cstrColLetter), Cells(lngLastRowDR, cstrColLetter)).Copy

all work for the same range. ;)

Holger
 
Upvote 0
@karentx09 The -1 was put in there because you said the last row will be a total and that you don't need that.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,922
Members
449,094
Latest member
teemeren

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