Avatar of Edward Pamias
Edward PamiasFlag for United States of America

asked on 

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
Microsoft Excel

Avatar of undefined
Last Comment
Edward Pamias
Avatar of SixSigmaGuy
SixSigmaGuy
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of byundt
byundt
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Edward Pamias
Edward Pamias
Flag of United States of America image

ASKER

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.
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo