Runtime error for savefile

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
There's this one line of code that throws runtime error at me
VBA Code:
        wbNew.SaveAs Filename:=wbaddress & "/" & wbname2 & ".xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
, but the best thing is I used to run this line of code without a problem when i had premade buttons on the sheet. Now when my buttons are being created "on the fly" when new sheet is being created it suddenly stopped running, can anyone help me figure out what happened?

1665146147298.png
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Please paste all of the code from this sub into a post (not a picture). Very often a problem originates somewhere besides the line raising the error.

What are the values of wbaddress and wbname2 at the point the error occurs? Usually a SaveAs runtime error is caused by an invalid path or filename. This may have nothing at all to do with your buttons.
 
Upvote 0
Ye shoudl've done that in the first place. here you go
VBA Code:
Sub ZapiszWchmurze_Click()
    Dim wbname As String, wbaddress As String
    Dim wbOrig As Workbook, wbNew As Workbook
    Dim shtNames As Variant, sName As Variant
    Application.ScreenUpdating = False
    Set wbOrig = ThisWorkbook
    wbname2 = InputBox("Podaj nazwę", "Nazwa pliku")
    wbname = ActiveSheet.Name
    wbaddress = wbOrig.Worksheets("PANEL WYCEN").Range("H17").Value
    shtNames = Array("MAG", "SZABLON_SZAFA", "KARTA REALIZACJI", "OFERTA", wbname)
  
    Set wbNew = Workbooks.Add(xlWBATWorksheet)  'Create new wb with a single blank sheet
  
    For Each sName In shtNames
        wbOrig.Worksheets(sName).Copy After:=wbNew.Worksheets(wbNew.Sheets.Count)
    Next sName
  
    Application.DisplayAlerts = False
        wbNew.Worksheets(1).Delete
        'wbOrig.Worksheets(wbname).Delete
      
        wbNew.SaveAs Filename:=wbaddress & "/" & wbname2 & ".xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        'wbNew.Close savechanges:=False             '<--- Uncomment this if you want to close the new workbook
        wbNew.Worksheets("MAG").Visible = xlSheetVeryHidden
        wbNew.Worksheets("KARTA REALIZACJI").Visible = xlSheetVeryHidden
        wbNew.Worksheets("SZABLON_SZAFA").Visible = xlSheetVeryHidden
        ActiveSheet.Buttons(sheetName & "_" & "Button 3").Visible = True

       
        'wbNew.Close savechanges:=False             '<--- Uncomment this if you want to close the new workbook
      
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
  
End Sub
and also for refference value/path provided in Sheets("PANEL WYCEN").Range("H17") - wbadress = C:\Users\artpr\OneDrive - ART-PRO Sp. z o.o\Dokumenty\Wyceny
 
Upvote 0
Why are you using a forward slash?
 
Upvote 0
Why are you using a forward slash?
This is the way path is created using this routine, placed onto "Browse" button
VBA Code:
Sub browse_Click()

Dim SharedFolder As FileDialog
Set SharedFolder = Application.FileDialog(msoFileDialogFolderPicker)
With SharedFolder
    .Title = "Wybierz folder zapisu"
    If .show <> -1 Then GoTo NoSel
    Sheets("PANEL WYCEN").Range("H17").Value = .SelectedItems(1)
NoSel:
End With
End Sub
 
Upvote 0
No, you put it in there:

Code:
wbaddress & "/" & wbname2
 
Upvote 0
I think i got lost in all the problems today regarding my excel file. I thougth you were reffering to my edit that provided pth as reference to mentione drange H17.
Truth be told I have no idea why I did that - made a quick fix just now. Nevertehless the issue continues regardless.
 
Upvote 0
VBA Code:
        wbNew.SaveAs Filename:=wbaddress & "/" & wbname2 & ".xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

What is the value of wbname2 just before this line of code is executed? You will get a runtime error if the value of wbname2 is not a valid Windows file name.
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,234
Members
448,951
Latest member
jennlynn

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