Facing issue with my Code to save files with name from reference

allExcell

Board Regular
Joined
Jan 8, 2013
Messages
71
Hi All,

I need your help in fixing the following code, not sure why it's not working the way I want it to be. Please see below for more details:

In the following code I'm trying to save files with specific file name and the value has to be taken from Sheet1, cell N2 of the Original file.
Can you please let me know why it's not happening and guide me accordingly? I'm badly in need of your help.
Code:
Sub Copy_NewBook()
    Dim sPath As String
    Dim twbk As Workbook
    Dim oWbK As Workbook
    Dim Sh  As Variant
Dim FName As String
Dim FPath As String
 
    sPath = "C:\Users\xxxx\Desktop\Reports\Template.xlsx"
    Set twbk = ThisWorkbook
    Set oWbK = Workbooks.Open(sPath)
    
    
        For Each Sh In twbk.Worksheets
        If Sh.Name <> "CC" Then
     GoTo 12
        Else
        GoTo 10
    End If
12        Sh.Range("B5", Sh.Range("B5").End(xlToRight).End(xlDown)).Copy
        oWbK.Worksheets(Sh.Name).Range("B3").PasteSpecial Paste:=xlPasteValues
        Next Sh
     
'File name has to be with file Owner's name, which is to be taken from below address, i.e. Sheet1, N2 cell ref.
FName = twbk.Sheets("sheet1").Range("N2").Value
FPath = "C:\Users\xxxx\Desktop\Reports\Uploads\"

10    oWbK.SaveAs FileName:=FPath & FName & "_" & Format(Now(), "yyyymmdd  hhmm AMPM"), FileFormat:=51, Password:="", _
        WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
      
    oWbK.Close saveChanges:=False
'    MsgBox ("Report Generated !!!")
    
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I would add & ".xlsx" in the SaveAs command, as the last part of the filename.
If this would not help, use F8 to step through the code and inspect the contents of the variables.
 
Upvote 0
Hi Wigi,

I'm not good in coding and still in the process of learning. I made few changes in the code as follows:

Code:
Dim oWbK As Workbook
Dim ws1 As Worksheets
Set ws1 = oWbK.Sheets("Sheet1") 'This is direct the cell ref. to Sheet1.N2//Else if I give N2 alone as range it picks up a wrong value from from active 'sheet


Finally at the end, i wanted to refer to sheet1.N2 for teh value to set the file name.
Code:
10    oWbK.SaveAs FileName:="C:\Users\xxxx\Desktop\Reports\Uploads\xx_" & ws1.Range("N2").Value & "_" & Format(Now(), "yyyymmdd"), FileFormat:=52, Password:="", _
        WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

but, this way I'm getting Run time Error 91 as Obj var or With Block Varnot set, highlighting teh above given code.
 
Upvote 0
What are the exact contents of ws1.Range("N2").Value ?
 
Upvote 0
Hi wigi, it has the names of the owners, so the files would be saved with their name along with Date.
 
Upvote 0
Did you already try to add the ".xlsx" at the end of the filename ?
 
Upvote 0
Hi Wigi, there is a small correction, please see below:
Code:
Dim ws1 As Worksheets
    Set ws1 = twbk.Sheets("Sheet1")
twbk is the workbook I'm copying from.

For .xlsx thing, I've tried something, please refer to my below codes for more details:

Code:
Sub Copy_NewBook()
    Dim sPath As String
    Dim twbk As Workbook
    Dim oWbK As Workbook
    Dim Sh  As Variant
    
    sPath = "C:\Users\xxxx\Desktop\Reports\Template.xlsx"
    Set twbk = ThisWorkbook
    Set oWbK = Workbooks.Open(sPath)
    
    
        For Each Sh In twbk.Worksheets
        If Sh.Name <> "CC" Then
     GoTo 12
        Else
        GoTo 10
    End If
12        Sh.Range("B5", Sh.Range("B5").End(xlToRight).End(xlDown)).Copy
        oWbK.Worksheets(Sh.Name).Range("B3").PasteSpecial Paste:=xlPasteValues
        Next Sh
     
    Dim strCellREF2Txt As String
    Dim strFILEname As String
    strCellREF2Txt = twbk.Sheets("Sheet1").Range("N2").Value
    strFILEname = "C:\Users\xxxx\Desktop\Reports\Uploads\xx_" & strCellREF2Txt & ".XLSX"
    Application.DisplayAlerts = False
[B]10    ActiveWorkbook.SaveAs FileName:=strFILEname, _
        FileFormat:=xlExcel8, CreateBackup:=False
[/B] Application.DisplayAlerts = True
End Sub

Here I'm getting runtime error: 1004, while it's highlighting bold lines.
 
Last edited:
Upvote 0
In this topic, you already used 3 types of FileFormat's in the SaveAs command: 51, 52, xlExcel8 (56).
Which one do you need?
 
Upvote 0
Then put the 51 in the code and do the tests.
If it does not work, use F8 to step through the code, line by line. See what happens (or does not happen).
 
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,654
Members
449,462
Latest member
Chislobog

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