+ Reply to Thread
Results 1 to 7 of 7

VBA Copy Paste Transpose Loop (Multiple Sheets)

Hybrid View

demon8991 VBA Copy Paste Transpose Loop... 10-29-2013, 08:33 PM
JBeaucaire Re: VBA Copy Paste Transpose... 10-29-2013, 08:50 PM
demon8991 Re: VBA Copy Paste Transpose... 10-29-2013, 09:02 PM
JBeaucaire Re: VBA Copy Paste Transpose... 10-29-2013, 09:05 PM
demon8991 Re: VBA Copy Paste Transpose... 10-29-2013, 09:36 PM
JBeaucaire Re: VBA Copy Paste Transpose... 10-30-2013, 10:17 AM
demon8991 Re: VBA Copy Paste Transpose... 11-03-2013, 11:50 PM
  1. #1
    Registered User
    Join Date
    02-26-2009
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    64

    VBA Copy Paste Transpose Loop (Multiple Sheets)

    Hello All,

    I have annotated the below code as I believe it might be a better explanation as to what I would like my file to do.

    Sub Macro1()
    
    Dim array_sheets As Variant
    
    'The sheets that I need the information copied from
    
    array_sheets = Array("ABS", "SCS", "CUS", "FIN", "H&R", "NET", "CEO", "ROP", "S&E")
    
        
    'This needs to loop through the above array
    
        Sheets("ABS").Select
        
    'This range will always start from cells A2 and B2 (then A3 and B3 etc)
    'but will vary in length and need it to stop when it reaches the last
    'values in column A as column B doesnt always have data
        
        Range("A2:B2").Select
        Application.CutCopyMode = False
        Selection.Copy
        
    'This is the destination cell that i need the range pasted and transposed to this will never change
        
        Sheets("Measure Overview").Select
        Range("S2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
     
    'Each time the above copy paste transpose is done i need the below PDF code to run
     
        Dim pack_name As String
        Dim pack_array As Variant
        Dim file_prefix As String
        Dim file_suffix As String
        
        file_prefix = (ThisWorkbook.Path & "\")
        file_suffix = (" " & Format(Now, "mm-yyyy") & ".pdf")
        
        pack_name = Sheets("Main").Range("C5").Value
        pack_array = report_pages
       
        
        Sheets(pack_array).Select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=file_prefix & pack_name & file_suffix, _
                                        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        
        Sheets("Main").Select
     
    'In total this macro should produce roughly 150 PDF's
     
    End Sub
    Thanks in advance.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Copy Paste Transpose Loop (Multiple Sheets)

    Like so, but I get an error still because "report_pages" is not a filled variable, I assume you have the done another way:

    Sub Macro1()
    Dim shARR As Variant, i As Long, LR As Long
    Dim pack_array As Variant, pack_name As String, file_prefix As String, file_suffix As String
    
    'The sheets that I need the information copied from
        shARR = Array("ABS", "SCS", "CUS", "FIN", "H&R", "NET", "CEO", "ROP", "S&E")
    
    'This needs to loop through the above array
        For i = LBound(shARR) To UBound(shARR)
            With Sheets(shARR(i))
    
    'get last row of data by looking UP column A
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
    
    'Copy the data
            .Range("A2:B" & LR).Copy
    
    'This is the destination cell that i need the range pasted and transposed to this will never change
    
            Sheets("Measure Overview").Range("S2").PasteSpecial xlPasteValues, Transpose:=True
    
    'Each time the above copy paste transpose is done i need the below PDF code to run
    
            file_prefix = (ThisWorkbook.Path & "\")
            file_suffix = (" " & Format(Now, "mm-yyyy") & ".pdf")
    
            pack_name = Sheets("Main").Range("C5").Value
            pack_array = report_pages
    
            Sheets(pack_array).Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=file_prefix & pack_name & file_suffix, _
                                            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        Next i
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-26-2009
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    64

    Re: VBA Copy Paste Transpose Loop (Multiple Sheets)

    Thanks for your help, I get this error message when I run the code.

    Image 1.png

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Copy Paste Transpose Loop (Multiple Sheets)

    Apologies, forgot to close the WITH section:
    Option Explicit
    
    Sub Macro1()
    Dim shARR As Variant, i As Long, LR As Long
    Dim pack_array As Variant, pack_name As String, file_prefix As String, file_suffix As String, report_pages As String
    
        shARR = Array("ABS", "SCS", "CUS", "FIN", "H&R", "NET", "CEO", "ROP", "S&E")
        For i = LBound(shARR) To UBound(shARR)
            With Sheets(shARR(i))
                LR = .Range("A" & .Rows.Count).End(xlUp).Row
                .Range("A2:B" & LR).Copy
                Sheets("Measure Overview").Range("S2").PasteSpecial xlPasteValues, Transpose:=True
    
                file_prefix = (ThisWorkbook.Path & "\")
                file_suffix = (" " & Format(Now, "mm-yyyy") & ".pdf")
    
                pack_name = Sheets("Main").Range("C5").Value
                pack_array = report_pages
    
                Sheets(pack_array).Select
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=file_prefix & pack_name & file_suffix, _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            End With
        Next i
    
    End Sub

  5. #5
    Registered User
    Join Date
    02-26-2009
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    64

    Re: VBA Copy Paste Transpose Loop (Multiple Sheets)

    Ahhh ok almost.

    A quick explanation as to how my file works, the copy paste tranpose part of the macro is basically collecting 1 or 2 ID numbers and dropping them into the display sheet (Measure Overview). These two numbers drive the information that appears on this sheet At this point I need the macro to PDF the Measure Overiview page before it goes to the next copy and paste loop.

    The copy paste transpose part of the macro has to be pasted into cell S2 (Measure Overview) each time. I think VBA above is copying the data into S2 then T2 then U2 etc and then PDFing which is not quite what I want.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Copy Paste Transpose Loop (Multiple Sheets)

    Quote Originally Posted by demon8991 View Post
    I think VBA above is copying the data into S2 then T2 then U2 etc and then PDFing which is not quite what I want.
    What makes you think that? When tested, did it do that? Your paste command is targeted at S2 only.

    Your paste command does include "Transpose=True" which may not be what you really want, but it was there originally, I simply left it there.
    Last edited by JBeaucaire; 10-30-2013 at 10:22 AM.

  7. #7
    Registered User
    Join Date
    02-26-2009
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    64

    Re: VBA Copy Paste Transpose Loop (Multiple Sheets)

    Sorry for the late reply I have now solved this issue and added some more features with the below code.

    Thanks for your help, much appreciated.

    Sub Macro3()
    
        Dim shARR As Variant
        Dim i As Long
        Dim j As Long
        Dim LR As Long
        Dim pack_name As String
        Dim file_prefix As String
        Dim file_suffix As String
        Dim error_measures As String
        
        error_measures = ""
        
        Application.ScreenUpdating = False
        
            shARR = Array("ABS", "CCS", "CUS", "FIN", "HR", "NET", "CEO", "ROP", "S&E")
            
            For i = LBound(shARR) To UBound(shARR)                              ' for each sheet
            
                LR = Sheets(shARR(i)).Range("A" & Sheets(shARR(i)).Rows.Count).End(xlUp).Row    ' determine number of measures to loop through
            
                For j = 2 To LR                                                 ' for each measure
                
                    Application.StatusBar = "Printing " & Sheets(shARR(i)).Name & ": " & Sheets("Measure Overview").Range("T2").Text
                    
                    Sheets(shARR(i)).Range("A" & j & ":B" & j).Copy             ' copy the data
                    
                    Sheets("Measure Overview").Range("S2").PasteSpecial xlPasteValues, Transpose:=True  'paste
                    
                    file_prefix = (ThisWorkbook.Path & "\" & shARR(i) & "\" & (j - 1) & " ")
    '                file_prefix = (ThisWorkbook.Path & "\")
                    file_suffix = (" " & Format(Now, "mm-yyyy") & ".pdf")
        
                    pack_name = Left(Sheets("Main").Range("C5").Text, 105)
        
                    Sheets("Measure Overview").ExportAsFixedFormat Type:=xlTypePDF, Filename:=file_prefix & pack_name & file_suffix, _
                        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False    ' pdf
                        
                    On Error GoTo ErrorHandler
                    
                Next j
                
            Next i
            
        Application.ScreenUpdating = True
        
        Application.StatusBar = False
        
        If error_measures <> "" Then MsgBox ("The following measures were not printed: " & vbCrLf & error_measures)
        
    ErrorHandler:
        
            error_measures = error_measures & Sheets("Measure Overview").Range("T2").Text & vbCrLf
            
            Resume Next
    
    End Sub

+ 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. Replies: 5
    Last Post: 05-25-2013, 07:12 AM
  2. [SOLVED] vba copy and paste based on multiple loop criteria
    By sarahcpa in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-19-2013, 10:29 AM
  3. find copy paste loop multiple results
    By MTB1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2012, 10:53 AM
  4. [SOLVED] Loop, Find, Copy, and Paste between multiple worksheets
    By bg_enigma1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-07-2012, 06:40 PM
  5. Loop to Copy and Paste and Transpose
    By o4008 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-08-2010, 02:11 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