Slight edit to existing working code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,251
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I am using the code supplied below.
The code works fine to a point,that point being somethings cell B3 will be empty so the line of code shown in red below then pops up an error message.
I will be removing that line of code but can you advise a line of code to replace it so that i then see (2) entered into cell B3

If all is good then my saved pdf will be as follows 2020_05 MAY (2)
I did try this but just got a 2
VBA Code:
Range("B3").Value = (2)


Thanks


Rich (BB code):
Private Sub SecondMonthsSheet_Click()
    Dim strFileName As String
   
       strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\INCOME 2020-2021\" & Range("D3") & "_" & Format(Month(DateValue(Range("A3") & " 1, " & "2020")), "00") & " " & Range("A3") & " " & Range("B3") & " " & ".pdf"
      
    If Dir(strFileName) <> vbNullString Then
        MsgBox "GRASS CUTTING INCOME SHEET " & Range("A3") & " " & Range("B3") & " " & Range("D3") & " WAS NOT SAVED AS IT ALREADY EXISTS", vbCritical + vbOKOnly, "INCOME SUMMARY GRASS SHEET MESSAGE"
   
        Exit Sub
    End If
   
    With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True
        MsgBox "GRASS CUTTING INCOME SHEET " & Range("A3") & " " & Range("B3") & " " & Range("D3") & " WAS  SAVED SUCCESSFULLY", vbInformation + vbOKOnly, "INCOME SUMMARY GRASS SHEET MESSAGE"
        Range("A5:B30").ClearContents
        Range("A5").Select
        Range("B3").Value = Range("B3").Value + 1
        Range("A5:A30").NumberFormat = "@"
        ActiveWorkbook.Save
    End With
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You're welcome & thanks for the feedback.
 
Upvote 0
Sorry im back,
The (2) was entered into cell B3 no problem.
The issue i see is when i run the code shown below i was expecting to see the saved pdf file as 2020_05 MAY (2) but what i did see was 2020_05MAY
As you can see the (2) isnt shown.

Also did i miss something as 2020_05 MAY already exists so i should of see a msg to advise me ?


VBA Code:
Private Sub SecondMonthsSheet_Click()
    Dim strFileName As String
    
       strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\INCOME 2020-2021\" & Range("D3") & "_" & Format(Month(DateValue(Range("A3") & " 1, " & "2020")), "00") & " " & Range("A3") & " " & Range("B3") & " " & ".pdf"
      
    If Dir(strFileName) <> vbNullString Then
        MsgBox "GRASS CUTTING INCOME SHEET " & Range("A3") & " " & Range("B3") & " " & Range("D3") & " WAS NOT SAVED AS IT ALREADY EXISTS", vbCritical + vbOKOnly, "INCOME SUMMARY GRASS SHEET MESSAGE"
    
        Exit Sub
    End If
    
    With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True
        MsgBox "GRASS CUTTING INCOME SHEET " & Range("A3") & " " & Range("B3") & " " & Range("D3") & " WAS  SAVED SUCCESSFULLY", vbInformation + vbOKOnly, "INCOME SUMMARY GRASS SHEET MESSAGE"
        Range("A5:B30").ClearContents
        Range("A5").Select
        Range("B3").Value = "(2)"
        Range("A5:A30").NumberFormat = "@"
        ActiveWorkbook.Save
    End With
End Sub
 

Attachments

  • 6889.jpg
    6889.jpg
    20.4 KB · Views: 2
Upvote 0
As you cannot have two files with the same name in a folder, then the file names are not the same.
 
Upvote 0
Ok i did know that but one had a space, so now its corrected.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,566
Members
449,318
Latest member
Son Raphon

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