If Range("AF1") = "#N/A" Then

jimmisavage

Board Regular
Joined
Jun 28, 2017
Messages
130
I have a simple VBA to hide and unhide cells based in a formula in AF1

Code:
If Range("AF1") = "Non Commercial" Then
        Columns("P").EntireColumn.Hidden = True
        Columns("Q").EntireColumn.Hidden = True
        Columns("R").EntireColumn.Hidden = True
    End If
If Range("AF1") = "Commercial" Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
    End If

This does the job but there is also an instance when AF1 can be #N/A which throws an error because i've not told it that would be an option.

I've tried adding an option of below but it doesn't seem to recognise the #N/A
Code:
If Range("AF1") = "#N/A" Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
     End If

How would I combat this?

Many thanks
Jimmi
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If that's an error value, not text, you can use:

Code:
If Range("AF1").Value = CVErr(xlerrna) Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
     End If

Note that that test has to be before the other two.
 
Upvote 0
Thanks Rory,
I think I've gone wrong somewhere now. Your code works fine and fixed the issue I had but now i get an error on the next If Range (Non-Commercial):

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("AF1") = CVErr(xlErrNA) Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
    End If
    If Range("AF1") = "Non Commercial" Then
        Columns("P").EntireColumn.Hidden = True
        Columns("Q").EntireColumn.Hidden = True
        Columns("R").EntireColumn.Hidden = True
    End If
    If Range("AF1") = "Commercial" Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
    End If
    End Sub
 
Upvote 0
Code:
If Range("AF1") = "Non Commercial" Then
    Columns("P:R").EntireColumn.Hidden = True
Else
    Columns("P:R").EntireColumn.Hidden = False
End If
 
Last edited:
Upvote 0
This seems to have worked though. Thanks for the help guys! :)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("AF1") = CVErr(xlErrNA) Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
    Else
    If Range("AF1") = "Non Commercial" Then
        Columns("P").EntireColumn.Hidden = True
        Columns("Q").EntireColumn.Hidden = True
        Columns("R").EntireColumn.Hidden = True
    Else
    If Range("AF1") = "Commercial" Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
    End If
    End If
    End If
    End Sub
 
Upvote 0
You need to nest them using ElseIf:

Code:
    If Range("AF1") = CVErr(xlErrNA) Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
    ElseIf Range("AF1") = "Non Commercial" Then
        Columns("P").EntireColumn.Hidden = True
        Columns("Q").EntireColumn.Hidden = True
        Columns("R").EntireColumn.Hidden = True
    ElseIf Range("AF1") = "Commercial" Then
        Columns("P").EntireColumn.Hidden = False
        Columns("Q").EntireColumn.Hidden = False
        Columns("R").EntireColumn.Hidden = False
    End If
 
Upvote 0
I spoke too soon! Now this only works if AF1 is #N/A. If it's not it won't move on to check if it's non-commercial or commercial :(
 
Upvote 0
Would it not be better to change your formula so that it returns "No Match" (say) instead of returning an error?
=IFERROR(your formula, "No Match")

Then you should be able to use the code per post #4
 
Upvote 0
Would it not be better to change your formula so that it returns "No Match" (say) instead of returning an error?
=IFERROR(your formula, "No Match")

Then you should be able to use the code per post #4

I can certainly try that but I can't make the code out of VBA. I know this is complicated but how to i convert this into a VBA formula?

Code:
=ISERROR(VLOOKUP(AE1,'Study Database'!B8:AN1001,9,0),"No Match")

Something like this?
Code:
"=ISERROR(VLOOKUP(RC[-1],'Study Database'!R[7]C[-30]:R[1000]C[8],9,0),"No Match")"
It's not working if that is correct.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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