我有这个宏我写过来过滤和排序工作表,然后在任何称为“短期付款”的单元格旁边添加vlookup公式。如果G列中有多行“短付”,我的代码就可以正常工作。
如果只有一行在G列中有“短付”,则宏开始使用标题,将标题(第1行)中的内容复制并粘贴到第2行。我发现奇怪的是,如果有多行“短付”,那么宏根本不会触及标题。这是代码:
Sub Prox2()
Dim LastRow As Long
Dim xRow As Range
Dim xRg As Range
Dim xRows As Range
ActiveSheet.Range("A:O").AutoFilter Field:=7, Criteria1:= _
"Short Paid"
LastRow = Range("G" & Rows.Count).End(xlUp).Row
Range("H2") = "=VLOOKUP(A2,Sheet2!A:B,2,FALSE)": Range("H2:H" &
LastRow).FillDown
Range("H2:H" & LastRow).Select
Selection.Copy
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Range("H2:H" & LastRow).Select
Application.CutCopyMode = False
Selection.ClearContents
Range("H2") = "=VLOOKUP(A2,Sheet2!A:P,16,FALSE)": Range("H2:H" &
LastRow).FillDown
Range("C2:C" & LastRow).Select
Application.CutCopyMode = False
Selection.ClearContents
Range("C2") = "=VLOOKUP(A2,Sheet2!A:F,6,FALSE)": Range("C2:C" &
LastRow).FillDown
Range("F2:F" & LastRow).Select
Application.CutCopyMode = False
Selection.ClearContents
Range("F2") = "=VLOOKUP(A2,Sheet2!A:J,10,FALSE)": Range("F2:F" &
LastRow).FillDown
Range("E2:E" & LastRow).Select
Application.CutCopyMode = False
Selection.ClearContents
Range("E2") = "=F2-30": Range("E2:E" & LastRow).FillDown
ActiveSheet.Range("A:O").AutoFilter Field:=7
End Sub
答案 0 :(得分:0)
问题是你试图找到LastRow(LastRow = ...)你没有检查可能没有行的可能性。你应该添加:
If LastRow = 1 Then
'This means no rows found. Exit or do something else.
编辑:
尝试这种方法:
Range("H2:H" & LastRow).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=VLOOKUP(RC1, Sheet2!C1:C2, 2, FALSE)"