范围类的自动填充方法失败:VBA
我是VBA新手,无法理解代码在哪里掉下来。 尝试执行If语句后收到消息“Range类的自动填充方法失败”。
我的代码是:
Columns("H:H").Select ActiveCell.FormulaR1C1 = _ "=IF(C[-6] = ""Commodities Ags/Softs"", (IF(RC[-3]=R1C24,""Y"",(IF(RC[-3]=R2C24,""Y"",(IF(RC[-3]=R3C24,""Y"",(IF(RC[-3]=R4C24,""Y"",(IF(RC[-3]=R5C24,""Y"",(IF(RC[-3]=R6C24,""Y"",(IF(RC[-3]=R7C24,""Y"",(IF(RC[-3]=R8C24,""Y"",(IF(RC[-3]=R9C24,""Y"",""N"")))))))))))))))))),"""")" Selection.AutoFill Destination:=Columns("H:H"), Type:=xlFillDefault
它在最后一行失败,如果声明是好的,任何帮助将不胜感激,
干杯
这是你正在尝试?
Sub Sample() Dim ws As Worksheet Dim lRow As Long '~~> Change this to the relevant sheet Set ws = ThisWorkbook.Sheets("Sheet1") With ws '~~> Find last row in Col H lRow = .Range("H" & .Rows.Count).End(xlUp).Row '~~> Enter the formula in one go .Range("H1:H" & lRow).FormulaR1C1 = "=IF(C[-6] = ""Commodities Ags/Softs"", " & _ "(IF(RC[-3]=R1C24,""Y"",(IF(RC[-3]=R2C24,""Y""," & _ "(IF(RC[-3]=R3C24,""Y"",(IF(RC[-3]=R4C24,""Y""," & _ "(IF(RC[-3]=R5C24,""Y"",(IF(RC[-3]=R6C24,""Y""," & _ "(IF(RC[-3]=R7C24,""Y"",(IF(RC[-3]=R8C24,""Y""," & _ "(IF(RC[-3]=R9C24,""Y"",""N"")))))))))))))))))),"""")" End With End Sub
Autofill
工作是这样的:
SingleCell.AutoFill MoreCells
你有
ColumnH.AutoFill ColumnH
如果你想使用自动填充,它可能看起来像这样
With ActiveSheet.Range("H1") .FormulaR1C1 = _ "=IF(C[-6] = ""Commodities Ags/Softs"", (IF(RC[-3]=R1C24,""Y"",(IF(RC[-3]=R2C24,""Y"",(IF(RC[-3]=R3C24,""Y"",(IF(RC[-3]=R4C24,""Y"",(IF(RC[-3]=R5C24,""Y"",(IF(RC[-3]=R6C24,""Y"",(IF(RC[-3]=R7C24,""Y"",(IF(RC[-3]=R8C24,""Y"",(IF(RC[-3]=R9C24,""Y"",""N"")))))))))))))))))),"""")" .AutoFill Destination:=.Resize(100, 1), Type:=xlFillDefault End With
我没有自动填充整列100万行的B / C,我没有那种时间。
Siddharth的R1C1答案是要走的路,我只想给你提供一些关于Autofill的信息。