Getting a runtime error when trying to pass date from userform to filename save as String

richieH

New Member
Joined
Jan 22, 2018
Messages
3
Hi ,

I am hoping that somebody can point me in the write direction, I am trying to use a user form to allow the user to select a date either in cell B6 or if they want todays date.

Depending on the selection then this is passed through to a save as macro and used as a string. the save as works with out this new addition but fails with a
runtime.PNG

The end of this error confusing to me as the date i am using is in the format dd-mm-yy so it can be used in a file name.

Here is my code it is the
VBA Code:
Private Sub btnOk_Click()
    ' Set Values
    
Dim theDate As Date
    theDate = Format(Date, "dd-mm-yy")
   

Dim Project As String
Dim Name As String
Dim Sheet As String
Dim Version As String
Dim SaveString As String
Dim ws As Worksheet
Dim TD As Date

If Me.OptionButton1.Value Then
        theDate = Date
    ElseIf Me.OptionButton2.Value Then
        theDate = ActiveSheet.Range("B6")
        
        End If



Set ws = ActiveSheet

Project = ActiveSheet.Range("B1").Value
Name = ActiveSheet.Range("B2").Value
Sheet = ActiveSheet.Name
Version = ws.Cells(5, Columns.Count).End(xlToLeft)
TD = theDate 'Format(Date, "dd-mm-yy")
'Rows(4).Find("*", ws.[A4], xlFormulas, xlByColumns, xlPrevious) 'ActiveSheet.Range("B3").Value

'Sheet = Left(Sheet, Len(Sheet) - 2) 'Left(filelist, Len(filelist) - 2)


SaveString = Project & "_" & Name & "_" & Sheet & "_" & TD   '& "v" & Version

'Application.ActiveWorkbook.Path &

ActiveWorkbook.Saveas filename:=SaveString, FileFormat:=52      '52 for .xlsm

    ' Unload Form
    Unload Me
End Sub

it fails as this
ActiveWorkbook.Saveas filename:=SaveString, FileFormat:=52 '52 for .xlsm


any help appreciated as i can not for the life of me work it out


 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try with a different Dim for theDate:
VBA Code:
Dim theDate As String
 
Upvote 0
Hi Anthony ,

I managed to work out how to do it. i am not sure what the first is not formatting the date corectly.

VBA Code:
Dim theDate As Date
    theDate = Format(Date, "dd-mm-yy")

What i have done is use the variable defined in the time as i had before when i was just using todays date

VBA Code:
TD = theDate 'Format(Date, "dd-mm-yy")

becomes
VBA Code:
TD = Format(theDate, "dd-mm-yy")

Thanks for your response
 
Upvote 0
I didn't understand if you "fixed it" or not, and how

In the filename you need to use Strings; a Date has its own format and you can see that the file name (in the error message) included a 29\06 (in the format of dir and sub dir) rather than "29-06-22"
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,838
Members
449,343
Latest member
DEWS2031

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