VBA Macro Improvement

heffo500

New Member
Joined
Sep 28, 2016
Messages
44
Hi

I have this macro I wrote which drags down formulas to the last row the formulas are contained in a certain row,

Sub CopyFormula()


Dim LastRow As Long
Dim Row As Long
Dim Ans As Long
Dim ColNo As Long


LastRow = Sheet2.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row


Ans = MsgBox("Continue?", vbYesNo + vbQuestion, "Tell Me")
If Ans = vbNo Then Exit Sub

ColNo = 2
Sheet2.Cells(1, ColNo).Select
Selection.Copy
Range(Cells(2, ColNo), Cells(LastRow, ColNo)).Select
ActiveSheet.Paste

ColNo = 3
Sheet2.Cells(1, ColNo).Select
Selection.Copy
Range(Cells(2, ColNo), Cells(LastRow, ColNo)).Select
ActiveSheet.Paste

End Sub

However I want to condense the part below to that I can state drag down columns D:E so to speak:


ColNo = 2
Sheet2.Cells(1, ColNo).Select
Selection.Copy
Range(Cells(2, ColNo), Cells(LastRow, ColNo)).Select
ActiveSheet.Paste

ColNo = 3
Sheet2.Cells(1, ColNo).Select
Selection.Copy
Range(Cells(2, ColNo), Cells(LastRow, ColNo)).Select
ActiveSheet.Paste

So it looks like this:
ColNo = 2:3
Sheet2.Cells(1, ColNo).Select
Selection.Copy
Range(Cells(2, ColNo), Cells(LastRow, ColNo)).Select
ActiveSheet.Paste

However when Ive tried to do this I get errors.

Thanks
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
So it looks like this:
ColNo = 2:3

One way...

Code:
Sub CopyFormula()


    Dim LastRow As Long
    Dim Row As Long
    Dim Ans As Long
    Dim ColNo As Long


    LastRow = Sheet2.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row


    Ans = MsgBox("Continue?", vbYesNo + vbQuestion, "Tell Me")
    If Ans = vbNo Then Exit Sub

    ColNo = 2
    With Sheet2
        .Cells(1, ColNo).Resize(, 2).Copy .Range(.Cells(2, ColNo), .Cells(LastRow, ColNo)).Resize(, 2)
    End With
End Sub
 
Last edited:
Upvote 0
One way...

Code:
Sub CopyFormula()


    Dim LastRow As Long
    Dim Row As Long
    Dim Ans As Long
    Dim ColNo As Long


    LastRow = Sheet2.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row


    Ans = MsgBox("Continue?", vbYesNo + vbQuestion, "Tell Me")
    If Ans = vbNo Then Exit Sub

    ColNo = 2
    With Sheet2
        .Cells(1, ColNo).Resize(, 2).Copy .Range(.Cells(2, ColNo), .Cells(LastRow, ColNo)).Resize(, 2)
    End With
End Sub

So how would I extend this to the third column?
 
Upvote 0
Did you try it? It already copies columns 2 & 3.
 
Upvote 0
Change the 2 places where it says Resize(, 2) to Resize(, 3)
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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