Excel VBA 自动填充范围(单元格())不起作用
Excel VBA Autofill with Range(cells()) not working
我正在尝试使此代码正常工作,但当 运行 这一行时,我似乎无法摆脱 1004 运行时错误:
范围("F14:F53")。自动填充目标:=范围(单元格(14、6),单元格(53,colChosenMonth))。Select,类型:=xlFillValues。
该代码的目的是检查将 Range("F14:F53") 中的公式扩展到哪一列,变量 colChosenMonth 会执行此操作并返回正确的列号 (10)。但我似乎无法让它与自动填充公式一起使用。如果我尝试 select 它工作正常的范围。
我已经在下面发布了完整的代码,我希望有人能帮我找出错误,它在代码的后面很远。
Sub PopulateBudget()
' Läs in värden från QV till PB
'
Dim colChosenMonth As Long
'
Sheets("Periodiserad budget").Select
Selection.Worksheet.Unprotect
Range("E14").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(QV!C8,QV!C9,RC1,QV!C7,""<=""&R10C)"
Range("E14").Select
Selection.AutoFill Destination:=Range("E14:E53"), Type:=xlFillValues
Range("E14:E53").Select
Range("E34").Select
Selection.ClearContents
Range("F14").Select
ActiveCell.FormulaR1C1 = _
"=SUMIFS(QV!C8,QV!C9,RC1,QV!C7,""<=""&R10C)-SUM(RC5:RC[-1])"
Range("F14").Select
Selection.AutoFill Destination:=Range("F14:F53"), Type:=xlFillValues
Range("F14:F53").Select
Range("F34").Select
Selection.ClearContents
colChosenMonth = Application.Match(Range("vald_månad"), Worksheets("Periodiserad budget").Range("A10:BJ10"), 0)
Range(Cells(14, 5), Cells(53, colChosenMonth)).Select
'ROW BELOW DOESNT WORK --> Run-time error '1004': autofill method of range class failed.
Range("F14:F53").AutoFill Destination:=Range(Cells(14, 6), Cells(53, colChosenMonth)).Select, Type:=xlFillValues
'Selection.AutoFill Destination:=Range("F14:BJ53"), Type:=xlFillValues
'Range("F14:BJ53").Select
Range(Cells(14, 5), Cells(53, colChosenMonth)).Copy
Range(Cells(14, 5), Cells(53, colChosenMonth)).Select
'Range("E14:BJ53").Copy
'Range("E14:BJ53").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Worksheet.Protect
End Sub
删除 Range("F14:F53").AutoFill Destination:=Range(Cells(14, 6), Cells(53, colChosenMonth)).Select, Type:=xlFillValues.
行中的 .Select
阅读How to avoid using Select in Excel VBA, there are some good ideas how to avoid .Select
and .Activate
. Probably it is a good idea to submit your working code to https://codereview.stackexchange.com,您可能会得到一些有价值的想法。
我正在尝试使此代码正常工作,但当 运行 这一行时,我似乎无法摆脱 1004 运行时错误:
范围("F14:F53")。自动填充目标:=范围(单元格(14、6),单元格(53,colChosenMonth))。Select,类型:=xlFillValues。
该代码的目的是检查将 Range("F14:F53") 中的公式扩展到哪一列,变量 colChosenMonth 会执行此操作并返回正确的列号 (10)。但我似乎无法让它与自动填充公式一起使用。如果我尝试 select 它工作正常的范围。
我已经在下面发布了完整的代码,我希望有人能帮我找出错误,它在代码的后面很远。
Sub PopulateBudget()
' Läs in värden från QV till PB
'
Dim colChosenMonth As Long
'
Sheets("Periodiserad budget").Select
Selection.Worksheet.Unprotect
Range("E14").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(QV!C8,QV!C9,RC1,QV!C7,""<=""&R10C)"
Range("E14").Select
Selection.AutoFill Destination:=Range("E14:E53"), Type:=xlFillValues
Range("E14:E53").Select
Range("E34").Select
Selection.ClearContents
Range("F14").Select
ActiveCell.FormulaR1C1 = _
"=SUMIFS(QV!C8,QV!C9,RC1,QV!C7,""<=""&R10C)-SUM(RC5:RC[-1])"
Range("F14").Select
Selection.AutoFill Destination:=Range("F14:F53"), Type:=xlFillValues
Range("F14:F53").Select
Range("F34").Select
Selection.ClearContents
colChosenMonth = Application.Match(Range("vald_månad"), Worksheets("Periodiserad budget").Range("A10:BJ10"), 0)
Range(Cells(14, 5), Cells(53, colChosenMonth)).Select
'ROW BELOW DOESNT WORK --> Run-time error '1004': autofill method of range class failed.
Range("F14:F53").AutoFill Destination:=Range(Cells(14, 6), Cells(53, colChosenMonth)).Select, Type:=xlFillValues
'Selection.AutoFill Destination:=Range("F14:BJ53"), Type:=xlFillValues
'Range("F14:BJ53").Select
Range(Cells(14, 5), Cells(53, colChosenMonth)).Copy
Range(Cells(14, 5), Cells(53, colChosenMonth)).Select
'Range("E14:BJ53").Copy
'Range("E14:BJ53").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Worksheet.Protect
End Sub
删除 Range("F14:F53").AutoFill Destination:=Range(Cells(14, 6), Cells(53, colChosenMonth)).Select, Type:=xlFillValues.
阅读How to avoid using Select in Excel VBA, there are some good ideas how to avoid .Select
and .Activate
. Probably it is a good idea to submit your working code to https://codereview.stackexchange.com,您可能会得到一些有价值的想法。