Excel Macro to copy Multiple Columns and dynamic rows

Hi All,

Here is a simple macro I created. I have a fixed number of columns and the rows change daily. I need a macro to copy from one sheet to another.

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'
    Range("A1:C17").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
LVL 19
Edward PamiasTeam Lead RRS DeskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SixSigmaGuyCommented:
Not really sure what your question is.  Are you saying that the NUMBER of rows change?  If so, then here's your answer.  I'm making assumptions here that the first row is always row 1; so you'll need to modify that isn't the case.  I'm also assuming that column 1 contains data for every row.  You'll need to modify if that isn't the case either.

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'
    iLastRow = iLastRowFilledInColumn(Me, 1)  ' see my function below
    stRange = "A1:C" & iLastRow
    Range(stRange).Select
    Selection.Copy
    Sheets("Sheet1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

Function iLastRowFilledInColumn(ws As Excel.Worksheet, iColumn As Long) As Long
    Dim iLastRow As Long
   
    iLastRow = ws.Cells(ws.Rows.Count, iColumn).End(xlUp).Row
    '
    ' If the entire column is empty, Excel still returns 1 even though
    ' there is no data in row 1.  Therefore, check to see if the
    ' cell is empty; if it is, return 0 instead of 1.
    '
    If iLastRow = 1& Then
        If Trim(ws.Cells(iLastRow, iColumn)) = "" Then
            iLastRow = 0&
        End If
    End If
   
    iLastRowFilledInColumn = iLastRow
End Function
0
byundtMechanical EngineerCommented:
You may prefer to paste the values in a specific location in Sheet1. If so, consider this variation on your original code. Like SixSigmaGuy's code, it assumes that the data starts in cell A1 and continues until the bottom of data in column A. The corresponding rows of columns A:C are copied, then pasted into cell A1 of Sheet1.

If you really prefer pasting the values into whichever cell was last active in Sheet1, then remove the single quotes from the statements following rg.Copy and delete the next to last statement (Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues)
Sub DataCopier()
' Keyboard Shortcut: Ctrl+Shift+G
'
Dim rg As Range
With ActiveSheet
    Set rg = .Range("A1")   'First cell to copy
    Set rg = Range(rg, .Cells(.Rows.Count, rg.Column).End(xlUp)) 'All the data in that column
    Set rg = rg.Resize(, 3) 'All the data in columns A:C
End With
rg.Copy
'Sheets("Sheet1").Select
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Edward PamiasTeam Lead RRS DeskAuthor Commented:
This code worked perfectly. The previous post gave me errors. Thanks to both for the assistance. I will have another question soon. Similar to this one.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.