VBA PasteSpecial - cant paste values from columns

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
Hi good people of mrexcel board :)

Trying to go through all the sheets select colums D and E and paste their value to another sheet.
Initially i wanted to do it without activating the sheets since I have 90+ sheets but so far I cant even make it work with this code.
The problem is with lr and paste values
can someone help me to fix it?
VBA Code:
Private Sub CommandButton1_Click()
Dim lr As Long
Dim lr2 As Long
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If InStr(wsSheet.Name, "Invoices") Then
        lr = Sheets("SUMMARY MENU").Cells(Rows.Count, "D").End(xlUp).Row
        wsSheet.Activate
ActiveSheet.Range("D:D").Select
        Selection.Copy
        Sheets("SUMMARY MENU").Cells(lr + 1, "D").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Range("E:E").Select
        Selection.Copy
        Sheets("SUMMARY MENU").Cells(lr2 + 1, "E").PasteSpecial Paste:=xlPasteValues
        End If
Next wsSheet

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi good people of mrexcel board :)

Trying to go through all the sheets select colums D and E and paste their value to another sheet.
Initially i wanted to do it without activating the sheets since I have 90+ sheets but so far I cant even make it work with this code.
The problem is with lr and paste values
can someone help me to fix it?
VBA Code:
Private Sub CommandButton1_Click()
Dim lr As Long
Dim lr2 As Long
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If InStr(wsSheet.Name, "Invoices") Then
        lr = Sheets("SUMMARY MENU").Cells(Rows.Count, "D").End(xlUp).Row
        wsSheet.Activate
ActiveSheet.Range("D:D").Select
        Selection.Copy
        Sheets("SUMMARY MENU").Cells(lr + 1, "D").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Range("E:E").Select
        Selection.Copy
        Sheets("SUMMARY MENU").Cells(lr2 + 1, "E").PasteSpecial Paste:=xlPasteValues
        End If
Next wsSheet

End Sub
Made some changes to the code.
VBA Code:
Private Sub CommandButton1_Click()
Dim lr As Long
Dim lr2 As Long
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
        If InStr(wsSheet.Name, "Faktury") Then
        lr = Sheets("PANEL PODLICZEŃ").Cells(Rows.Count, "D").End(xlUp).Row
        lr2 = Sheets("PANEL PODLICZEŃ").Cells(Rows.Count, "E").End(xlUp).Row
        wsSheet.Activate
ActiveSheet.Range("D:D").Select
        Selection.Copy
        Sheets("PANEL PODLICZEŃ").Cells(lr + 1, "D").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Range("E:E").Select
        Selection.Copy
        Sheets("PANEL PODLICZEŃ").Cells(lr2 + 1, "E").PasteSpecial Paste:=xlPasteValues
        End If
Next wsSheet

End Sub

Also here's the error :
1642415136389.png

1642415109864.png
 
Upvote 0
VBA Code:
ActiveSheet.Range("D:D").Select
name activesheet??
 
Upvote 0
VBA Code:
ActiveSheet.Range("D:D").Select
name activesheet??
wsSheet holds the name which would be any sheet that has "Faktury" in it's name
after that I activate wsSheet, therefore activesheet is wsSheet by default
 
Upvote 0
wsSheet holds the name which would be any sheet that has "Faktury" in it's name
after that I activate wsSheet, therefore activesheet is wsSheet by default
the range ("D: D") is 1000000 rows. When pasting into .cells( lr+1) there will be a missing row leading to an error
 
Upvote 0
the range ("D: D") is 1000000 rows. When pasting into .cells( lr+1) there will be a missing row leading to an error
So what would be the best way to copy columns D and E from each sheet with "Faktury" in it's name to a specific sheet.
Originally i wanted to do excel sumsifs formula but it's really hard to do for so many sheets.
So i decided to copy all these values to one sheet and then sumifs for them
I need formula like this: =SUMIFS(E:E;D:D;">="&M5;D:D;"<="&M6)
That extends to all sheets
 
Upvote 0
So what would be the best way to copy columns D and E from each sheet with "Faktury" in it's name to a specific sheet.
Originally i wanted to do excel sumsifs formula but it's really hard to do for so many sheets.
So i decided to copy all these values to one sheet and then sumifs for them
I need formula like this: =SUMIFS(E:E;D:D;">="&M5;D:D;"<="&M6)
That extends to all sheets
Can you attach the file to google drive and pull the link here?
 
Upvote 0
let me know when u get it so I can delete it form here, it's not a file that should be shared publicly
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,842
Members
449,193
Latest member
MikeVol

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