简体   繁体   中英

Copy/Paste Macro Pasting Randomly

I am having an issue copying and pasting a drop-down data validation menu from one sheet into all selected sheets. The drop-down menu seems to paste randomly instead of pasting into sheet "B22" of the selected sheets.

Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
Selection.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = False
End Sub

Any suggestions on how to tackle this? I am having some difficulty finding the error in my code.

In case you want to work with the workbook and avoid using copy/paste..

    Option Explicit
    Sub Test()
       Dim excel_sheet As Worksheet
       Dim sht As Worksheet
       Dim drop_down_value As Range

       Set sht = ThisWorkbook.Sheets("Sheet2")
       Set drop_down_value = sht.Range("B22")

      'Sheets selection should be done before running macro

      For Each excel_sheet In ThisWorkbook.Windows(1).SelectedSheets
          excel_sheet.Range("B22").Value = drop_down_value.Value
      Next
    End Sub

Try the following, you need to loop through all selected Sheets instead of using Selection.Range:

Sub TEST()
    Dim sht As Worksheet
    Sheets("Sheet2").Range("B22").Copy
    'Sheets selection should be done before running macro
    For Each sht In ActiveWindow.SelectedSheets
        sht.Range("B22").PasteSpecial xlPasteValidation
        Application.CutCopyMode = True
    Next
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM