我正在尝试编写一个包含下拉菜单的“复制/粘贴为值”宏。我认为我具有“下拉”部分,但是在插入下一个下拉值之前,我需要Excel一次复制/粘贴为一行值。
我需要弄清楚的是,我有 1)“ d8 = d”应以正确的顺序向上移动 2)复制/粘贴范围的语法正确吗?
3)如何告诉我一次复制/粘贴为一行值?
这段代码是否能为我提供所需的功能,包括插入下拉列表,让公式计算,一次复制和粘贴一行?
Sub Copy_Paste()
Sheets("MS Wall Summary Weekly View").Activate
Dim vRws As Long, vRng As Range
Dim d As Range, d8 As Range, Wst As Worksheet
Dim vRng2 As Range, vRws2 As Long, i As Long
Set Wst = Worksheets("MS Wall Summary Weekly View")
Set d8 = Wst.Range("D8")
With Wst
vRws = .Cells(.Rows.Count, "A").End(xlUp).Row
Set vRng = Range(.Cells(2, "A"), .Cells(vRws, "A"))
End With
For Each d In vRng.Cells
d8 = d
Sheets("Email").Activate
With ActiveSheet
vRws2 = .Cells(.Rows.Count, "U").End(x1Up).Row
For i = 8 To vRws2
Set vRng2 = Range("U" & i & "AC" & i)
vRng2.Copy
vRng2.PasteSpecial Paste:=xlPasteValues
End With
Next d
Next i
答案 0 :(得分:2)
关于编译错误的好处是,它们可以准确告诉您错误的存在位置。
下的标记行上有错别字和无效的字符串...End(x1Up)
(您将数字1替换为字母l)。 Set vRng2 = Range (""U" & i:"AC"& i")
。您在U
周围有双引号,并且没有将:
括在引号内。您还将i
留在了引号内。请在下面的代码中查看正确的方法Option Explicit
Sub Copy_Paste()
Sheets("MS Wall Summary Weekly View").Activate
Dim vRws As Long, vRng As Range
Dim d As Range, d8 As Range, Wst As Worksheet
Dim vRng2 As Range, vRws2 As Long, i As Long
Set Wst = Worksheets("MS Wall Summary Weekly View")
Set d8 = Wst.Range("D8")
With Wst
vRws = .Cells(.Rows.Count, "A").End(xlUp).Row
Set vRng = .Range(.Cells(2, "A"), .Cells(vRws, "A"))
End With
For Each d In vRng.Cells
d8 = d
Sheets("Email").Activate
With ActiveSheet
vRws2 = .Cells(.Rows.Count, "U").End(xlUp).Row '< -- Here
For i = 8 To vRws2
Set vRng2 = .Range("U" & i & "AC" & i) '< -- Here
Next d
其他几个问题一目了然
MS Wall Summary Weekly View
工作表变量,并从开始处删除.Activate
行With
块。您的Range
属性必须加点号d8
声明为范围,但将其设置为等于d
。您可能需要重新查看此处的目标,因为这不是有效范围Cells
属性(上面的错误2:)可以更容易地建立多个单元格的范围。您可以改用.Range(.Cells(i, "U"), .Cells(i, "AC"))
(注意限定符点,与With ActiveSheet
块变量相反)