我有一系列步骤变成了一个宏。当独立运行时,它们都很好,但是在集体运行时,在步骤18上发生了某些情况,这会导致“我粘贴到的位置与我的副本的大小不同”的问题。你们有什么可以帮助的吗?我的代码应该没问题,因为当我特别选择第18步时,它可以正常运行,但是我确定自己做错了什么。这是我第一次使用VBA,所以任何帮助!这样做的目的是将vlookup语句填充到P,Q和R列中的动态范围(R列从工作簿中的另一张纸获取其值)表示卡住的位置是:
"Range("P2:P" & LR).PasteSpecial xlPasteAll"
以下是供参考的步骤18和步骤18的代码:
Step 17:
Sub Step17_2()
'
' Step17_2 Macro
'
'
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("D:M").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlToLeft
End Sub
Step 18:
Sub Step18()
'
' Step18 Macro
'
'
Sheets("Contact").Select
Range("P1").Select
ActiveCell.FormulaR1C1 = "VlookupType"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "VlookupIP"
Range("R1").Select
ActiveCell.FormulaR1C1 = "VlookupMailingName"
Range("P1").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Dim LR As Integer
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("P2").Formula = "=VLOOKUP(A2,PastedValues!A$2:D$" & LR & ",2,FALSE)"
Range("P2").Copy
Range("P2:P" & LR).PasteSpecial xlPasteAll
Application.CutCopyMode = False
Range("Q2").Formula = "=VLOOKUP(A2,PastedValues!A$2:D$" & LR & ",4,FALSE)"
Range("Q2").Copy
Range("Q2:Q" & LR).PasteSpecial xlPasteAll
Application.CutCopyMode = False
Sheets("ContactDetailed").Select
Dim TA As Integer
TA = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Contact").Select
Range("R2").Formula = "=VLOOKUP(A2,ContactDetailed!A$2:D$" & TA & ",4,FALSE)"
Range("R2").Copy
Range("R2:R" & LR).PasteSpecial xlPasteAll
Application.CutCopyMode = False
End Sub
答案 0 :(得分:1)
这里有一些原始代码,一些技巧:
.Select
,请阅读here原因和方式。Integer
变量。为什么?您的数据在某个时候可能有更多数据可以存储一个整数变量,即-32,768
至32,767
。使用Long
或Double
不会给您带来麻烦。Option Explicit
。这样,您就不会错过任何未声明的变量。以下是我如何解释您的第18步的示例:
Dim LR As Double, TA As Double
Option Explicit
Sub Step18()
TA = ActiveWorkbook.Sheets("ContactDetailed").Range("A" & Rows.Count).End(xlUp).Row
With ActiveWorkbook.Sheets("Contact")
.Range("P1").FormulaR1C1 = "VlookupType"
.Range("Q1").FormulaR1C1 = "VlookupIP"
.Range("R1").FormulaR1C1 = "VlookupMailingName"
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Range("P2:P" & LR).Formula = "=VLOOKUP(A2,PastedValues!A$2:D$" & LR & ",2,FALSE)"
.Range("Q2:Q" & LR).Formula = "=VLOOKUP(A2,PastedValues!A$2:D$" & LR & ",4,FALSE)"
.Range("R2:R" & LR).Formula = "=VLOOKUP(A2,ContactDetailed!A$2:D$" & TA & ",4,FALSE)"
End With
End Sub
还查看了您的步骤17_2,例如,您可以这样简化:
Sub Step17_2()
With ActiveWorkbook.Sheets("Contact") 'I assume you meant this sheet, change if need be
.Range("C:C,E:N,P:AA").EntireColumn.Delete 'I used AA but you could also change this
End With
End Sub