Code ignores my If code statement

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,731
Office Version
  1. 2007
Platform
  1. Windows
I am trying to check if a checkbox is true & if so do something.

My invoice is printed & i see the message INVOICE HAS NOW BEEN SAVED.
It is now i wish the code to see if Checkbox2 has a tick in it.
So a basic check & a Msgbox pop to be shown saying HI.

Ive run this code many times & just dont see the Hi meessage at all.
The code continues & clears the contents
Why is the code ignoring my If code ???

This is the end of my code.
Everything works fine & does what the code asks its to BUT

VBA Code:
    With Sheets("INV")
      Worksheets("INV").Activate
    End With
      
     'ActiveWindow.SelectedSheets.PrintOut copies:=1 'INVOICE NOW PRINTED
      MsgBox "INVOICE HAS NOW BEEN SAVED", vbInformation, "INVOICE PRINT OK MESSAGE"
      
      If CheckBox2 = True Then
      MsgBox "HI"
      End If
    
      Range("L4").Value = Range("L4").Value + 1
      Range("G27:L36").ClearContents
      Range("G39:G40").ClearContents
      Range("G46:G50").ClearContents
      Range("G13").ClearContents
      CheckBox1 = False
      CheckBox2 = False
      Range("G13").Select
      ActiveWorkbook.Save
      

    End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Assuming your code is in a regular sub
If it is a Forms checkbox try
VBA Code:
If ActiveSheet.Shapes("CheckBox2").ControlFormat.Value = 1 Then
If it is an ActiveX checkbox try
VBA Code:
If ActiveSheet.OLEObjects("CheckBox2").Object.Value = True Then

P.S.
Your With statement here
VBA Code:
    With Sheets("INV")
      Worksheets("INV").Activate
    End With

is doing nothing, just use

VBA Code:
Worksheets("INV").Activate
 
Last edited:
Upvote 0
Hi,
Its active x on a sheet.
I now get the message If without end if.
Where do i put the end if as in various places its ignored or told end if with if

VBA Code:
     'ActiveWindow.SelectedSheets.PrintOut copies:=1 'INVOICE NOW PRINTED
      MsgBox "INVOICE HAS NOW BEEN SAVED", vbInformation, "INVOICE PRINT OK MESSAGE"
      
      If ActiveSheet.OLEObjects("CheckBox2").Object.Value = True Then
      Workbooks.Open ("C:\Users\Ian\Desktop\REMOTES ETC\DR\EXCEL WORKSHEETS\MOTORCYCLES.xlsm")
      Worksheets("INVOICES").Activate
      With Sheets("INVOICES")
     .Range("G3").Select
      Call HYPERLINKMC
      
      End With
      Range("L4").Value = Range("L4").Value + 1
      Range("G27:L36").ClearContents
      Range("G39:G40").ClearContents
      Range("G46:G50").ClearContents
      Range("G13").ClearContents
      CheckBox1 = False
      CheckBox2 = False
      Range("G13").Select
      ActiveWorkbook.Save
    
    End Sub
 
Upvote 0
According to the way your code is written, put
VBA Code:
End If
directly below
VBA Code:
ActiveWorkbook.Save
 
Upvote 0
Well i did try that but them most of the code in between wasnt actioned.
Whilst waiting for a reply from original post i worked on just getting my code working of which i have now.
So i just need to trigger it to either do what i want or ignore it.

This is why i had the code look to see if Checkbox 2 was True.

Maybe this is another way.
When checkbox 2 is true there is a value in cell G39

Can we have the code look to see if a value is in cell G39 then do what i require.
No value then thats it.
 
Upvote 0
VBA Code:
If Range("G39") <> "" Then
 
Upvote 0
Does this look correct before i make the move to try

Basically if a value is in cell G39 then do RED CODE ONLY

If no value is in cell G39 then do BLUE CODE ONLY



Rich (BB code):
      MsgBox "INVOICE HAS NOW BEEN SAVED", vbInformation, "INVOICE PRINT OK MESSAGE"
      
      If Range("G39") <> "" Then
        
      Workbooks.Open ("C:\Users\Ian\Desktop\REMOTES ETC\DR\EXCEL WORKSHEETS\MOTORCYCLES.xlsm")
      Worksheets("INVOICES").Activate
      With Sheets("INVOICES")
     .Range("G3").Select
      Call HYPERLINKMC
      End With
      Range("L4").Value = Range("L4").Value + 1
      Range("G27:L36").ClearContents
      Range("G39:G40").ClearContents
      Range("G46:G50").ClearContents
      Range("G13").ClearContents
      CheckBox1 = False
      CheckBox2 = False
      Range("D1").Select
      ActiveWorkbook.Save
      
      Else
      
      Range("L4").Value = Range("L4").Value + 1
      Range("G27:L36").ClearContents
      Range("G39:G40").ClearContents
      Range("G46:G50").ClearContents
      Range("G13").ClearContents
      CheckBox1 = False
      CheckBox2 = False
      Range("D1").Select
      ActiveWorkbook.Save
      
    End Sub
 
Upvote 0
You forgot the "End If". Try:
VBA Code:
Sub Test()
    MsgBox "INVOICE HAS NOW BEEN SAVED", vbInformation, "INVOICE PRINT OK MESSAGE"
    If Range("G39") <> "" Then
         Workbooks.Open ("C:\Users\Ian\Desktop\REMOTES ETC\DR\EXCEL WORKSHEETS\MOTORCYCLES.xlsm")
         Worksheets("INVOICES").Activate
         With Sheets("INVOICES")
        .Range("G3").Select
         Call HYPERLINKMC
         End With
         Range("L4").Value = Range("L4").Value + 1
         Range("G27:L36").ClearContents
         Range("G39:G40").ClearContents
         Range("G46:G50").ClearContents
         Range("G13").ClearContents
         CheckBox1 = False
         CheckBox2 = False
         Range("D1").Select
         ActiveWorkbook.Save
    Else
        Range("L4").Value = Range("L4").Value + 1
        Range("G27:L36").ClearContents
        Range("G39:G40").ClearContents
        Range("G46:G50").ClearContents
        Range("G13").ClearContents
        CheckBox1 = False
        CheckBox2 = False
        Range("D1").Select
        ActiveWorkbook.Save
    End If
End Sub
If you look at my code, you will see that I use indentation to mark the beginning and end of the "If....Then" section of code. I use indentation all the time when writing code because it helps me to see if I have missed an "End If" or if I have missed a "Next" statement in a "For....Next" loop or an "End With" in a "With...End With". I would suggest that you try to use indentation as well to help guide you when writing code.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

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