Can I use VBA to return value based on multiple variables?

Arinoum

Board Regular
Joined
Sep 28, 2016
Messages
64
Below are the conditions and the desired return value if conditions are met:

My table looks like
StatusTypeGM$GM%Dues

<tbody>
</tbody>
Here are the conditions. These conditions only apply if GM$ & GM% are above $25 or 25%. If GM% & GM$ aren't above = or more than 25 the cell should return $0 in dues.
StatusTypeDues
PendingEmp4000
Term<30Emp0
Term<60Emp1333
Term<90Emp2000
PendingC2C1500
Term<30C2C0
Term<60C2C500
Term<90C2C1000

<tbody>
</tbody>


Can I use VBA to assess whether these conditions are met and return the desired value? Example: assuming GM$ & GM% are met at 25 then if status is = Term<60 and Type = Emp then I would like the cell to return $1,333 value. How can I make this possible?
Please help with formula. Thank you!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Without seeing your data, it's hard to know if this will work. However, it worked on some test data I generated.

Put this code in a vba module and run it:
Code:
Sub duesCalculation()
    Dim i As Long, duesValue As Integer
    Dim sh1 As Worksheet, sh2 As Worksheet
    
    Set sh1 = ActiveSheet
    Set sh2 = Worksheets("Sheet2")
    
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        duesValue = 0
        If Cells(i, 3).Value >= 25 And Cells(i, 4).Value >= 25 Then
            Dim searchStr1 As String, searchStr2 As String
            searchStr1 = Cells(i, 1).Value
            searchStr2 = Cells(i, 2).Value
            
            sh2.Activate
            Dim rgSearch As Range
            Set rgSearch = sh2.Columns(1)
        
            Dim cell As Range
            Set cell = rgSearch.Find(searchStr1)
        
            If Not cell Is Nothing Then
                Dim firstCellAddress As String
                firstCellAddress = cell.Address
            
                Do
                    If Range(firstCellAddress).Offset(0, 1).Value = searchStr2 Then
                        duesValue = Range(firstCellAddress).Offset(0, 2).Value
                        Exit Do
                    End If
                    Set cell = rgSearch.FindNext(cell)
                Loop While firstCellAddress <> cell.Address
            End If
        End If
        sh1.Activate
        Cells(i, 5).Value = duesValue
    Next
End Sub
 
Upvote 0
Without seeing your data, it's hard to know if this will work. However, it worked on some test data I generated.

Put this code in a vba module and run it:
Code:
Sub duesCalculation()
    Dim i As Long, duesValue As Integer
    Dim sh1 As Worksheet, sh2 As Worksheet
    
    Set sh1 = ActiveSheet
    Set sh2 = Worksheets("Sheet2")
    
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        duesValue = 0
        If Cells(i, 3).Value >= 25 And Cells(i, 4).Value >= 25 Then
            Dim searchStr1 As String, searchStr2 As String
            searchStr1 = Cells(i, 1).Value
            searchStr2 = Cells(i, 2).Value
            
            sh2.Activate
            Dim rgSearch As Range
            Set rgSearch = sh2.Columns(1)
        
            Dim cell As Range
            Set cell = rgSearch.Find(searchStr1)
        
            If Not cell Is Nothing Then
                Dim firstCellAddress As String
                firstCellAddress = cell.Address
            
                Do
                    If Range(firstCellAddress).Offset(0, 1).Value = searchStr2 Then
                        duesValue = Range(firstCellAddress).Offset(0, 2).Value
                        Exit Do
                    End If
                    Set cell = rgSearch.FindNext(cell)
                Loop While firstCellAddress <> cell.Address
            End If
        End If
        sh1.Activate
        Cells(i, 5).Value = duesValue
    Next
End Sub


Here's what it looks like(image in below link). I highlighted the cells that have the variables in yellow and the green one is where the desired return value should go. The table in the bottom is actually on the 2nd tab sheet1. I would like the formula to read cross the row ex:A8:J8 and check if conditionas are met per the table in sheet 1. If Status says pending and type says Emp and both GM$ and GM% are above $25 then return value in Tab 2 C6. If Status says Term<60 and type says C2C (and GM$ & GM% are above 25) then return value in tab 2 cell C11. I hope that makes sense.


Thanks in advance!

https://imgur.com/RPixrFb
 
Upvote 0
Same code with adjustments to where the data should be looking.

Code:
Sub duesCalculation()
    Dim i As Long, duesValue As Integer
    Dim sh1 As Worksheet, sh2 As Worksheet
    
    Set sh1 = Worksheets("Sheet1")  'change this to the worksheet name with the Status,Client,GM table
    Set sh2 = Worksheets("Sheet2")  'change this to the worksheet name with the status terms
    
    For i = 8 To Cells(Rows.Count, 1).End(xlUp).Row
        duesValue = 0
        If Cells(i, 9).Value >= 25 And Cells(i, 10).Value >= 25 Then
            Dim searchStr1 As String, searchStr2 As String
            searchStr1 = Cells(i, 1).Value
            searchStr2 = Cells(i, 6).Value
            
            sh2.Activate
            Dim rgSearch As Range
            Set rgSearch = sh2.Columns(1)
        
            Dim cell As Range
            Set cell = rgSearch.Find(searchStr1)
        
            If Not cell Is Nothing Then
                Dim firstCellAddress As String
                firstCellAddress = cell.Address
            
                Do
                    If Range(firstCellAddress).Offset(0, 1).Value = searchStr2 Then
                        duesValue = Range(firstCellAddress).Offset(0, 2).Value
                        Exit Do
                    End If
                    Set cell = rgSearch.FindNext(cell)
                Loop While firstCellAddress <> cell.Address
            End If
        End If
        sh1.Activate
        Cells(i, 11).Value = duesValue
    Next
End Sub

Also note that in the image you spelled Pending wrong in the first table. No results would come back with that.

P.S. If the number in the GM% is actually a percentage, then it will be below 1, not above 25. If so, change the code to check for the value to be greater than or equal to 0.25 (e.g., Cells(i, 10).Value >= 0.25).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,414
Members
448,895
Latest member
omarahmed1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top