• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Paste data in selected sheets - Loop through selected sheets

nagovind

Member
Dear All,

Below is the code that paste the data in the selected sheets from clipboard
It is doing the job perfectly

But this is not working if the copied data contains an object instead it is pasting only in fist selected sheet and not in all sheets

Please refer to the attached Worksheet-1 without object, Worksheet-2 with object, please advise the code

Public Sub PASTEDATASELSHEET()
'PASTE DATA IN SELECTED SHEETS
Application.DisplayAlerts = False
Dim wks As Worksheet
For Each wks In ThisWorkbook.Windows(1).SelectedSheets
Range("B2").Select
ActiveSheet.Paste
Next wks
Application.DisplayAlerts = True
End Sub
 

Attachments

  • Worksheet-1 without object.xlsm
    20.5 KB · Views: 8
  • Worksheet-2 with object.xlsm
    99.2 KB · Views: 8
Hi Nagovind,

I am unable to figure out the issue you have. I tried the macro in each sheet and it works.

1) Do you mean grouping the sheets and then run the macro?
2) Do you want to run this macro on all the sheets in the work book? If so, you will have to change the line of code (for loop to the below).
Code:
 For Each wks In ThisWorkbook.Sheets

Try and let me know, if your requirement is different.
 
Hi Arul Govind ,

Add the below line of code after the FOR statement. Activate the sheet before you paste the values. This should work fine.

wks.Activate

Your code should be

Code:
Public Sub PASTEDATASELSHEET()
'PASTE DATA IN SELECTED SHEETS
Application.DisplayAlerts = False
Dim wks As Worksheet
For Each wks In ThisWorkbook.Windows(1).SelectedSheets
wks.Activate
Range("B2").Select
ActiveSheet.Paste
Next wks
Application.DisplayAlerts = True
End Sub
 
Back
Top