我有一个控制模块,它可以在另一个模块之后调用一个模块。
我的问题:如果我通过控制模块进行呼叫,则会遇到运行时错误1004。
当我手动启动宏时,没有问题,而且每一件事都可以正常工作..
这是我的代码:
[...]
rngname = 3
temp = 1
Do Until Cells(lngRow, 1).Value = "test"
lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row
Set slcFind = Range(Cells(lngRow, 1), Cells(lngLastRowOfSection, 1))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(1, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngRow >= Rows.Count Then Exit Do
Loop
lngRow = 1
rngname = 3
i = 2
Do Until Cells(lngRow, 1).Value = "test"
lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row
Set slcFind = Range(Cells(lngRow, 2), Cells(lngLastRowOfSection, 2))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(i, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngRow >= Rows.Count Then Exit Do
i = i + 1
Loop
[...]
有人有想法吗? 这部分代码标记为:
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
最诚挚的问候, Kalain
答案 0 :(得分:0)
Scott Holtzman是正确的,在设置范围时定义工作表。我能够通过1张包含有效数据,要粘贴的工作表和另一张完全空白的工作表来重新创建错误。当我从有效工作表中运行宏时,它很好,当我在激活空白工作表的情况下运行它时,它会尝试将空白列转置为一行。 Excel工作表有更多的列然后是行,所以它崩溃,因为它不适合。
尝试使用此功能,更改" First"无论您的来源表是什么。
Sub test()
Dim Other As New Worksheet
Set Other = Worksheets("First")
lngrow = 1
rngname = 3
temp = 1
Do Until other.Cells(lngrow, 1).Value = "test"
lngLastRowOfSection = Other.Cells(lngrow, 1).End(xlDown).Row
Set slcFind = Range(Other.Cells(lngrow, 1), Other.Cells(lngLastRowOfSection, 1))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(1, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngrow = Other.Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngrow >= Rows.Count Then Exit Do
Loop
lngrow = 1
rngname = 3
i = 2
Do Until Other.Cells(lngrow, 1).Value = "test"
lngLastRowOfSection = Other.Cells(lngrow, 1).End(xlDown).Row
Set slcFind = Range(Other.Cells(lngrow, 2), Other.Cells(lngLastRowOfSection, 2))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(i, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngrow = Other.Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngrow >= Rows.Count Then Exit Do
i = i + 1
Loop
End Sub