Making ranges in a macro dynamic?

sadia1991

New Member
Joined
Aug 10, 2014
Messages
11
Hello!

This is the macro I have recorded:
Rich (BB code):
Range("A1:G1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range("A1:G212").Select
    Selection.Copy
    Windows("arrangement.xlsx").Activate
    Range("A1").Select
    ActiveSheet.Paste
    Range("L1:L55").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("MINING.xlsx").Activate
    Range("RA73:RA127").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Windows("!2014-05-15 experiment").Activate
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    Range("A1").Select
End Sub
How do I make the ranges in red (above) dynamic?
for the line Range("A1:G212").Select I was trying to select a block of cells (which I can do by pressing ctrl + A and then shift+down arrow OR ctrl+shift+down 12 times then shift+up arrow once)

for the line Range("RA73:RA127").Select I was trying to select a block of cells adjacent to the cell already active in the sheet (the idea is that I would paste a block of cells then repeat the macro and the next block of cells would get pasted in the next column etc.)


Thank you for taking the time to read and respond to my question! Any help is greatly appreciated!
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You should define the Last Row with data like this:

Code:
[COLOR=#0000ff]Dim[/COLOR] LastRow [COLOR=#0000ff]As Long[/COLOR]

LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row

and then use this variable in your range references:

Code:
[COLOR=#000000]Range("A1:G" & LastRow).Select [/COLOR]

To offset the selection over one column to the right use this:

Code:
Range("RA73:RA127").Offset(, 1).Select

Hopefully this will help. Let me know if you have anymore questions.
 
Last edited:
Upvote 0
Thank you mrmmickle1 for your reply! but I'm really confused, I'm not sure what to look for in the lastrow, I was hoping maybe if I put a sample data here my question would make more sense:

A B C D E F G (column in excel)
StartDate:06/21/12
EndDate:05/15/14
Subject:F1O
Experiment:0
Group:0
Box:3
StartTime:11:29:20
EndTime:11:58:25
MSN:
C:1299
A:
0:001309000750100
5:000.530003500500
10:000
B:
0:0045001530
5:0001302369
10:009855001
15:000.0830010-10
20:0030228615198
25:00:002000000
30:00:0009630
D:
0:00211.826153045
5:003620220.15184188
10:000
E:
0:000.170.290.220.120.22
5:000.04
F:
0:0032.260.340.030.040.04
5:000.10.020.070.080.1
10:000.260.770.530.270.03
G:
0:000.887773.937771.51
5:007772.237770.87777
10:001.987771.067771.51
15:007771.397771.01777
20:001.717771.037770.89
25:00:007771.717771.86777
H:
0:000.090.137770.491.25
5:001.371.752.317770.3
10:007770.741.187770.15
15:007770.720.917770.24
20:007770.467770.54777
25:00:000.10.27770.620.71
30:00:000.937770.110.16777
35:00:000.077770.110.480.58
40:00:007770.090.580.81777
I:
0:0030.060.440.470.130.3
5:000.190.050.720.110.13
10:000.330.80.60.370.15
15:002.970.440.110.320.39
20:000.280.190.9277730.14
25:00:000.840.060.390.070.22
30:00:000.260.050.10.420.53
35:00:000.130.120.520.131.71
40:00:000.480.260.130.870.13
45:00:000.510.130.10.990.07
50:00:000.470.210.0477730.83
55:00:000.670.060.410.370.31
J:
0:000.10.040.020.520.06
5:000.060.020.41.010.42
10:000.170.550.120.041001
15:0039.750.220.160.10.05
20:000.320.030.090.090.31
25:00:000.030.440.30.030.04
30:00:000.550.1100132.780.38
35:00:000.060.120.860.130.57
40:00:000.020.040.50.030.23
45:00:000.0310010.720.040.07
50:00:000.370.020.220.030.64
55:00:000.980.020.10.031001
60:00:0038.350.051.280.020.46
65:00:001.170.04100133.420.23
70:00:000.281.030.721.051001
75:00:000.580.170.420.60.02
80:00:000.020.50.110.020.21
85:00:000.26100133.170.020.7
90:00:000.040.250.140.090.73
K:
0:0040.920.450.030.360.05
5:000.080.040.030.080.24
10:000.230.080.080.320.09
15:00100135.760.290.030.16
20:000.350.240.55100137.81
25:00:000.020.06100140.010.15
30:00:000.720.020.020.190.04
35:00:001001320.490.061.62
40:00:000.620.030.0210010.08
45:00:000.850.02100133.050.1
50:00:000.430.040.040.030.03
55:00:000.050.040.10.020.04
60:00:000.110.030.020.031001
65:00:0033.250.30.040.050.04
70:00:000.030.110.580.040.14
75:00:000.250.470.0410010.03
80:00:000.180.020.030.540.04
85:00:000.110.030.240.030.23
90:00:000.740.240.2810010.73
95:00:000.10.160.020.60.1
100:00:000.140.050.070.030.18
105:00:000.10.030.260.190.2
O:
0:001-1
P:
0:001-1-11-1
5:0011-1-11
10:00-111-11
15:001-11-1-1
20:0011-1-11
25:00:00-1-111-1
empty row
StartDate:06/15/12
EndDate:05/25/14
Subject:F2O

<colgroup><col width="64" span="7" style="width:48pt"> </colgroup><tbody>
</tbody>
etc.... the data for the next subject is similar to above (same variables) BUT the variables E: F: G: H: I: J: K: O: and P: can have different number of rows. eg. one subject may have 22 data points in "G:" while another may have 135.

I want to be able to select the whole block for the subject and the empty row between that and the next block. then copy and paste this into another sheet (that extracts the important data and then copy Range("L1:L55") from this 2nd sheet into a 3rd sheet where I'm compiling the data. In the 3rd and final sheet I want to be able to paste the data in adjacent columns automatically (the offset doesn't work because I have to repeat this process many times- a few 1000- and I don't want to always paste it in the column next to "RA73:RA127") :(

I hope this makes sense! I really really appreciate your help. Thank you so so much for taking the time to read this.
 
Upvote 0
ahhh so sorry about that :( my column headings got reformatted but basically all my data is in columns A-G
 
Upvote 0
sadia1991,

It's difficult to decipher what is going on based on the sample data being formatted funny in Post # 3, but I think you want to do something like this. It will increment through columns one at a time up to Column 550... :


Code:
[COLOR=#0000ff]Sub [/COLOR]Test()

     [COLOR=#0000ff]  Dim[/COLOR] i [COLOR=#0000ff]As Integer[/COLOR]

    [COLOR=#0000ff]   For[/COLOR] i = 469 [COLOR=#0000ff]To[/COLOR] 550 [COLOR=#0000ff]Step[/COLOR] 1 [COLOR=#008000]'469 signifies Column RA the next selection will be Column RB (470)[/COLOR]

                Range(Cells(73, i), Cells(127, i)).Offset(, 1).Select

[COLOR=#008000]           'Your Paste Data Code Here[/COLOR]

     [COLOR=#0000ff]  Next[/COLOR] i

[COLOR=#0000ff]End Sub[/COLOR]

I am confused as to how your data is laid out. If you can get a better example of the data I can probably help you further.

Do you know you many iterations you need?
 
Upvote 0
This is what the data looks like: (the different variables E: F: G: H: I: J: K: O: and P: can have different number of rows.)
https://drive.google.com/file/d/0B5d-7iW5ZlFwTjFlY1pfRUxYMEk/edit?usp=sharing

I want to be able to select the whole block for each 'subject' and the empty row right below it (everything in black border in the above file). then copy and paste this into another sheet (that extracts the important data and then copy Range"L1:L55" ( this files gives me a single column of 55 cells that I copy) from this 2nd sheet into a 3rd sheet where I'm compiling the data.

In the 3rd sheet I want to be able to paste the data in adjacent columns, sample sheet here:
https://drive.google.com/file/d/0B5d-7iW5ZlFwY3YtdF9OR2Vzd1U/edit?usp=sharing
so if I already had the first 2 columns of data i'd want the next column in C10:C64, and the next in D10:D64 etc...

I need to repeat this macro at least 5000 times. would that change the code in your previous reply?

Thank you so so much for helping me out!
 
Upvote 0
update!!
I found a way to copy the data from the first file!! :) using
Code:
Range("$A$1").CurrentRegion.Select

now I need to figure out how to paste it in the correct position in the 3rd file!

so sorry for taking up so much of your time. really appreciate all your help!
 
Upvote 0
sadia1991,

I got a chance to look at your files. I had a few questions.

So you want to copy and paste this data in blocks. It appears to me that one row above the Third occurrence of the word "Start" in column A could define a block. (Is This Correct?) If so this link may prove useful: Find Nth Occurance Also Is a block always the same number of rows? Say 100 rows? (The main importance is if the number is static....

So when you say paste this data into another sheet what is the sheet named? Is this a calculation sheet? Once the data is pasted to this calculation **** I assume it spits out numbers in column L1:L55? You want to copy this data into a brand new sheet until you run out of subjects to complete this process on?

How many subjects to you think you have? 5000? Anyway to get a solid number?

I think I may be able to help you with your task. I just need to drill down the specifics of the process so I can understand what you would like for an end result.....
 
Upvote 0
Hi mrmmickle1!! :)
I was able to figure out the code, thank u so much for all your help. I ended up using the offset function and it did the job!

I do have another question tho; is there a way to make the selection in the offset function dynamic? Ie if in my macro I'm selecting all cells with a specific number in them, then I want to 'offset' the selection (select the cells 2 rows directly below the current selection. But the number of cells selected in the first step vary. How would the code change for this?

Thank you and have a lovely week! :)
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,004
Members
449,203
Latest member
Daymo66

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