簡體   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