Have File saved from name found in specific cell

Papi

Well-known Member
Joined
May 22, 2007
Messages
1,592
I found this code on another site and it works fine as it is. What is needed to have it look at in worksheet Inventory in cell B42 rather than having to enter the name?

Code:
Option Explicit
 
Sub KillPreviousFile()
    Dim szMsgResponse As String
     
     '   Get the name of this workbook with out the .xlsm
    Dim szDefaultName As String
    szDefaultName = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
     
     
StartAgain:
     '   Use an input box to obtain the new file name:
    Dim szNewBookName As String
    szNewBookName = InputBox("Please enter a name for the new file" & _
    vbNewLine & _
    "It will be saved in the same directory as the original" & vbNewLine & _
    vbNewLine & _
    "Valid file-names cannot include these characters" & vbNewLine & _
    "< > \ / * ? | : ; """, , szDefaultName)
     
     
     '   If a name has been specified:
    If szNewBookName <> Empty Then
         
         
         '       Suppress messages
        Application.DisplayAlerts = False
         
         
         '       Determine old workbooks path and name and store for later use
        Dim szOldBook As String
        szOldBook = ThisWorkbook.FullName
         
         
         '       Create a valid path for our new file, same directory as this file
        Dim szThisPath As String
        szThisPath = ThisWorkbook.Path & "\"
         
         
         '       Build our new file name
        Dim szNewFileName As String
        szNewFileName = szThisPath & szNewBookName & ".xlsm"
         
         
         '       If the user typed in the same name as the original, we have some options
         '       we can present, by either starting the procedure over, saving the file,
         '       or canceling the procedure entirely
        If szNewFileName = szOldBook Then
             
             
             '           Variable szMsgResponse holde the msgbox button press:
            szMsgResponse = MsgBox("The new file name is the same as the original" & _
            vbNewLine & "Would you like to save now, try again, or cancel?", 19)
             
             '           Proceed based on the selected option
            Select Case szMsgResponse
            Case 2
                Exit Sub
            Case 7
                GoTo StartAgain
            Case 6
                ThisWorkbook.Save
                Exit Sub
            End Select
             
             
        End If
         
         
         '       If we are valid, save this file under the new name:
        On Error GoTo ExitProc
        ThisWorkbook.SaveAs szNewFileName, xlWorkbookNormal
         
         
         '       Then remove the old workbook we just were using
        Kill szOldBook
         
         
    Else
         
         
         '       if nothing was given in the input box, just exit.
        Exit Sub
         
    End If
     
ExitProc:
    Application.DisplayAlerts = True
    Exit Sub
     
InvalidName:
    MsgBox Err.Description
    GoTo ExitProc
End Sub
 
That worked. The only difference is obviously it is .xls but the file size is 150k verses the first file that was 68kb
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Can't understand why.....I tried the code and created 3 files all ended up about 50k, very little data in them though !

HAve you done a CTRL + End on the worksheets in the new workbook, to see if there isn't any bloat involved ??
 
Upvote 0
They are an even match as to where the data stops in both columns and rows.
 
Upvote 0
Can't really help much more.....is workbook sharing turned off in the "new" workbook ??
 
Upvote 0
I am not sure on workbook sharing as I have never dabbled with that. Thanks so very much Michael for all your help. You have kicked this a long way and I really appreciate that on your part.
 
Upvote 0
I am not sure on workbook sharing as I have never dabbled with that. Thanks so very much Michael for all your help. You have kicked this a long way and I really appreciate that on your part.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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