Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在vba中循环时出现逻辑问题_Vba_Excel_Loops - Fatal编程技术网

在vba中循环时出现逻辑问题

在vba中循环时出现逻辑问题,vba,excel,loops,Vba,Excel,Loops,我创建了一个特别长的vba宏来为我编辑一个大的数据电子表格,而不是自己做数千次。实际编辑的代码工作正常,但是,当我添加第一个if语句和第一个while循环以使其在整个电子表格中循环时,我得到一个运行时错误1004。我是vba新手,但我非常确定我的逻辑中存在错误,而不是代码本身。我已经标记了添加时导致错误的代码行 Sub RCFS() Dim ProfCtr As String Dim Year As String Dim Amount As Currency Dim Period As L

我创建了一个特别长的vba宏来为我编辑一个大的数据电子表格,而不是自己做数千次。实际编辑的代码工作正常,但是,当我添加第一个if语句和第一个while循环以使其在整个电子表格中循环时,我得到一个运行时错误1004。我是vba新手,但我非常确定我的逻辑中存在错误,而不是代码本身。我已经标记了添加时导致错误的代码行

  Sub RCFS()

Dim ProfCtr As String
Dim Year As String
Dim Amount As Currency
Dim Period As Long
Dim S2FreecellH As Long
Dim ProfCenCellH As Long
Dim FreeCellClone As Long
Dim Clone2 As Long
Dim Clone3 As Long


Dim y As Long ' placeholder 2
y = 1

S2FreecellH = 3
ProfCenCellH = 2
AmountH = 2
PeriodH = 2
YearH = 2


ProfCtr = Cells(ProfCenCellH, 4)
Year = Cells(YearH, 7)
Amount = Cells(AmountH, 8)
Period = Cells(PeriodH, 6)

'//////////////////////////////////////////////////////////////////////////////////
While IsEmpty(Cells(ProfCenCell, 4).Value) = False 
在上面的while循环和下面的if语句之前,一切正常。剩下的没有这两条语句也可以,但是我需要它来循环整个电子表格


您从未为ProfCenCell设置值,因此它的默认值为0。然后,你使用CellsFofcencell,4,在你的例子中是Cells0,4,0会产生问题。

哦,天哪,我真不敢相信我漏掉了,它应该是ProfCenCell,而不是ProfCenCell。我修复了它,它消除了运行时错误,但现在它给了我一个溢出错误。@user392500您能将Option Explicit放在页面顶部并重新编译吗?仅供参考,静态代码分析本可以让您不用问这个问题。。。另外,我强烈建议您尝试它的缩进项目功能;嗯,看起来很有趣,我去看看。我同意Vba是缺少缩进的一种恼人的原因。Dim Year As String不使用预先存在的函数名作为变量名。您还应该考虑使用WorkSeSsESET2替换38个工作表SHIET2引用。在某一点上,年份Mod 11*4可能等于0,这将导致对象定义错误。TAB键在手动缩进方面发挥了神奇的作用。。。不要为此责备语言。
If Cells(ProfCenCell, 4).Value = Worksheets("Sheet2").Cells(S2FreecellH, 1).Value Then

Worksheets("Sheet2").Cells(S2FreecellH, 1).Value = ProfCtr
Worksheets("Sheet2").Cells(S2FreecellH, 5).Value = ProfCtr
Worksheets("Sheet2").Cells(S2FreecellH, 9).Value = ProfCtr

FreeCellClone = S2FreecellH 'setting clones
Clone2 = S2FreecellH
Clone3 = S2FreecellH

For x = S2FreecellH + 1 To S2FreecellH + 12
Worksheets("Sheet2").Cells(x, 2).Value = y 'Creating 1 to 12 numbering in column 1
Worksheets("Sheet2").Cells(x, 6).Value = y 'Creating 1 to 12 numbering in column 2
Worksheets("Sheet2").Cells(x, 10).Value = y 'Creating 1 to 12 numbering in column 3
S2FreecellH = S2FreecellH + 1
y = y + 1
Next x

While Worksheets("Sheet2").Cells(FreeCellClone, 1).Value = Cells(YearH, 4).Value 'Loop to input all amounts
Worksheets("Sheet2").Cells(FreeCellClone + Period, (((Year Mod 11) * 4)) - 1).Value = Amount 'Calculation on post year to select correct column to post amount in

PeriodH = PeriodH + 1
AmountH = AmountH + 1
YearH = YearH + 1

Year = Cells(YearH, 7)
Amount = Cells(AmountH, 8)
Period = Cells(PeriodH, 6)
Wend

Worksheets("Sheet2").Cells(S2FreecellH + 1, 3) = WorksheetFunction.Sum(Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(FreeCellClone + 1, 3), Worksheets("Sheet2").Cells(S2FreecellH, 3)))
Worksheets("Sheet2").Cells(S2FreecellH + 1, 7) = WorksheetFunction.Sum(Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(FreeCellClone + 1, 7), Worksheets("Sheet2").Cells(S2FreecellH, 7)))  'Creating sums for all 3 columns
Worksheets("Sheet2").Cells(S2FreecellH + 1, 11) = WorksheetFunction.Sum(Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(FreeCellClone + 1, 11), Worksheets("Sheet2").Cells(S2FreecellH, 11)))

For Z = Clone2 + 1 To Clone2 + 12  'creating intitial percentage values
Worksheets("Sheet2").Cells(Z, 4).Value = Format((Worksheets("Sheet2").Cells(Z, 3) / Worksheets("Sheet2").Cells(S2FreecellH + 1, 3)) * 100, "%0.00")
Worksheets("Sheet2").Cells(Z, 8).Value = Format((Worksheets("Sheet2").Cells(Z, 7) / Worksheets("Sheet2").Cells(S2FreecellH + 1, 7)) * 100, "%0.00")
Worksheets("Sheet2").Cells(Z, 12).Value = Format((Worksheets("Sheet2").Cells(Z, 11) / Worksheets("Sheet2").Cells(S2FreecellH + 1, 11)) * 100, "%0.00")
Next

For q = Clone3 + 1 To Clone3 + 12 'creating final percentage values
Worksheets("Sheet2").Cells(q, 13).Value = Format(((Worksheets("Sheet2").Cells(q, 4) + Worksheets("Sheet2").Cells(q, 8) + Worksheets("Sheet2").Cells(q, 12)) / 3) * 100, "%0.00")
Next q

Worksheets("Sheet2").Cells(S2FreecellH + 1, 13) = WorksheetFunction.Sum(Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(FreeCellClone + 1, 13), Worksheets("Sheet2").Cells(S2FreecellH, 13)))




Else
ProfCenCell = ProfCenCell + 1
End If
    '/////////////////////////////////////////////////////////////////////////////// Loop these Loops
S2FreecellH = S2FreecellH + 3
y = 1
Wend

End Sub