繁体   English   中英

Excel VBA如果是ElseIF其他

[英]Excel VBA If ElseIF else

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

if声明:

'=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中的数量。如果是,则检查是否为绝对值列L中的金额小于列V中金额的绝对值。如果为真,则返回列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

提前致谢!!

https://i.stack.imgur.com/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。 用偏移量替换R1C1引用,然后它应该适合您。 如果上面没有做任何事情,或者是不正确的,请告诉我......但看看你是否能理解我正在尝试的事情,因为我认为这是你主要问题的症结所在。

编辑:使用myCell而不是ActiveCell

Edit2:编辑:这是代码的“更严格”版本:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM