+ Reply to Thread
Results 1 to 13 of 13

Copy/Paste Multiple Dynamic Ranges below each other

Hybrid View

sudric Copy/Paste Multiple Dynamic... 01-28-2015, 02:15 AM
:) Sixthsense :) Re: Copy/Paste Multiple... 01-28-2015, 02:32 AM
sudric Re: Copy/Paste Multiple... 01-28-2015, 06:12 AM
:) Sixthsense :) Re: Copy/Paste Multiple... 01-28-2015, 06:21 AM
sudric Re: Copy/Paste Multiple... 01-28-2015, 06:31 AM
:) Sixthsense :) Re: Copy/Paste Multiple... 01-28-2015, 06:34 AM
sudric Re: Copy/Paste Multiple... 01-28-2015, 06:41 AM
:) Sixthsense :) Re: Copy/Paste Multiple... 01-28-2015, 07:01 AM
sudric Re: Copy/Paste Multiple... 01-28-2015, 07:11 AM
:) Sixthsense :) Re: Copy/Paste Multiple... 01-28-2015, 07:17 AM
sudric Re: Copy/Paste Multiple... 01-28-2015, 07:29 AM
:) Sixthsense :) Re: Copy/Paste Multiple... 01-28-2015, 07:37 AM
sudric Re: Copy/Paste Multiple... 01-28-2015, 10:13 AM
  1. #1
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Copy/Paste Multiple Dynamic Ranges below each other

    Hello All,

    Could use some help with a macro. I have a workbook with 30+ worksheets. Each worksheet has a range of values, the columns are always the same but the number of rows are different for each sheet - hence dynamic range. What I need is a macro something like that:

    - Go to Sheet 1
    - Copy Range in Sheet 1 (currSheet)
    - Paste in Sheet named "Query"
    - Go to Sheet 2
    - Copy Range in Sheet 2 (currSheet)
    - Paste in Sheet "Query" BELOW the previous range that was pasted
    ---Rinse & Repeat for all worksheets in workbook----

    So basically I would be making a list of the ranges in a sense (if that makes sense)

    I get a "Subscript Out of Range" error at the copy/paste code line:

    Worksheets(currSheet).Range("F2:H" & LastRow).Copy Destination:=Worksheets("Query").Cells(Rows.Count, "A").End(xlUp).Offset(1)
    Prior to this line I have some code that loops through and does some formatting stuff on all sheets

    Any advice/help would be much appreciated.

    Can't seem to attach the workbook...is there a size limit on uploads?

    Cheers.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    What is the value of LastRow and currSheet??


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Here is the full code. Let me know if you need the workbook.:

    Option Explicit
    Sub datechange()
    
    Dim currSheet As Variant
    Dim allSheets() As Variant
    Dim LastRow As Integer
    Dim i As Integer
    Dim k As Integer
    Dim fmt As String
    Dim var As String
    Dim well As String
    
    Application.ScreenUpdating = False
    
    allSheets = Sheets("Tags").Range("M3:M33").Value
        For Each currSheet In allSheets
            LastRow = Worksheets(currSheet).Cells(Rows.Count, 2).End(xlUp).Row
            Worksheets(currSheet).Select
                For i = 2 To LastRow Step 1
                 If Cells(i, 3) = "Null" Then
                    Cells(i, 3) = -9999.97
                End If
                    fmt = "0."
                    
                    For k = 1 To Len(Cells(i, 3)) - 2
                        fmt = fmt & "0"
                    Next
                    
                    If Len(Cells(i, 3)) = 1 Then fmt = "0"
                        If i = LastRow Then
                            Cells(i, 6) = "UPDATE Table_Name SET [" & Cells(1, 6) & "]='" & Format(Cells(i, 3), fmt) & "' Where TimeStamp >= '" & Format(Cells(i, 2), "mm/dd/yyyy hh:mm:ss") & "'  and TimeStamp < '01/23/2015 07:30:00'"
                        Else
                            Cells(i, 6) = "UPDATE Table_Name SET [" & Cells(1, 6) & "]='" & Format(Cells(i, 3), fmt) & "' Where TimeStamp >= '" & Format(Cells(i, 2), "mm/dd/yyyy hh:mm:ss") & "'  and TimeStamp < '" & Format(Cells(i + 1, 2), "mm/dd/yyyy hh:mm:ss") & "'"
                    End If
                    
                    If InStr(Cells(1, 7), "Manifold") Then
                        well = "M01-M07"
                            If InStr(Cells(1, 7), "DPMeas") Then
                                    var = "Meas DP"
                                ElseIf InStr(Cells(1, 7), "OutletPressureMeas") Then var = "Outlet Press Meas"
                                ElseIf InStr(Cells(1, 7), "OutletTemperatureMeas") Then var = "Outlet Temp Meas"
                                Else
                                    var = "Gas Volume Rate Meas"
                            End If
                    ElseIf InStr(Cells(1, 7), "AM-C1DToAM-A1D") Then
                        well = "C1D to A1D"
                            If Right(Cells(1, 7), 1) = 1 Then
                                var = "Valve Position1"
                            Else
                                var = "Valve Position0"
                            End If
                    Else
                        well = "Well " & Mid(Cells(1, 7), 25, 3)
                            If InStr(Cells(1, 3), "_P") Then
                                var = "PBC"
                            ElseIf InStr(Cells(1, 3), "_T") Then var = "TBC"
                            ElseIf InStr(Cells(1, 3), "_H") Then var = "Choke"
                            Else
                                var = "Wing Valve"
                            End If
                    End If
                        
                    Cells(i, 8) = "all"
                    Cells(i, 7) = "Correcting " & well & " " & var & " to " & Format(Cells(i, 3), "0.000") & ""
                Next i
            Worksheets(currSheet).Range("F2:H" & LastRow).Copy Destination:=Worksheets("Query").Cells(Rows.Count, "A").End(xlUp).Offset(1)
        Next currSheet
    
    Application.ScreenUpdating = True
    
    End Sub

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    The full code seems to be ok... but I need a sample file to fix the issue

  5. #5
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Quote Originally Posted by :) Sixthsense :) View Post
    The full code seems to be ok... but I need a sample file to fix the issue
    Is there a cap on the upload file size? The workbook is 2.8MB but won't let me upload...

  6. #6
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Yes, excel file is limited to 1 MB but Zip have 10 MB capacity so add it in zip and attach it

  7. #7
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Genius! Here it is
    Attached Files Attached Files

  8. #8
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Worksheets(currSheet).Range("F2:H" & LastRow).Copy Destination:=Worksheets("Query").Cells(Rows.Count, "A").End(xlUp).Offset(1)
    I found the root cause of the error

    Sheet Named Query is not available in your workbook

  9. #9
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Quote Originally Posted by :) Sixthsense :) View Post
    I found the root cause of the error

    Sheet Named Query is not available in your workbook
    Wow.....I feel retarded...I thought I renamed an empty sheet to that! Jeez. Thanks for the help! sorry for being dumb

    On a similar note, any idea how easy/hard it would be to copy/paste these ranges to a NEW workbook instead of a new sheet in current workbook? I.e. Code to create a new workbook, paste these ranges.
    Last edited by sudric; 01-28-2015 at 07:16 AM.

  10. #10
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Check whether this helps you..

    Dim wNew As Workbook, wCurr As Workbook
    
    'Setting current code workbook in a variable
    Set wCurr = ThisWorkbook
    
    'The below line will create a new workbook and set that workbook in wNew Variable
    Set wNew = Workbooks.Add
    
    'Activating Source Workbook
    wCurr.Activate
    
    'Activating Newly Creating workbook
    wNew.Activate
    
    wCurr.Worksheets(currSheet).Range("F2:H" & LastRow).Copy _
        Destination:=wNew.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)

  11. #11
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Where would you Set the current and new workbook? If i put these outside the first For loop, I get an subscript out of range error at the allSheets line, shown below:

    Set wCurr = ThisWorkbook
    Set wNew = Workbooks.Add
    allSheets = Sheets("Tags").Range("M3:M33").Value
        For Each currSheet In allSheets
    And I've activated the current and new sheet within the first for loop:

                Next i
            wCurr.Activate
            wNew.Activate
            wCurr.Worksheets(currSheet).Range("F2:H" & LastRow).Copy _
            Destination:=wNew.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)
    '        Worksheets(currSheet).Range("F2:H" & LastRow).Copy Destination:=Worksheets("Query").Cells(Rows.Count, "A").End(xlUp).Offset(1)
        Next currSheet

  12. #12
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Check this...

    Option Explicit
    Sub datechange()
    
    Dim currSheet As Variant
    Dim allSheets() As Variant
    Dim LastRow As Integer
    Dim i As Integer
    Dim k As Integer
    Dim fmt As String
    Dim var As String
    Dim well As String
    Dim wSource As Workbook, wNew As Workbook
    'Dim startRow As Long
    
    Set wSource = ThisWorkbook
    Set wNew = Workbooks.Add
    wSource.Activate
    
    Application.ScreenUpdating = False
    
    allSheets = Sheets("Tags").Range("M3:M33").Value
        For Each currSheet In allSheets
            LastRow = Worksheets(currSheet).Cells(Rows.Count, 2).End(xlUp).Row
            Worksheets(currSheet).Select
                For i = 2 To LastRow Step 1
                 If Cells(i, 3) = "Null" Then
                    Cells(i, 3) = -9999.97
                End If
                    fmt = "0."
                    
                    For k = 1 To Len(Cells(i, 3)) - 2
                        fmt = fmt & "0"
                    Next
                    
                    If Len(Cells(i, 3)) = 1 Then fmt = "0"
                        If i = LastRow Then
                            Cells(i, 6) = "UPDATE Table_Name SET [" & Cells(1, 6) & "]='" & Format(Cells(i, 3), fmt) & "' Where TimeStamp >= '" & Format(Cells(i, 2), "mm/dd/yyyy hh:mm:ss") & "'  and TimeStamp < '01/23/2015 07:30:00'"
                        Else
                            Cells(i, 6) = "UPDATE Table_Name SET [" & Cells(1, 6) & "]='" & Format(Cells(i, 3), fmt) & "' Where TimeStamp >= '" & Format(Cells(i, 2), "mm/dd/yyyy hh:mm:ss") & "'  and TimeStamp < '" & Format(Cells(i + 1, 2), "mm/dd/yyyy hh:mm:ss") & "'"
                    End If
                    
                    If InStr(Cells(1, 7), "Manifold") Then
                        well = "M01-M07"
                            If InStr(Cells(1, 7), "DPMeas") Then
                                    var = "Meas DP"
                                ElseIf InStr(Cells(1, 7), "OutletPressureMeas") Then var = "Outlet Press Meas"
                                ElseIf InStr(Cells(1, 7), "OutletTemperatureMeas") Then var = "Outlet Temp Meas"
                                Else
                                    var = "Gas Volume Rate Meas"
                            End If
                    ElseIf InStr(Cells(1, 7), "AM-C1DToAM-A1D") Then
                        well = "C1D to A1D"
                            If Right(Cells(1, 7), 1) = 1 Then
                                var = "Valve Position1"
                            Else
                                var = "Valve Position0"
                            End If
                    Else
                        well = "Well " & Mid(Cells(1, 7), 25, 3)
                            If InStr(Cells(1, 3), "_P") Then
                                var = "PBC"
                            ElseIf InStr(Cells(1, 3), "_T") Then var = "TBC"
                            ElseIf InStr(Cells(1, 3), "_H") Then var = "Choke"
                            Else
                                var = "Wing Valve"
                            End If
                    End If
                    Cells(i, 5).ClearContents
                    Cells(i, 8) = "all"
                    Cells(i, 7) = "Correcting " & well & " " & var & " to " & Format(Cells(i, 3), "0.000") & ""
                Next i
            Worksheets(currSheet).Range("F2:H" & LastRow).Copy Destination:=wNew.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)
        Next currSheet
    
    wNew.Activate
    
    Application.ScreenUpdating = True
    
    End Sub

  13. #13
    Registered User
    Join Date
    11-04-2014
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    29

    Re: Copy/Paste Multiple Dynamic Ranges below each other

    Beautiful! Works greats.

    Thanks a lot man

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Record Macro to Copy / Paste Dynamic Ranges
    By Steve N. in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-12-2013, 08:36 PM
  2. Replies: 0
    Last Post: 08-05-2013, 11:34 AM
  3. Copy and paste dynamic ranges + transforming into a table
    By tigerallied in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2013, 01:26 PM
  4. Copy and paste based on found cells using two spreadsheets with dynamic ranges
    By shawnsonline in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2012, 02:49 PM
  5. [SOLVED] copy dynamic ranges in multiple workbooks based on a data in 1 col
    By jbsand1001 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-18-2005, 03:06 PM

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