我试图创建一个VBA宏来填充某个范围内为空的单元格(“ INV_Nums”),而不覆盖已包含数据的单元格。为了做到这一点,我试图使用一个if语句来检查所讨论的单元格是否为空...如果不是,那么我希望循环继续到下一个单元格,但是如果它为空,那么我想要在单元格中输入index(__,(match())函数。
我在True语句中始终收到“编译错误:不匹配”,但我对为什么我的Synatax出错感到困惑。任何帮助将不胜感激。
这是我的代码:
Dim i As Integer
Dim Rng As Range
Dim ARwkb As Excel.Workbook
Dim ARwks As Excel.Worksheet
Dim Samwkb As Excel.Workbook
Dim Samwks As Excel.Worksheet
Set Samwkb = Excel.Workbooks("Samples - one sheet")
Set Samwks = Samwkb.Worksheets("samples shipment")
Set ARwkb = Excel.Workbooks("AR balance.xlsx")
Set ARwks = ARwkb.Worksheets("Total Trading")
Set Rng = Samwkb.Range("INV_Nums")
For i = 6 To Rng.Rows.Count + 6
If Range("AAi") = "" Is True Then
Range("AAi").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('AR balance.xlsx'!AR_Invoice_Nums,MATCH(RC[-21],'AR
balance.xlsx'!AR_PL_Nums,0))"
End If
Next i
答案 0 :(得分:1)
问题在于您如何确定范围和管理标准。
For i = 6 To Rng.Rows.Count + 6
If IsEmpty(Range("AA" & i)) Then
Range("AA" & i).FormulaR1C1 = _
"=INDEX('AR balance.xlsx'!AR_Invoice_Nums, MATCH(RC[-21],'AR balance.xlsx'!AR_PL_Nums, 0))"
End If
Next i
.SpecialCells方法可以快速确定空白单元格,并且可以使用xlR1C1公式一次插入所有公式。
...
with Samwkb.Range("INV_Nums")
.specialcells(xlcelltypeblanks).FormulaR1C1 = _
"=INDEX('AR balance.xlsx'!AR_Invoice_Nums, MATCH(RC[-21],'AR balance.xlsx'!AR_PL_Nums, 0))"
end with
...