If Statement VBA Search and Output Help

KSolid

New Member
Joined
Mar 7, 2018
Messages
5
Hello All,

I have currently have trouble developing a VBA/macro that would do a search in a data range within a table and once search it will populate another range with the information I need.
Below I have an example of what I am trying to do but can't seem to wrap my head around

AB
StatusResults
SoldProfit
On SaleLoss
Sold TodayProfit
Sold YesterdayProfit
InventoryStable

<tbody>
</tbody>

The if statement would be for if the range in Status had the word "Sold" it will output "Profit" in the Result range, else it would look for "On Sale" which would output "Loss". So on and so on until the if statement were to end.

Thank you so much in advance for the help and assistance. Feel free to post if I am coming across as unclear.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try:
Code:
Sub KSolid()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*Sold*"
    Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Profit"
    Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*On Sale*"
    Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Loss"
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub KSolid()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*Sold*"
    Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Profit"
    Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*On Sale*"
    Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Loss"
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub

My Good Sir you are one amazing individual, it is currently working well. Is there a way for it to filter out Blank data in the Status range. For example. If status had no information it will output "No data" in the Result range.
 
Upvote 0
My Good Sir you are one amazing individual, it is currently working well. Is there a way for it to filter out Blank data in the Status range. For example. If status had no information it will output "No data" in the Result range.

I encountered a minor bug I believe. For example if I don't happen to have "Sold" in the range, it would not run the rest of the VBA, so it will not skip over it rather it would give me an error message.
 
Upvote 0
I'll get back to you soon.
 
Last edited:
Upvote 0
Try:
Code:
Sub KSolid()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    If WorksheetFunction.CountIf(Range("A2:A" & LastRow), "*Sold*") <> 0 Then
        Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*Sold*"
        Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Profit"
    End If
    If WorksheetFunction.CountIf(Range("A2:A" & LastRow), "*On Sale*") <> 0 Then
        Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*On Sale*"
        Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Loss"
    End If
    Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="="
    Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "No Data"
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub KSolid()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    If WorksheetFunction.CountIf(Range("A2:A" & LastRow), "*Sold*") <> 0 Then
        Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*Sold*"
        Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Profit"
    End If
    If WorksheetFunction.CountIf(Range("A2:A" & LastRow), "*On Sale*") <> 0 Then
        Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="*On Sale*"
        Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "Loss"
    End If
    Range("A1:B" & LastRow).AutoFilter Field:=1, Criteria1:="="
    Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible) = "No Data"
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub

mumps You have need a giant help, it is working perfectly!! Thank you so much!
 
Upvote 0
mumps You have need a giant help, it is working perfectly!! Thank you so much!
Sorry misspelled it was meant to say You were a giant help. Had something else on my mind at the same time. Once again Mumps thank you so much for your help!
 
Upvote 0
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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