VBA copy and paste

londa_vba

Board Regular
Joined
May 11, 2023
Messages
61
Office Version
  1. 365
Platform
  1. Windows
Hello
I need to copy from three different excel sheets ("G", "R", and "Y") from the same cell ranges (B10:B33,B39:B64,B70:B95,B101:B126) on all three sheets and paste onto sheet "C" starting with cell C5. I would like the code to remove the blank cells (all cells have formulas in them so by blank I guess I mean no value from formula results) and I would like the pasted list to be continuous (wherever G finishes R begins right beneath it, etc.) I have tried several available codes online but I do not understand vba enough to adjust them to my specific needs. Thank you
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What version of Excel are you using?

Please update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using, as the best solution often varies by version. Don’t forget to scroll down to save your changes.

Doug
 
Upvote 0
Would you be able to update your profile with the version of Excel you are using please ?

That way people know what kind of armoury they can use when looking at solutions for you
 
Upvote 0
Hello
I need to copy from three different excel sheets ("G", "R", and "Y") from the same cell ranges (B10:B33,B39:B64,B70:B95,B101:B126) on all three sheets and paste onto sheet "C" starting with cell C5. I would like the code to remove the blank cells (all cells have formulas in them so by blank I guess I mean no value from formula results) and I would like the pasted list to be continuous (wherever G finishes R begins right beneath it, etc.) I have tried several available codes online but I do not understand vba enough to adjust them to my specific needs. Thank you
Does this work?

Try it on a copy of your data.

VBA Code:
Private Sub VBAcopyandpaste()
Dim arrRanges() As String
Dim arrWorksheets() As String
Dim Ws As Worksheet
Dim i As Integer
Dim ii As Integer
Dim intRow As String
Dim rng As Range

    ActiveWorkbook.Save
    
    Worksheets("C").Activate
    
    With Worksheets("C")
        .Range("C5:C" & .Cells(.Rows.Count, "C").End(xlUp).Row).Value = ""
    End With

    arrRanges = Split("B10:B33,B39:B64,B70:B95,B101:B126", ",")
        
    arrWorksheets = Split("G,R,Y", ",")
    
    intRow = 5
    
    For i = LBound(arrWorksheets) To UBound(arrWorksheets)
        
        For ii = LBound(arrRanges) To UBound(arrRanges)
            
            For Each rng In Worksheets(arrWorksheets(i)).Range(arrRanges(ii))
                If Len(Trim(rng.Value)) > 0 Then
                    Worksheets("C").Cells(intRow, 3).Value = rng.Value
                    intRow = intRow + 1
                End If
            Next rng
            
        Next ii
        
    Next i
    
    Worksheets("C").Range("C5").Select
    
    MsgBox intRow - 5 & " rows of data copied", vbOKOnly, "Confirmation"
            
End Sub
 
Upvote 1
Does this work?

Try it on a copy of your data.

VBA Code:
Private Sub VBAcopyandpaste()
Dim arrRanges() As String
Dim arrWorksheets() As String
Dim Ws As Worksheet
Dim i As Integer
Dim ii As Integer
Dim intRow As String
Dim rng As Range

    ActiveWorkbook.Save
   
    Worksheets("C").Activate
   
    With Worksheets("C")
        .Range("C5:C" & .Cells(.Rows.Count, "C").End(xlUp).Row).Value = ""
    End With

    arrRanges = Split("B10:B33,B39:B64,B70:B95,B101:B126", ",")
       
    arrWorksheets = Split("G,R,Y", ",")
   
    intRow = 5
   
    For i = LBound(arrWorksheets) To UBound(arrWorksheets)
       
        For ii = LBound(arrRanges) To UBound(arrRanges)
           
            For Each rng In Worksheets(arrWorksheets(i)).Range(arrRanges(ii))
                If Len(Trim(rng.Value)) > 0 Then
                    Worksheets("C").Cells(intRow, 3).Value = rng.Value
                    intRow = intRow + 1
                End If
            Next rng
           
        Next ii
       
    Next i
   
    Worksheets("C").Range("C5").Select
   
    MsgBox intRow - 5 & " rows of data copied", vbOKOnly, "Confirmation"
           
End Sub
THIS WORKS PERFECTLY!!! genius. thank you
 
Upvote 0
What version of Excel are you using?

Please update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using, as the best solution often varies by version. Don’t forget to scroll down to save your changes.

Doug
Hi Doug, would this be version 2303? I did not see that as an option under account details.

1696012162422.png
 
Upvote 0
That's the build version, you need the product version, such as 365, 2021 etc
 
Upvote 0
If you click the "About Excel" button it should tell you the product version near the top of the popup window. It will be something like 365, 2021, 2019, 2016, etc.

Doug
 
Upvote 0
Does this work?

Try it on a copy of your data.

VBA Code:
Private Sub VBAcopyandpaste()
Dim arrRanges() As String
Dim arrWorksheets() As String
Dim Ws As Worksheet
Dim i As Integer
Dim ii As Integer
Dim intRow As String
Dim rng As Range

    ActiveWorkbook.Save
   
    Worksheets("C").Activate
   
    With Worksheets("C")
        .Range("C5:C" & .Cells(.Rows.Count, "C").End(xlUp).Row).Value = ""
    End With

    arrRanges = Split("B10:B33,B39:B64,B70:B95,B101:B126", ",")
       
    arrWorksheets = Split("G,R,Y", ",")
   
    intRow = 5
   
    For i = LBound(arrWorksheets) To UBound(arrWorksheets)
       
        For ii = LBound(arrRanges) To UBound(arrRanges)
           
            For Each rng In Worksheets(arrWorksheets(i)).Range(arrRanges(ii))
                If Len(Trim(rng.Value)) > 0 Then
                    Worksheets("C").Cells(intRow, 3).Value = rng.Value
                    intRow = intRow + 1
                End If
            Next rng
           
        Next ii
       
    Next i
   
    Worksheets("C").Range("C5").Select
   
    MsgBox intRow - 5 & " rows of data copied", vbOKOnly, "Confirmation"
           
End Sub
Sorry to bother you again but I needed to move the paste site on the code solution you gave me and now it is pasting where I would like AA3 on Sheet C but I am getting an error message regarding line 29 type mismatch

VBA Code:
Sub PopulateCEPrep()
Dim arrRanges() As String
Dim arrWorksheets() As String
Dim Ws As Worksheet
Dim i As Integer
Dim ii As Integer
Dim intRow As String
Dim rng As Range

    ActiveWorkbook.Save
    
    Worksheets("Sheet C").Activate
    
    With Worksheets("Sheet C")
        .Range("AA3:AA" & .Cells(.Rows.Count, "AA").End(xlUp).Row).Value = ""
    End With

    arrRanges = Split("B8:B33,B39:B64,B70:B95,B101:B126", ",")
        
    arrWorksheets = Split("G,R,Y", ",")
    
    intRow = 3
    
    For i = LBound(arrWorksheets) To UBound(arrWorksheets)
        
        For ii = LBound(arrRanges) To UBound(arrRanges)
            
            For Each rng In Worksheets(arrWorksheets(i)).Range(arrRanges(ii))
                If Len(Trim(rng.Value)) > 0 Then
                    Worksheets("Sheet C").Cells(intRow, 27).Value = rng.Value
                    intRow = intRow + 1
                End If
            Next rng
            
        Next ii
        
    Next i
    
    Worksheets("Sheet C").Range("AA3").Select
    
    'MsgBox intRow - 5 & " rows of data copied", vbOKOnly, "Confirmation"
 
Upvote 0
... Coming in from duplicate post here: mismatch error

What is the exact error message you are getting? And on which line of code?

If you have an error value anywhere in the ranges "B8:B33,B39:B64,B70:B95,B101:B126" in worksheets "G", "R" or "Y", you will get a Run-time error '13': Type mismatch error when you test the cell value on this line of code:

If Len(Trim(rng.Value)) > 0 Then
 
Upvote 1
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top