VBA: If blank cell do nothing, if not continue with code

diegogda

New Member
Joined
May 11, 2018
Messages
5
I have this code which filters for "RET" and then goes to next active cell. But in case "RET" is not found in the list, the next active cell will be blank and I want it to do nothing and continue on another part of the code and if it is not blank I want it to replace "RET" with 0:

Code:
ActiveSheet.Range("$A$1:$T$1000000").AutoFilter Field:=8, Criteria1:="RET"    ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 8).Select
    ActiveCell.FormulaR1C1 = "0"
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.End(xlUp).Select
    ActiveSheet.Range("$A$1:$T$1000000").AutoFilter Field:=8
    Selection.End(xlToRight).Select
    Range("S2").Select
ActiveCell.FormulaR1C1 = _
        "=+(RC[-4]*RC[-3]*(1+(RC[-11]/100)))*(1-(RC[-16]/100))"

So, in this part it looks for "RET" and then finds next active cell after the filter:

Code:
ActiveSheet.Range("$A$1:$T$1000000").AutoFilter Field:=8, Criteria1:="RET"
    ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 8).Select

If the active cell is blank I want it to continue here:

Code:
    Selection.End(xlUp).Select
    ActiveSheet.Range("$A$1:$T$1000000").AutoFilter Field:=8
    Selection.End(xlToRight).Select
    Range("S2").Select
ActiveCell.FormulaR1C1 = _
        "=+(RC[-4]*RC[-3]*(1+(RC[-11]/100)))*(1-(RC[-16]/100))"

If not blank I want it to continue with business as usual!

Thank you very much!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
If I've understood you correctly, replace your code with
Code:
   Range("H:H").Replace "RET", "0", xlWhole, , False, , False, False
   Range("S2").FormulaR1C1 = _
   "=+(RC[-4]*RC[-3]*(1+(RC[-11]/100)))*(1-(RC[-16]/100))"
 
Upvote 0
I am a VBA amateur but I should have thought of that anyway.

Works like a charm! Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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