VBA to copy formula from one cell to variable range

rasinc

Board Regular
Joined
Jun 28, 2011
Messages
119
I am trying to copy a cell formula from one cell to a column that starts at cell G6 but is variable length.

I created a macro and the copy worked but with a variable number of rows, the code created may not work.

So I have no idea how to come up with the formula for the destination in the following code

Code:
    Sheets("Setup").Select
    Range("J2").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("PriceLists").Select
    Range("G6").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.AutoFill Destination:=Range("G6:G90")  'here I believe may be the issue
    Range("G6:G90").Select

So I want to be able to copy but down as many rows as may be there. However, I do not know how to determine the range to copy based on what is in column F.

Any help is appreciated.

TIA, rasinc
 
I am a little less familiar with this code, but I believe you are suggesting I replace:

Code:
LR = Range("A" & Rows.Count).End(xlDown).Row
with:
Code:
LR = Range(strStartCol & Rows.Count).End(xlUp).Row
which gives me the final code:
Code:
    'Paste in Due/Ready formulas and formatting
    Range("C6:M6").Select
    Selection.Copy
    Range("C6").Select
    Selection.End(xlDown).Offset(1).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    'Pastes format into last row
    Range("B6:M6").Select
    Selection.Copy
    Range("M6").Select
    Selection.End(xlDown).Offset(0, -11).Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    
    'Copies down formulas on Orders tab
    Windows("CAL.CSV").Activate
    Range("C1").Select
    Dim LR As Long
    LR = Range(strStartCol & Rows.Count).End(xlUp).Row
    Windows("PFP.xlsm").Activate
    Range("M6").Select
    Range(ActiveCell, Cells(LR, ActiveCell.Row)).FillDown

Is that correct? When I run this macro it gets hung up on the portion that I changed. I think I am adding this in at the wrong part.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I am a little less familiar with this code, but I believe you are suggesting I replace:
Is that correct? When I run this macro it gets hung up on the portion that I changed. I think I am adding this in at the wrong part.

No sorry. I was answering based on the original topic of the thread, where I posted a question that asked how to copy a formula in a cell to cells in a variable number of rows. eg. copy the formula in C1 to D5:D10.

Your question is different and should probably be in a different message thread. Now that I am reading it more carefully, you want to look at the calc.csv file for the number of rows. Then copy a formula in pfp.xlsm down the same number of rows from a starting point.

Could this be accomplished by knowing what your range selection and final destination are and then after activating pfp.xlsm

Code:
Selection.Copy
Range("A" & row).PasteSpecial Paste:=xlPasteAll

Just change Range("A" & row) to what ever destination you want. Change the Paste:= to formulas, formats, etc. as needed.

Now if in fact you are attempting to count the number of rows in calc.csv and that number today happens to be three, then you want to paste the cells M6:M10 three times, you would need a loop to do the PasteSpecial three times and Range("A" & row) would then be "A" & row and on each iteration of the loop, add the number of rows to row so each paste of the range starts x rows down from the first. This would then start each set at M6, M11 and M16.

Tomorrow calc.csv might contain 15 rows, so the loop would function 15 times to copy the range down that many times.
 
Upvote 0
I'm guessing that the LR bit isn't working correctly, and this snippet could be the reason.
Change
Code:
[COLOR=#333333]LR = Range("A" & Rows.Count).End(xlDown).Row[/COLOR]
to
Code:
LR = Range("A" & Rows.Count).End(xlUp).Row

See if that fixes the problem.

Denis
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,680
Members
449,116
Latest member
HypnoFant

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