Range类的自动填充方法失败

时间:2014-04-04 19:39:39

标签: excel vba excel-vba autofill

这行代码在我的宏中的某一点上起作用:

Selection.AutoFill Destination:=Range(Cells(2, agentEmailColumn), Cells(propertyRows, agentEmailColumn)), Type:=xlFillDefault

但是当我第二次尝试使用它时失败了,我得到以下错误:

Run-time error '1004':
AutoFill method of Range class failed

我如何解决这个问题?

整个宏:

Sub adj_prop_zip()

Dim wsProperty As Worksheet:              Set wsProperty = Worksheets("propertyOutput.csv")
Dim zipColumnProperty As Integer:       zipColumnProperty = 5 ' column that has properly adjusted zip codes
Dim propertyRows As Integer:               propertyRows = wsProperty.Cells(Rows.Count, 1).End(xlUp).Row

Dim userEmailColumn As Integer:         userEmailColumn = 6 ' column that has user emails
' Dim lookupRange As String:                  lookupRange = "'userOutput.csv!'R[2]C:R[162]C5" ' range for vlookup table on user table

' count rows of userOutput to set up vlook up for user emails
Dim userRows As Integer:                            userRows = Worksheets("userOutput.csv").Cells(Rows.Count, 1).End(xlUp).Row
Dim agentIdColumn As Integer:                   agentIdColumn = 7
Dim agentEmailUserOutput As Integer:        agentEmailUserOutput = 8 ' used on UserOutput for placing agent email

' count rows of agentsOutput to set up vlook up for agent emails
Dim agentRows As Integer:                      agentRows = Worksheets("agentsOutput.csv").Cells(Rows.Count, 1).End(xlUp).Row
Dim agentEmailColumn As Integer:        agentEmailColumn = 8



Worksheets("propertyOutput.csv").Select
    Range("E1").Select
    Application.CutCopyMode = False

    ' insert adjusted zip formula into cells
            ActiveCell.FormulaR1C1 = "adjusted_zip"
            Range("E2").Select
            ActiveCell.FormulaR1C1 = "=IF(AND(ISNUMBER(RC[-3]),(RC[-3]>0)),VALUE(RC[-3]),"""")"
            '=IF(AND(ISNUMBER(B16),(B16>0)),VALUE(B16),"")
            Range("E2").Select
            Selection.NumberFormat = "00000"
            ' next line coppies sellected cells into a range
            Selection.AutoFill Destination:=Range(Cells(2, zipColumnProperty), Cells(propertyRows, zipColumnProperty)), Type:=xlFillDefault

    ' insert user email vlookup formula into cells
            Range("F1").Select
            ActiveCell.FormulaR1C1 = "User Email"
            Range("F2").Select
            ' =VLOOKUP($C2,userOutput.csv!$A$2:$E$162,4)
            ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3], 'userOutput.csv'!R2C1:R" & userRows & "C5 ,4, FALSE)"
            Selection.AutoFill Destination:=Range(Cells(2, userEmailColumn), Cells(propertyRows, userEmailColumn)), Type:=xlFillDefault

    ' insert agent id into report
            Range("G1").Select
            ActiveCell.FormulaR1C1 = "Adjusted Agent ID"
            Range("G2").Select
            ' =VLOOKUP(C2, userOutput.csv!$A$2:$F$162,6)
            ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-4], 'userOutput.csv'!R2C1:R" & userRows & "C8 ,8, FALSE)"
            Selection.AutoFill Destination:=Range(Cells(2, agentIdColumn), Cells(propertyRows, agentIdColumn)), Type:=xlFillDefault

    ' insert agent email into report
            Range("H1").Select
            ActiveCell.FormulaR1C1 = "Agent Email"
            Range("H2").Select
            ' =VLOOKUP(G2,agentsOutput.csv!$A$2:$F$11,4)
            ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1], 'agentsOutput.csv'!R2C1:R" & agentRows & "C6 ,4, FALSE)"
            Selection.AutoFill Destination:=Range(Cells(2, agentEmailColumn), Cells(propertyRows, agentEmailColumn)), Type:=xlFillDefault

    Range("E2").Select


' inserting formulas into the userOutput.csv sheet
  Worksheets("userOutput.csv").Select
'                Application.CutCopyMode = False
            Range("I1").Select
            ' insert agent email into report
            ActiveCell.FormulaR1C1 = "Agent Email"
            Range("I2").Select
            ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1], 'agentsOutput.csv'!R2C1:R" & agentRows & "C6 ,4, FALSE)"

' this next line fails:
            Selection.AutoFill Destination:=Range(Cells(2, agentEmailColumn), Cells(propertyRows, agentEmailColumn)), Type:=xlFillDefault


' insert time stamp
 Worksheets("WorkStation").Select
    Range("B5").Select
    ActiveCell = DateTime.Now
    Range("C5") = propertyRows

    Range("A1").Select

End Sub

0 个答案:

没有答案