[VBA] IF And Statement not working

NessPJ

Active Member
Joined
May 10, 2011
Messages
422
Office Version
  1. 365
Hi there,

For some reason i have an IF AND statement that is giving me trouble. I have tried several other examples on the internet that seem to work...yet the statement i wrote will not work properly. When 1 of the 2 statements if TRUE the code is already executed (which should only happen when both are TRUE).

Can anyone help me please?

Code:
Private Sub Tabelkopieren()


Dim TabelX1C1 As String
Dim TabelX1C2 As String


TabelX1C1 = Sheets("Sheet1").Range("L4").Value
TabelX1C2 = Sheets("Sheet1").Range("L5").Value


    If (Len(TabelX1C1) = 0) And (Len(TabelX1C2) = 0) Then
    GoTo Leeg
    Else
    'Do Nothing
    End If
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Re: [VBA] IF And Statement nog working

Hi there. There doesnt seem to be anything wrong with your code. As written, it checks that both L4 and L5 are empty. Is that what you are expecting? In the VB editor, try putting a breakpoint on the if statement and see what the values of TabelX1C1 and TabelX1C2 really are at that point.
 
Upvote 0
Re: [VBA] IF And Statement nog working

Hi there. There doesnt seem to be anything wrong with your code. As written, it checks that both L4 and L5 are empty. Is that what you are expecting? In the VB editor, try putting a breakpoint on the if statement and see what the values of TabelX1C1 and TabelX1C2 really are at that point.

I read the values from the immediate window.
I tested with: L4 = "" and L5 = "1"

Result from VBA editor:

TabelX1C1 = ""
TabelX1C2 = "1"
Len(TabelX1C1) = 0
Len(TabelX1C2) = 1

The statement is being continued. While i would expect in this case it would execute "GoTo Leeg".
Which...to be clear is fairly simple:

Code:
Leeg:


    MsgBox ("It appears there is no data entered. Execution halted!"), vbCritical, "No data!"
    
    Sheets("Sheet1").Protect ("1234")
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    Exit Sub
 
Upvote 0
Re: [VBA] IF And Statement nog working

It will only hit the GoTo Leeg line if both are 0, if you want to execute that line if either are 0 then change And to Or
 
Upvote 0
Re: [VBA] IF And Statement nog working

You should change the AND to OR if thats the result you want.
 
Upvote 0
Re: [VBA] IF And Statement nog working

It will only hit the GoTo Leeg line if both are 0, if you want to execute that line if either are 0 then change And to Or

You are absolutely right! Thanks for the help!
 
Upvote 0
Re: [VBA] IF And Statement nog working

Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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