为什么在此宏中出现编译错误

时间:2019-04-16 16:28:51

标签: vba

我正在尝试编写一个包含下拉菜单的“复制/粘贴为值”宏。我认为我具有“下拉”部分,但是在插入下一个下拉值之前,我需要Excel一次复制/粘贴为一行值。

我需要弄清楚的是,我有 1)“ d8 = d”应以正确的顺序向上移动 2)复制/粘贴范围的语法正确吗?

3)如何告诉我一次复制/粘贴为一行值?

这是带有我要复制/粘贴为值的单元格的工作表 enter image description here

这段代码是否能为我提供所需的功能,包括插入下拉列表,让公式计算,一次复制和粘贴一行?

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

1 个答案:

答案 0 :(得分:2)

关于编译错误的好处是,它们可以准确告诉您错误的存在位置。

下的标记行上有错别字和无效的字符串
  1. 错误1:...End(x1Up)(您将数字1替换为字母l)。
  2. 错误2: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

其他几个问题一目了然

  1. 立即声明您的MS Wall Summary Weekly View工作表变量,并从开始处删除.Activate
  2. 您并未完全使用With块。您的Range属性必须加点号
  3. 您已将d8声明为范围,但将其设置为等于d。您可能需要重新查看此处的目标,因为这不是有效范围
  4. 使用Cells属性(上面的错误2:)可以更容易地建立多个单元格的范围。您可以改用.Range(.Cells(i, "U"), .Cells(i, "AC"))(注意限定符点,与With ActiveSheet块变量相反)