运行时错误'1004',已经研究并尝试了几种推荐的解决方案无济于事

时间:2017-03-01 16:13:15

标签: vba excel-vba excel-formula runtime-error excel

我正在尝试使用公式填充范围并继续获得运行时错误“1004”。错误发生在我已加星标表格(“预测”)的行上。范围(“H125”)。公式= formulaTest 。我的Sub中的代码如下:

Sub FirmShareFill()

Dim RampUp As Range
Dim RampBas As Range
Dim RampDn As Range
Dim Numbering As Range
Dim Approval As Range
Dim PeakShare As Range



Dim tcount As Byte
Dim bcount As Byte
Dim ubdcount As Byte
Dim yearRange2 As Byte

year = Worksheets("Inputs").Range("B6").Value
cntry = Worksheets("Inputs").Range("B5").Value
bnd = Worksheets("Inputs").Range("B3").Value
typ = Worksheets("Inputs").Range("B2").Value
cat = Worksheets("Inputs").Range("B4").Value

tcount = bnd * cat + bnd
ubdcount = tcount * 2 + 1
yearCount = year * 4 - 1

        For ubd = 1 To 3
        For t = 1 To typ
        For b = 1 To bnd
        For c = 1 To cat
        For i = 1 To cntry

        Set RampUp = Columns(7).Find(What:="Ramp_Up" & i, MatchCase:=True).Offset(0, 1)
        Set RampBas = Columns(7).Find(What:="Ramp_Bas" & i, MatchCase:=True).Offset(0, 1)
        Set RampDn = Columns(7).Find(What:="Ramp_Dn" & i, MatchCase:=True).Offset(0, 1)

        Set Numbering = Sheets("Inputs").Range("B13")
        Set Approval = Columns(6).Find(What:="Approval", MatchCase:=True).Offset(i, 2 + ubd)


        bcount = c + (cat + 1) * (b - 1)

        If t = 1 And b = 1 And ubd = 1 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c)
        ElseIf t = 1 And b > 1 And ubd = 1 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount)
        ElseIf t > 1 And b = 1 And ubd = 1 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount)
        ElseIf t > 1 And b > 1 And ubd = 1 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount)

        ElseIf t = 1 And b = 1 And ubd = 2 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c + ubdcount)
        ElseIf t = 1 And b > 1 And ubd = 1 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount + ubdcount)
        ElseIf t > 1 And b = 1 And ubd = 1 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount + ubdcount)
        ElseIf t > 1 And b > 1 And ubd = 1 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount + ubdcount)

        ElseIf t = 1 And b = 1 And ubd = 3 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c + 2 * ubdcount)
        ElseIf t = 1 And b > 1 And ubd = 3 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount + 2 * ubdcount)
        ElseIf t > 1 And b = 1 And ubd = 3 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount + 2 * ubdcount)
        ElseIf t > 1 And b > 1 And ubd = 3 Then
            Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount + 2 * ubdcount)
        End If

        Dim formulaTest As String

        formulaTest = "=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","", " & PeakShare.Address & " * " & RampUp.Address & ")"

        If ubd = 1 Then
        **Sheets("Forecast").Range("H125").Formula = formulaTest**
        ActiveCell.Offset(1, 0).Select

        ElseIf ubd = 2 Then
        Range(ActiveCell, ActiveCell.Offset(0, yearRange2)).Formula = "=IF(" & Numbering.Address(False, False) & " < " & Approval.Offset(1, 0).Address & ","", " & PeakShare.Address & " * " & RampBas.Address & ""

        ElseIf ubd = 3 Then
        Range(ActiveCell, ActiveCell.Offset(0, yearRange2)).Formula = "=IF(" & Numbering.Address(False, False) & " < " & Approval.Offset(1, 0).Address & ","", " & PeakShare.Address & " * " & RampDn.Address & ""

        End If


        Next i

        ActiveCell.Offset(1, 0).Select

        Next c
        Next b
        Next t
        Next ubd



End Sub

我认为错误可能与我如何宣布范围“编号”范围有关,但到目前为止,我一直无法弄明白。我已多次在同一张纸上使用此代码,唯一的区别是我在另一张纸上设置了一个范围,编号。

3 个答案:

答案 0 :(得分:1)

这应该有效:

formulaTest = "=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ",""""," & PeakShare.Address & "*" & RampUp.Address & ")"

正如@Comintern指出的那样,您需要使用""""在公式中包含双空语音标记。我还删除了*

两侧的空格

答案 1 :(得分:0)

更改

IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","", " & PeakShare.Address & " * " & RampUp.Address & ")"

IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","""", " & PeakShare.Address & " * " & RampUp.Address & ")"

答案 2 :(得分:0)

您可以使用"内的Chr(34)"进行Formula检查,而不是计算"的{​​{1}}数量。

在您的情况下,请使用:

"=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & "," & Chr(34) & ", " & PeakShare.Address & " * " & RampUp.Address & ")"