2016-03-08 38 views
0

我使用下面的代碼來計算的數據平衡,平衡=需求 - 收集多重條件VB宏在Excel中,簡單的代碼需要修改

代碼:

Sub DCB() 
Dim c As Range 
Dim lRow As Long 
lRow = 1 
Dim lRowLast As Long 
Dim lRowDiff As Long 
Dim lRowPortion As Long 
lRowPortion = 1 
Dim bFoundCollection As Boolean 
With ActiveSheet 
    lRowLast = .Cells(.Rows.Count, 1).End(xlUp).Row 
    Do 
    Set c = .Range("A" & lRow) 
    If c.Value Like "*COLLECTION*" Then 
    bFoundCollection = True 
    ElseIf bFoundCollection Then 
    bFoundCollection = False 
    If c.Value <> "BALANCE" Then 
    c.EntireRow.Insert 
    lRowLast = lRowLast + 1 
    Set c = c.Offset(-1, 0) 
    c.Value = "BALANCE" 
    End If 
    If c.Value = "BALANCE" Then 
    .Range(c, c.Offset(0, 18)).Font.Color = RGB(0, 0, 0) 
    .Range(c, c.Offset(0, 18)).Interior.Color = RGB(200, 200, 200) 
    lRowDiff = c.Row - lRowPortion 
    .Range(c.Offset(0, 3), c.Offset(0, 18)).FormulaR1C1 = _ 
     "=SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*DEMAND*"", R[-" & lRowDiff & "]C:RC)" & _ 
     "-SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*COLLECTION*"", R[-" & lRowDiff & "]C:RC)" 
    lRowPortion = c.Row + 1 
    End If 
    End If 
    lRow = lRow + 1 
    Loop While lRow <= lRowLast + 1 
End With 
End Sub 

但我有一些現在的問題,在計算餘額時有一些條件需要應用。 U W Y列是超額金額列,這些列的值分別取決於I N S列餘額金額。

例如在I1中,需求是50個集合是10,所以按照公式,Balance = Demand - collection因此50-10 = 40,但是U列中有值,我想剩下的40個數量應該被扣除從U列&餘額應顯示0和調整後的40應該顯示在T1列。

我想要對上面的宏進行調整,首先應該檢查I2的值,如果I2的值大於I1則無問題,簡單的公式將應用B = DC,但如果I2的值較小比I1或I2的值爲0,則會檢查U1中是否有值,如果有一些值,那麼將相應地調整該量,並在T1中顯示調整量。

同樣N列與W列關聯,調整量顯示在V列& S列與Y列關聯,調整量顯示在X列上。 Kindly check the image

+0

這不是VB.NET代碼。它看起來像某種形式的VBA – Plutonix

回答

1

我想這是你想要什麼:

Sub DCB() 
Dim c As Range 
Dim lRow As Long 
lRow = 1 
Dim lRowLast As Long 
Dim lRowDiff As Long 
Dim lRowPortion As Long 
lRowPortion = 1 
Dim bFoundCollection As Boolean 
Dim lRowLastDemand As Long 


With ActiveSheet 
    lRowLast = .Cells(.Rows.Count, 1).End(xlUp).row 
    Do 
    Set c = .Range("A" & lRow) 
    If c.Value Like "*COLLECTION*" Then 
    bFoundCollection = True 
    ElseIf c.Value Like "*DEMAND*" Then 
    lRowLastDemand = lRow 
    ElseIf bFoundCollection Then 
    bFoundCollection = False 
    If c.Value <> "BALANCE" Then 
    c.EntireRow.Insert 
    lRowLast = lRowLast + 1 
    Set c = c.Offset(-1, 0) 
    c.Value = "BALANCE" 
    End If 
    If c.Value = "BALANCE" Then 
    .Range(c, c.Offset(0, 18)).Font.Color = RGB(0, 0, 0) 
    .Range(c, c.Offset(0, 18)).Interior.Color = RGB(200, 200, 200) 
    lRowDiff = c.row - lRowPortion 
    .Range(c.Offset(0, 3), c.Offset(0, 18)).FormulaR1C1 = _ 
     "=SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*DEMAND*"", R[-" & lRowDiff & "]C:RC)" & _ 
     "-SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*COLLECTION*"", R[-" & lRowDiff & "]C:RC)" 
     If ((.Cells(lRow, 9)) > 0) And (.Cells(lRowLastDemand, 21) > 0) Then 'If Column I Balance > 0 and U has value 
     .Cells(lRowLastDemand, 20) = .Cells(lRow, 9) 'T1 = Balance 
     .Cells(lRowLastDemand, 21) = .Cells(lRowLastDemand, 21) - .Cells(lRow, 9) 'Adjust Col U 
     End If 
     If ((.Cells(lRow, 14)) > 0) And (.Cells(lRowLastDemand, 23) > 0) Then 'If Column N Balance > 0 and W has value 
     .Cells(lRowLastDemand, 22) = .Cells(lRow, 14) 'V1 = Balance 
     .Cells(lRowLastDemand, 23) = .Cells(lRowLastDemand, 23) - .Cells(lRow, 14) 'Adjust Col W 
     End If 
     If ((.Cells(lRow, 19)) > 0 And (.Cells(lRowLastDemand, 25) > 0)) Then 'If Column S Balance > 0 and Y has value 
     .Cells(lRowLastDemand, 24) = .Cells(lRow, 19) 'X1 = Balance 
     .Cells(lRowLastDemand, 25) = .Cells(lRowLastDemand, 25) - .Cells(lRow, 19) 'Adjust Col Y 
     End If 
    lRowPortion = c.row + 1 
    End If 
    End If 
    lRow = lRow + 1 
    Loop While lRow <= lRowLast + 1 
End With 
End Sub 
+0

感謝快速回復 我有兩個問題 當U是100 1.在I3中運行您的宏後,我得到Rs 40,我想零和T1是40偉大和U1是60偉大。 我希望餘額I3爲零,因爲Rs 40的金額已經用U1的值進行了調整。 UW Ycolumn是多餘的專欄,消費者提前付款給我們,以便如果他從Rs 50支付了10盧比,那麼我們應該有餘額40,但他已經爲我們預先支付了100盧比,這就是爲什麼Rs 40需要調整&應顯示在T1上並且您的代碼正在工作,但我需要I3列顯示零 –

+0

2.當U 1的值小於餘額 例如I1需求爲50 I2收集爲10並且餘額顯示爲40,但U1價值10然後那Rs 10需要被調整U1應該是0,但與您的代碼我得到U1 -30。 &I3應該是50-集合10&調整10因此I 3應該是30&T1應該是10 同樣的原則應該適用於與UWY列相關的INS列 –

+0

嘗試使用調試器逐步執行代碼。您將能夠看到這些值是什麼以及if語句如何工作。 – dev1998