Range defining problem

Gromace

New Member
Joined
Jan 11, 2011
Messages
15
Hello,

I have the following code in an Excel 2007 macro:

Range("M5:M18264").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _:=False, Transpose:=False

The problem is that I will not know the length of the range each time I run the macro. The cells contain a formula and I am trying to get rid of the formula and paste just the values. How can I define and select that range automatically then copy it and paste special values?

Any help would be greatly appreciated!

Thanks in advance,

Gromace
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Assuming your range is continuous, how about this:

Code:
        Dim lr As Long
        lr = Range("M5").End(xlDown).Row
        With Range("M5:M" & lr)
            .Copy
            .PasteSpecial xlPasteValues
        End With

That should do ya.
 
Upvote 0
What will define the variable length. Will the range always begin at the same cell (M5), but the number of cells changes, is the last cell of the range always the last row of your data?
 
Upvote 0
Try:-
Code:
With Range(Range("M1"), Range("M" & rows.Count).End(xlUp))
.value = .value
End With
 
Upvote 0
Or:
Code:
Dim lr As Long
lr = Range("M5").End(xlDown).Row
Range("M5:M" & lr).Value = Range("M5:M" & lr).Value
 
Upvote 0
Thank you so much for all of the replies! I used the first one, from Airfix9, because it was first and it worked. I did not test the suggestions from MickG and jmthompson, but I still appreciate your response and your time.

Speaking of time, I'd like to thank all of you at Mr.Excel. My problem was solved, most likely multiple times, by three different individuals, in less than 8 minutes! Fantastic!!!

What a great group of people!

Thanks again,

Gromace
 
Upvote 0
Hi Guys,

Just wanted to say thank you, I hunted for something like this for hours.

Really appreciate it.

I used AirFix's option as it was first one I tested.

Craig
 
Upvote 0

Forum statistics

Threads
1,224,612
Messages
6,179,890
Members
452,948
Latest member
Dupuhini

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