IF Then in Excel VB Script

BMD44

Board Regular
Joined
Sep 25, 2019
Messages
72
Hi,

I am trying to Activate a worksheet based on a condition.The condition is the return value of a function.

Private Function ChkValidation()
Dim returnValue As Boolean
returnValue = True

If Range("check_val") = "Failed" Then
MsgBox "validation failed."
returnValue = True
End If

ChkValidation = returnValue
End Function

Based on the return value, I need to activate my worksheet.


If Not ChkValidation = False Then GoTo endSub:

Sheets("Sheet1").Activate

MsgBox "Submission is active"

endSub:

Application.EnableEvents = True
'----------------------
Application.ScreenUpdating = True

When I run the logic, I see 'validation failed' when the return value is true. But, I am not having the sheet Activated when the return value is false.
Please let me if i am missing any logic. I tried adding Else after 'If Not ChkValidation = False Then GoTo endSub:'. That did not work either.

Thanks in advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Your function will always return true.
Try
VBA Code:
Private Function ChkValidation() As Boolean

   ChkValidation = False
   If Range("check_val") = "Failed" Then
      MsgBox "validation failed."
      ChkValidation = True
   End If
End Function
 
Upvote 0
In the calling sub use
VBA Code:
If ChkValidation = False Then GoTo endSub:
or
VBA Code:
If ChkValidation Then GoTo endSub:
depending on which way round you want it to work.
 
Upvote 0
Your function will always return true.
Try
VBA Code:
Private Function ChkValidation() As Boolean

   ChkValidation = False
   If Range("check_val") = "Failed" Then
      MsgBox "validation failed."
      ChkValidation = True
   End If
End Function

This worked. Thank you so much!
 
Upvote 0
In the calling sub use
VBA Code:
If ChkValidation = False Then GoTo endSub:
or
VBA Code:
If ChkValidation Then GoTo endSub:
depending on which way round you want it to work.

Thank you for the suggestion
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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