随机复制/粘贴宏粘贴

时间:2018-02-01 17:14:29

标签: excel vba excel-vba

我在将下拉数据验证菜单从一张纸复制并粘贴到所有选定的纸张中时遇到问题。下拉菜单似乎是随机粘贴而不是粘贴到所选工作表的“B22”表中。

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

有关如何解决此问题的任何建议?我在代码中找到错误时遇到了一些困难。

2 个答案:

答案 0 :(得分:2)

如果您想使用工作簿并避免使用复制/粘贴..

with d1 as (. . .),
     d2 as (. . .),
     d3 as (. . .)
select d3.*
from d3
union all
select d1.*
from d1
where not exists (select 1 from d3 where d1.key = d3.key)
union all
select d2.*
from d2
where not exists (select 1 from d3 where d2.key = d3.key);

答案 1 :(得分:1)

尝试以下操作,您需要遍历所有选定的表格,而不是使用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