Problem with variables misbehaving!

crocodile

New Member
Joined
Mar 15, 2019
Messages
8
This is my first post on the forum – so hello to you all and many thanks for reading this.

I am by no means new to Excel/VBA – but have come up against an issue which is making me feel like a real newbie! I sincerely hope someone can tell me where I’m going wrong.

I have an issue with an error check – the easiest way I can explain is to show you the code which DOES work and the code which I’d LIKE to work (but doesn’t!):

For information:
  • H18 is an input field populated by a dropdown list.
  • E30 contains a vlookup the result of which is either a name, tbc or nothing.

This code works:

Code:
Dim NewProjectStatus As String

'Set the variables from the Manage Project form[INDENT]NewProjectStatus = Sheets("Manage Project").Range("H18").Value[/INDENT]
 
'Check to make sure that the a resource has been assigned if the Status is being changed to "In Flight"[INDENT]If NewProjectStatus = "In Flight" And (Sheets("Manage Project").Range("E30").Value = Empty Or Sheets("Manage Project").Range("E30").Value = "tbc") Then
[/INDENT]
[INDENT=2]MsgBox "You cannot set a project to In Flight unless there is an Assigned Resource", vbExclamation
Sheets("Manage Project").Range("H18").ClearContents
End[/INDENT]
[INDENT]End If[/INDENT]
But if I change both values to variables, as follows, it doesn’t work:

Code:
Dim NewProjectStatus As String
[B]Dim AssignedResource As String[/B]
 
'Set the variables from the Manage Project form[INDENT]NewProjectStatus = Sheets("Manage Project").Range("H18").Value
[B]AssignedResource = Sheets("Manage Project").Range("E30").Value[/B][/INDENT]
 
'Check to make sure that the a resource has been assigned if the Status is being changed to "In Flight"[INDENT]If NewProjectStatus = "In Flight" And ([B]AssignedResource [/B]= Empty Or [B]AssignedResource [/B]= "tbc") Then
[/INDENT]
[INDENT=2]MsgBox "You cannot set a project to In Flight unless there is an Assigned Resource", vbExclamation
Sheets("Manage Project").Range("H18").ClearContents
End[/INDENT]
[INDENT]End If[/INDENT]
If E30 is empty the code just doesn’t trap the error.
If E30 is “tbc” it works fine.

Why does this work with E30 defined explicitly, but not if I set it as a variable? I'm clearly missing something - I just don't know what!!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi & welcome to MrExcel.
Try changing Empty to ""
 
Upvote 0
Hi Fluff,

I've tried that and it behaves exactly the same.

I've tried, Empty, Null, Nothing, "" - everything I can think of - but I can't get it to work.

I've even tried splitting it into two separate clauses - one for "" and one for "tbc" - still no joy. :(
 
Upvote 0
That looks ok, Try adding this line
Code:
Dim NewProjectStatus As String
Dim AssignedResource As String
 
'Set the variables from the Manage Project form
   NewProjectStatus = Sheets("Manage Project").Range("H18").Value
   AssignedResource = Sheets("Manage Project").Range("E30").Value
  [COLOR=#ff0000] Debug.Print Len(AssignedResource), "|" & AssignedResource & "|"[/COLOR]
'Check to make sure that the a resource has been assigned if the Status is being changed to "In Flight"
   If NewProjectStatus = "In Flight" And (AssignedResource = Empty Or AssignedResource = "tbc") Then
      MsgBox "You cannot set a project to In Flight unless there is an Assigned Resource", vbExclamation
      Sheets("Manage Project").Range("H18").ClearContents
      End
   End If
and run the code with E30 as ""
What does it say in the immediate window?
 
Upvote 0
In that case try
Code:
If NewProjectStatus = "In Flight" And (AssignedResource = [COLOR=#ff0000]0[/COLOR] Or AssignedResource = "tbc") Then
 
Upvote 0
Ahhhh - now, I'm one step ahead of you here!

Have tried that - and now the error has changed (and this is why I've been banging my head on the wall!!)

Now:

If E30 is Empty (or 0 as we have discovered) the code traps the error. Yay!

However...

If E30 is “tbc” I get a "Run-time Error '13' - Type mismatch" on that line of code. Not yay! :(
 
Upvote 0
I was forgetting that it was a string so it should be "0" rather than 0

I would also recommend changing End to Exit Sub, as End will kill any global variables if you use them.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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