+ Reply to Thread
Results 1 to 14 of 14

Need code to copy and paste based on variable rows

Hybrid View

  1. #1
    Registered User
    Join Date
    04-30-2013
    Location
    NJ
    MS-Off Ver
    Excel 2007
    Posts
    4

    Smile Need code to copy and paste based on variable rows

    Hello
    I am writing a macro to combine a few different different spreadsheet.
    Out team individiually type out a list of dailt deliverables.
    Every morning, someone manually goes in an grabs all the rows in everyone's files and combines then in a master file.
    The amount of rows changes day to day based on work load.
    But I am not sure the code to use to select only the rows that have data in them.
    Can anyone help with that code??
    Thanks
    Bonnie

  2. #2
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: Need code to copy and paste based on variable rows

    Please attach a sample Workbook so that we can understand the data you are talking about!
    Cheers!
    Deep Dave

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Need code to copy and paste based on variable rows

    Hi, Bonnie ,

    maybe like this:
    Dim lngLast As Long
    
    With ActiveSheet
      lngLast = .Cells(Rows.Count, "A").End(xlUp).Row
    End With
    
    MsgBox "Range to copy: " & Range("A2:E" & lngLast).Address
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    04-30-2013
    Location
    NJ
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need code to copy and paste based on variable rows

    Hi Holger, this is my code, can you let me know where I should put the above code in?
    Also, do you know what code I would use to add a subject into the email listed in the code??
    Thank you so much!!

    Sub Test1()
    '
    ' Test1 Macro
    
    ChDir "C:\Users\bonnie.\Desktop\test\Test Files"
        Workbooks.Open Filename:= _
            "C:\Users\bonnie.\Desktop\test\Test Files\Master Deliverables Spreadsheet __.xlsx"
        Application.DisplayAlerts = False
        Range("D3").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Workbooks.Open Filename:= _
            "C:\Users\bonnie.\Desktop\test\Test Files\Mandy 4.30.13.xlsx"
        Rows("1:12").Select
        Selection.Copy
        Windows("Master Deliverables Spreadsheet __.xlsx").Activate
        Range("A6").Select
        ActiveSheet.Paste
        Windows("Mandy 4.30.13.xlsx").Activate
        Application.DisplayAlerts = False
        ActiveWindow.Close
        Workbooks.Open Filename:= _
            "C:\Users\bonnie.\Desktop\test\Test Files\Rob 4.30.13.xlsx"
        Rows("1:12").Select
        Selection.Copy
        Windows("Master Deliverables Spreadsheet __.xlsx").Activate
        Range("A6").Select
        ActiveSheet.Paste
        Windows("Rob 4.30.13.xlsx").Activate
        Application.DisplayAlerts = False
        ActiveWindow.Close
            ActiveWorkbook.SaveAs Filename:= _
            "C:\Users\bonnie.\Desktop\test\Test Files\old\Master Deliverables Spreadsheet " & Format(Now(), "mm.dd.yy") & ".xlsx" _
            , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        With ActiveWorkbook
        Select Case Day(Now())
        Case 1, 21, 31
        ordsuffix = "st"
        Case 2, 22
        ordsuffix = "nd"
        Case 3, 23
        ordsuffix = "rd"
        Case Else
        ordsuffix = "th"
        End Select
        .SendMail Recipients:=Array("b@aaaaaaaaaa.com", "b@aol.com"), Subject:="Deliverables Test- " & (MonthName(Month(Now()))) & (Day(Now())) & (ordsuffix)
    
    
        End With
        
    End Sub

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Need code to copy and paste based on variable rows

    Hi, bonnie6850,

    this is hard-coded for the files to open, will all the files be in one folder or shouldn´t be anything within the code to only open the workbnooks for a special day?

    Code is untested as I´m running out of time for a test:
    Sub Test1()
    '
    ' Test1 Macro
    
    Dim wbMaster As Workbook
    Dim wbClone As Workbook
    Dim strPath As String
    Dim lngFree As Long
    Dim lngLast As Long
    
        strPath = CurDir
        ChDir "C:\Users\bonnie.\Desktop\test\Test Files"
        Set wbMaster = Workbooks.Open(Filename:= _
            "C:\Users\bonnie.\Desktop\test\Test Files\Master Deliverables Spreadsheet __.xlsx")
        With wbMaster.ActiveSheet.Range("D3")
          .Value = .Value
        End With
        Set wbClone = Workbooks.Open("C:\Users\bonnie.\Desktop\test\Test Files\Mandy 4.30.13.xlsx")
        lngFree = wbMaster.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
        lngLast = wbClone.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
        wbaster.ActiveSheet.Rows(lngFree).Resize(lngLast).Value = wbClone.ActiveSheet.Rows(1).Resize(lngLast).Value
        wbClone.Close False
        Set wbClone = Workbooks.Open("C:\Users\bonnie.\Desktop\test\Test Files\Rob 4.30.13.xlsx")
        lngFree = wbMaster.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
        lngLast = wbClone.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
        wbaster.ActiveSheet.Rows(lngFree).Resize(lngLast).Value = wbClone.ActiveSheet.Rows(1).Resize(lngLast).Value
        wbClone.Close False
        wbMaster.SaveAs Filename:= _
            "C:\Users\bonnie.\Desktop\test\Test Files\old\Master Deliverables Spreadsheet " & Format(Now(), "mm.dd.yy") & ".xlsx" _
            , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        With wbMaster
          Select Case Day(Now())
            Case 1, 21, 31
              ordsuffix = "st"
            Case 2, 22
              ordsuffix = "nd"
            Case 3, 23
              ordsuffix = "rd"
            Case Else
              ordsuffix = "th"
          End Select
          .SendMail Recipients:=Array("b@aaaaaaaaaa.com", "b@aol.com"), Subject:="Deliverables Test- " & (MonthName(Month(Now()))) & (Day(Now())) & (ordsuffix)
        End With
        Set wbClone = Nothing
        Set wbMaster = Nothing
        
        ChDir strPath
    End Sub
    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    04-30-2013
    Location
    NJ
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need code to copy and paste based on variable rows


  7. #7
    Registered User
    Join Date
    04-30-2013
    Location
    NJ
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need code to copy and paste based on variable rows

    I need something that will go out and open each one and copy and past into master under the last one, so the cells will always ver variable.
    Thanks!

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Need code to copy and paste based on variable rows

    Hi,

    Quote Originally Posted by msexcelathome
    so that we can understand
    We?

    @bonnie6850:
    maybe RDBMerge, Excel Merge Add-in for Excel for Windows may help.

    Ciao,
    Holger

  9. #9
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: Need code to copy and paste based on variable rows

    "We" goes to anyone who might want to respond..

  10. #10
    Registered User
    Join Date
    06-16-2010
    Location
    Dayton, NJ
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need code to copy and paste based on variable rows

    Hi Holger
    This worked perfectly!!
    The files are in all different folders, I think I am going to make everyone use a standard file name, only their name on their current file, so I don't have the build the dates into the file names, it would be a pain with people not updating and weekends. My last question for you is I need to copy over the the Values and Number formats, the master does not have the coloring we use for the file. Please let me know what I can do you copy that over.
    Thanks soooo much!!!

    Bonnie

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Need code to copy and paste based on variable rows

    Hi, Bonnie,

    the following lines may help (they are for one sheet but I´m sure you may adapt them as needed):
    Rows("1:12").Copy
    Rows("41:52").PasteSpecial Paste:=xlPasteFormats
    Although I wouldn´t use Rows here but only color the range as needed.

    Ciao,
    Holger

  12. #12
    Registered User
    Join Date
    06-16-2010
    Location
    Dayton, NJ
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need code to copy and paste based on variable rows

    Here is the source file and the master.
    How would I go about setting in the formatting since the rows change daily in all the different lists?
    Thanks
    Bonnie
    Attached Files Attached Files

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Need code to copy and paste based on variable rows

    Hi, Bonnie,

    for this combination it may look like this (although I´m still not happy with copying whole rows instead of the used range only)
    Dim wbMaster As Workbook
    Dim wbClone As Workbook
    Dim strPath As String
    Dim lngFree As Long
    Dim lngLast As Long
    
    
        strPath = CurDir
        ChDir "C:\HaHoBe\Downloads"
        Set wbMaster = Workbooks.Open(Filename:= _
            "Master Deliverables Spreadsheet 05.03.13.xlsx")
        Set wbClone = Workbooks.Open("Cassandra.xlsx")
        lngFree = wbMaster.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
        lngLast = wbClone.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
        wbMaster.ActiveSheet.Rows(lngFree).Resize(lngLast).Value = wbClone.ActiveSheet.Rows(1).Resize(lngLast).Value
        wbClone.ActiveSheet.Rows(1).Resize(lngLast).Copy
        wbMaster.ActiveSheet.Rows(lngFree).Resize(lngLast).PasteSpecial Paste:=xlPasteFormats
        Application.CutCopyMode = False
        wbClone.Close False
    The indivual workbooks are counted by variable lngLast while lngFree get the first free row in the master workbook.

    Ciao,
    Holger

  14. #14
    Registered User
    Join Date
    06-16-2010
    Location
    Dayton, NJ
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need code to copy and paste based on variable rows

    Hi Holger
    Not sure that will work, I am using your code to copy and paste the different ranges:
    How to I add it into this code?
    
    Dim wbMaster As Workbook
    Dim wbClone As Workbook
    Dim strPath As String
    Dim lngFree As Long
    Dim lngLast As Long
    
    
        strPath = CurDir
        ChDir "C:\Users\bonnie.worthington\Desktop\test\Test Files"
        Set wbMaster = Workbooks.Open(Filename:= _
            "K:\Daily Deliverables Spreadsheet\Master Spreadsheet\Master Deliverables  __DO_NOT_DELETE.xlsx")
        Set wbClone = Workbooks.Open("K:\Daily Deliverables Spreadsheet\Cassandra\2013\Cassandra.xlsx")
        lngFree = wbMaster.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
        lngLast = wbClone.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
        wbMaster.ActiveSheet.Rows(lngFree).Resize(lngLast).Value = wbClone.ActiveSheet.Rows(1).Resize(lngLast).Value
        wbClone.Close False

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1