简体   繁体   中英

Excel VBA If ElseIF else

Im having a little trouble with a code in excel vba. My goal is convert the following if statement to a VBA macro.

The if Statement:

'=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).

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

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. and down 2 rows, or if two offset cells are less than 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. Replace the R1C1 references with an Offset, and then it should work for you. 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 .

Edit2: Edit: Here's a "tighter" version of the code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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