SaveAs with Default File Name

mharper90

Board Regular
Joined
May 28, 2013
Messages
117
Office Version
  1. 365
Platform
  1. MacOS
I'm trying to Save the current file, then Save As to a new filename (for the new quarter/period) prior to clearing the data from the previous quarter/period. I want the user to be prompted for the SaveAs filename and path, but I'd like a default filename to populate in the prompt window using data from a user form (I know this user form is not in the current code below, but I can fix that easily). I'd like to use Me.TextBox1 (year) and Me.TextBox2 (period/quarter #), such as "TLD 2018 P4". Btw, the "TLD" and "P" are not included in the textbox values.

I found a GetSaveAsFilename sample in the Excel Help, but it doesn't work. I keep getting an error that highlights the "fName =" line, and I think I recall it saying that it is a mismatch error. Also, this sample doesn't do anything about a default filename. Thanks for the help!

Code:
Sub TLDChangeOut()Dim fName As Long
    Dim msg As String
Dim Lr As Long
Dim ws1 As Worksheet:  Set ws1 = ThisWorkbook.Sheets("Main Data")

Lr = ws1.Range("A" & Rows.Count).End(xlUp).Row



    
    msg = "Are you sure?@@All TLD numbers and issue/collectiondates will be permanently deleted!"
    
    Select Case MsgBox(Replace(msg, "@", vbCrLf), vbYesNoCancel + vbCritical, "TLD Changeout Confirm")
        
Case Is = vbYes
            ActiveWorkbook.Save
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
            ActiveWorkbook.SaveAs Filename:=fName
            Sheets("Main Data").Range("C7:D" & Lr, "G7:G" & Lr).ClearContents

        Case Is = vbNo, vbCancel
    End Select

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Not to have a one-sided conversation with myself, but I figured this out eventually on my own (with some Googling). I'll post my code here when I have a chance so that others might learn from it.
 
Upvote 0
For the reference of anyone needing this:

This is the code that worked for me, and replaces the contents of the "Case Is = vbYes" statement above.

Code:
Dim msgValue

ActiveWorkbook.Save
msgValue = MsgBox("Select file location for new TLD Period on next window.", vbOKOnly, "New TLD Period")
fName = Application.GetSaveAsFilename( _[INDENT]InitialFileName:="TLD " & Me.TextBox1.Text & " P" & Me.TextBox2.Text, FileFilter:="(*.xlsm), *xlsm", Title:="Save New TLD Period")[/INDENT]
If fName <> False Then
[INDENT]ActiveWorkbook.SaveAs Filename:=fName
Sheets("Main Data").Range("C7:D" & Lr, "G7:G" & Lr).ClearContents[/INDENT]
Else
End If
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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