Please advise on my IF code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Below is part of my full code.
It should print an invoice then ask if it printed ok.
If YES then continue to do the part in Red.

BUT if NO then print again & then do the part in RED

Currently both the print options work BUT the part in Red does nothing


Rich (BB code):
    With ActiveSheet
      'ActiveWindow.SelectedSheets.PrintOut copies:=1 'INVOICE NOW PRINTED
      MsgBox "1st Print"
    End With
      answer = MsgBox("INVOICE HAS NOW BEEN SAVED" & vbNewLine & vbNewLine & "DID THE INVOICE PRINT OK FOR YOU ?", vbInformation + vbYesNo, "INVOICE PRINT OK MESSAGE")
    If answer = vbNo Then
      'ActiveWindow.SelectedSheets.PrintOut copies:=1 'INVOICE PRINTED AGAIN IF FIRST PRINT WAS POOR
      MsgBox "2nd Print"
      Range("L4").Value = Range("L4").Value + 1
      Range("G27:L36").ClearContents
      Range("G46:G50").ClearContents
      Range("L18").ClearContents
      Range("G13").ClearContents
      Range("G13").Select
      ActiveWorkbook.Save
    End If
       
    End With

    End Sub
 
Hi,
I put it all works now so meaning it was just printing. I don’t / can’t explain correctly so it was supposed to explain that the fill code I supplied all works where before trying to add the advice code kept given me errors. So the issue I had was the printing part at the end.

Been trying to put this together now for say 4 days by coupling together bits & bobs.

I made a few posts but either didn’t get a reply or when I did there was an issue & original poster didn’t reply again.

I needed this to work as it’s used pretty much each day so when I was putting it together & didn’t get an error I assumed it was correct ha ha ha

I’m still open to advice if you have time Thanks.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I'm afraid that I can't see any reason why the code @6StringJazzer posted in post number 6 wouldn't work for you, as it works for me every time i.e. when I click no on the answer message box I get msgbox "2nd Print" come up each time.

Did you step through the code with F8 so you see what is happening?
 
Upvote 0
See what happens when you press yes.
What I would expect, it skips MsgBox "2nd Print", it then runs the red block and clears the contents of the cells.
You sure that you are running the code in post 6?
 
Upvote 0
Yes that is what I used.
If you put the full code here that I supplied & then add yours to it thus removing my last part of the code. I will try it & report back with what happens.

I couldn’t get it to work hence why I opted for the one print then clear contents.
 
Upvote 0
then add yours
Post 6 is the post by @6StringJazzer not me and the code goes at the end of yours and so it doesn't affect it as long as you are on the right sheet when you reach the code (see my earlier remarks about using the sheet variables rather than ActiveSheet when coding)
If you put the full code here that I supplied & then add yours to it thus removing my last part of the code. I will try it & report back with what happens.
Why do I need to put the code here!!!!
 
Upvote 0
Just in case I leave something out then I get if & with errors.
 
Upvote 0
You can delete code and paste in the code by @6StringJazzer yourself as it is the last code in your sub, make sure that you have the correct sheet active before it reaches the code
 
Upvote 0
The sheet that is active is the sheet where the cells are cleared of which is INV sheet.
 
Upvote 0
So im now up & looking at it

I add this at the top but told duplicate declaration in current scope but when looking there isnt another the same so i leave it out.
Rich (BB code):
Dim answer As Long

I am then told i need to add 1 x End With so i add it at the bottom

My code in use now looks like the below & typical it now works

Rich (BB code):
Private Sub Print_Invoice_Click()
    Dim answer As Integer
    Dim rng As Range
    Dim cell As Range
    Dim MyFile As String
    Dim findString As String
    Dim sPath As String, strFileName As String
    Dim srcWS As Worksheet, destWS As Worksheet
    Set srcWS = ActiveWorkbook.Worksheets("INV")
    Set destWS = ActiveWorkbook.Worksheets("DATABASE")
    
    With ActiveSheet
    If Range("G13") = "" Then
      MsgBox "NO NAME SELECTED IN THE CUSTOMER DETAILS SECTION", vbCritical, "NO CUSTOMER SELECTED MESSAGE"
      Range("G13").Select 'CHECKING IF CUSTOMER IS SELECTED
    Exit Sub
    End If
  
    If Range("L18") = "" Then
      MsgBox ("PLEASE SELECT A PAYMENT TYPE "), vbCritical, "PAYMENT TYPE WAS NOT SELECTED"
      Range("L18").Select 'CHECKING IF PAYMENT TYPE HAS BEEN SELECTED
    Exit Sub
    End If
        
    strFileName = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\" & Range("L4").Value & ".pdf"
    If Dir(strFileName) <> vbNullString Then
      MsgBox "INVOICE " & Range("L4").Value & " WAS NOT SAVED AS IT ALLREADY EXISTS" & vbNewLine & vbNewLine & "PLEASE CHECK FILE IN FOLDER THAT WILL NOW OPEN.", vbCritical + vbOKOnly, "INVOICE NOT SAVED MESSAGE"
      VBA.Shell "explorer.exe /select, " & "" & strFileName & "", vbNormalFocus 'DUPLICATE INVOICE FOUND
    Exit Sub
    End If
    
    strFileName = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\" & Range("L4").Value & ".pdf"
    With ActiveSheet
      .ExportAsFixedFormat Type:=xlTypePDF, fileName:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
    End With 'CURRENT INVOICE IS NOW SAVED
    
    With ActiveSheet
      .ExportAsFixedFormat Type:=xlTypePDF, fileName:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
    End With 'CURRENT INVOICE IS NOW SAVED
    
    With Sheets("DATABASE")
      Worksheets("DATABASE").Activate
    End With
    
    Set rng = ActiveSheet.Columns("A:A")
      findString = Worksheets("INV").Range("G13").Value
    Set cell = rng.Find(What:=findString, LookIn:=xlFormulas, _
      LookAt:=xlWhole, MatchCase:=False) ' CUSTOMER FOUND IN COLUMN A
    
    If cell Is Nothing Then
      MsgBox "NO CUSTOMER WAS FOUND"
    Else
    With Sheets("DATABASE")
      cell.Select
      ActiveCell.Offset(0, 15).Select ' CUSTOMERS CELL IN COLUMN P NOW SELECTED
    End With
    End If
    
    If Len(ActiveCell.Value) <> 0 Then
      ValueInInvoiceCell.Show 'MESSAGE SHOWN IF CUSTOMERS INVOICE CELL IN COLUMN P HAS A VALUE IN IT

    Exit Sub
    Else
      TransferInvoiceNumber.Show 'NOW ENTER INVOICE NUMBER IN CUSTOMERS CELL IN COLUMN P & NOW HYPERLINKED
    End If
    
    With Sheets("INV")
      Worksheets("INV").Activate
    End With
      
        'ActiveWindow.SelectedSheets.PrintOut copies:=1 'INVOICE NOW PRINTED
        MsgBox "1st Print"
 
  
    answer = MsgBox("INVOICE HAS NOW BEEN SAVED" & vbNewLine & vbNewLine & "DID THE INVOICE PRINT OK FOR YOU ?", vbInformation + vbYesNo, "INVOICE PRINT OK MESSAGE")
  
    If answer = vbNo Then
        'ActiveWindow.SelectedSheets.PrintOut copies:=1 'INVOICE PRINTED AGAIN IF FIRST PRINT WAS POOR
        MsgBox "2nd Print"
   End If


   Range("L4").Value = Range("L4").Value + 1
   Range("G27:L36").ClearContents
   Range("G46:G50").ClearContents
   Range("L18").ClearContents
   Range("G13").ClearContents
   Range("G13").Select
   ActiveWorkbook.Save
   End With
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

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