Sumtimes code works other times it doesnt

Hlatigo

Well-known Member
Joined
Jun 3, 2002
Messages
677
This peice of code is just suppose to create the new month and year if the directory doesnt exist. At times when I test, it will create the path but then if I try to run again, it will not save if the month already exists. I took this piece of code from a posting but havent been able to get it to work smoothly. any ideas?

thanks so much!

Code:
    prmptyear = Format(x1, "YYYY")
    prmptmonth = Format(x1, "MMM")
    fpath = "S:\USA-HOUSTON\FAMM\jcb39tfm\Bunker Price Reporting" & "\" & prmptyear & "\" & prmptmonth     '____
    fname1 = "BunkerPR_" & Format(x1, "mmddyyyy")                               
    fwrkbk1 = fname1 & ".xls"                                                            
    filenme1 = fpath & "\" & fwrkbk1                                                     
    


If Len(Dir(fpath)) > 0 Then
        ActiveWorkbook.SaveAs Filename:=filenme1
    
    Else
        MkDir Len(Dir(fpath))
    End If
    
    ActiveWorkbook.SaveAs Filename:=filenme1
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
so i have made the following changes based on another posting and I seem to even get a worse result. hahaha I am going backwards here :(


Code:
Sub createdated()
    Dim x1
    
    
    
     x1 = Sheets("Main").Range("G7").Value
     
     If x1 = "" Then
        MsgBox ("Please Enter Date")
        End
     End If
     
    prmptyear = Format(x1, "YYYY")
    prmptmonth = Format(x1, "MMM")
    fpath = "S:\USA-HOUSTON\FAMM\jcb39tfm\Bunker Price Reporting" & "\" & prmptyear & "\" & prmptmonth
    fname1 = "BunkerPR_" & Format(x1, "mmddyyyy")
    fwrkbk1 = fname1 & ".xls"
    filenme1 = fpath & "\" & fwrkbk1
    
     
    If Len(Dir(fpath)) > 0 Then
        MkDir Len(Dir(fpath))
    End If
    
    ActiveWorkbook.SaveAs Filename:=filenme1
 
Upvote 0
Not sure if this is what you wanted
Code:
Sub test()
Dim myDate As Date, myDir As String, fn As String
With Sheets("Main").Range("G7")
    If (.Value = "") + (Not IsDate(.Value)) Then
        MsgBox "Please Enter Date"
        Exit Sub
    End If
    myDate = .Value
End With
myDir = "S:\USA-HOUSTON\FAMM\jcb39tfm\Bunker Price Reporting\" & _
          Year(myDate) & "\" & MonhtName(Month(myDate), True)
fn = BunkerPR_"  & Format$(myDate, mmddyyyy") & ".xls" 
If Dir(myDir, vbDirectory) = "" Then MkDir myDir
ActiveWorkbook.SaveAs myDir & "\" & fn
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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