简体   繁体   English

Excel VBA如果是ElseIF其他

[英]Excel VBA If ElseIF else

Im having a little trouble with a code in excel vba. 我在excel vba中的代码有点麻烦。 My goal is convert the following if statement to a VBA macro. 我的目标是将以下if语句转换为VBA宏。

The if Statement: 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)))

What I want to do is that If there is a W in column R, I want it to check if the amount in Column L is great that zero, if it true, return the amount in column L. If false check if the absolute value of the amount in column L is less than the absolute value of the amount in column V. If true, return the amount in column L otherwise sum the amount in column N and column L. If the sum is less than zero return the sum of the amount in column L and Z. If it's false return the amount in column v (make it negative). 我想要做的是,如果列R中有一个W,我希望它检查列L中的数量是否大于零,如果为真,则返回列L中的数量。如果是,则检查是否为绝对值列L中的金额小于列V中金额的绝对值。如果为真,则返回列L中的金额,否则将列N和列L中的金额相加。如果总和小于零,则返回总和列L和Z中的数量。如果为假,则返回列v中的数量(使其为负数)。

My attempt to solve it. 我试图解决它。

   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

Thanks in advance!! 提前致谢!!

https://i.stack.imgur.com/4KiVg.png https://i.stack.imgur.com/4KiVg.png

If I understand what you're trying to do correctly, it's take your ...Range("V" & rw_cnt) cell and see if it's greater than 0, less than an absolute value of a cell left one col. 如果我理解你正在尝试做什么,那就是你的...Range("V" & rw_cnt)单元格并查看它是否大于0,小于单元格的绝对值。 and down 2 rows, or if two offset cells are less than 0... 向下2行,或者如果两个偏移单元小于0 ...

How does this work? 这是如何运作的?

'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

Basically, where you have a formula, VBA is just interpreting that as a String. 基本上,在有公式的地方,VBA只是将其解释为String。 Replace the R1C1 references with an Offset, and then it should work for you. 用偏移量替换R1C1引用,然后它应该适合您。 If the above doesn't do anything, or is incorrect, let me know...but see if you can understand what I'm trying, as I believe that's the crux of your main issue. 如果上面没有做任何事情,或者是不正确的,请告诉我......但看看你是否能理解我正在尝试的事情,因为我认为这是你主要问题的症结所在。

edit: Instead of ActiveCell , use myCell . 编辑:使用myCell而不是ActiveCell

Edit2: Edit: Here's a "tighter" version of the code: 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