Save WorkSheet as per Cell VALUE (keep original open, and do not open new)

TTUK

Board Regular
Joined
Apr 5, 2012
Messages
137
Hello all,

I am trying to achieve the following within VBA.

1. Save specific worksheet (IIR_temp) to a location shown in the cell in A1 on that page, which is a reference from another sheet =IIR_data!5 which looks like (\\server\report\test.xls)
2. Then have the sheet just be saved, and not to override the current spreadsheet which is open


At present this is what I have:

ActiveSheet.Copy ' Copies active sheet to a new workbook
ActiveSheet.SaveAs Range("A1").Value, xlOpenXMLWorkbookMacroEnabled


I just don't know how to get the formula to save the certain sheet in the specified location shown in A1, and allow me to return as I was in the original spreadsheet.

Hope someone can help me out on this.

Thank you!
 
What sheet is active when you run that macro?

I start the macro by being on the Register tab.
When I click in a row with the value 'Raise Follow-up Form' it starts, and links in to the code which I pasted above Call Followup_Confirm


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Selection.Count = 1 Then
    
    If Target.Value = "Raise Follow-up Form" Then
    
        If Not Intersect(Target, Range("AY7:AY99999")) Is Nothing Then
          
          Dim lMaxRows As Long


            If Not Intersect(Target, Columns("AY")) Is Nothing Then
                 Range(Cells(Target.Row, "A"), Cells(Target.Row, "BA")).Copy
                 With Sheets("IIR_data")
                  lMaxRows = .Cells(Rows.Count, "A").End(xlUp).Row
                .Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
                Application.CutCopyMode = False


                End With
                End If
                        
[B]           Call Followup_Confirm[/B]
        End If
    End If
End If
       
End Sub

Hope this makes sense?

Thanks,
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Can't see anything obviously wrong with the code.
Have you checked that the file path exists & that you have Read/Write access to that folder?
Whilst this may take some time, it would be worth searching the network for the file name, incase it has been saved to the wrong place
 
Upvote 0
Hi Fluff,

Have checked and double-checked.
No file on the server saved with the filename.
Read/Write access is enabled.

It creates the specific new worksheet (Book#), however it just doesn't name the file nor save to server.

Thanks,
 
Upvote 0
Ok try this
Code:
Sub Followup_Confirm()
    Dim Msg As String, Ans As Variant

    Msg = "Confirm you would like to raise a Follow-up form"

    Ans = MsgBox(Msg, vbYesNo)

    Select Case Ans

    Case vbYes

        ' Copy date and paste in cell on row
        ActiveCell.FormulaR1C1 = "Form Raised"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=R4C52"

        Selection.Copy

        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False

        ' Select the worksheet that is to be saved
        Sheets("IIR_temp").Select
        
        ActiveSheet.Copy ' Copies active sheet to a new workbook
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Range("A1").Value & Range("B1").Value, 56
        MsgBox ActiveWorkbook.FullName
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
         
    End Select
End Sub
It will come up with a msgbox showing the full path/filename before closing.
What does the msgbox say & is it what you expect?
 
Upvote 0
When I run the above code I get a Run-time error '9' Subscript out of range;
I select Debug

It has highlighted - With Worksheets("Register") from the following code

Code:
Public Sub Create_Links()


    Dim lastRow As Long
    Dim cell As Range
    
    'Create hyperlinks in column AW for each cell in column AW starting at AW8
    
[B]    With Worksheets("Register")[/B]
        lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        For Each cell In .Range("A8:A" & lastRow)
            .Hyperlinks.Add anchor:=.Cells(cell.Row, "AW"), Address:="", TextToDisplay:="Create " & cell.Value & " folder"
        Next
    End With
    
End Sub

This code is always running to detect if entry into column B, then show create folder link in corresponding Row in column AW.

Maybe there is a different way this could be done?

Thank you,
 
Upvote 0
As I haven't changed anything on that code, it's something that has changed at your end.
Do you have a sheet called "Register"?
 
Upvote 0
Yes there is a Sheet called Register where the whole process begins.
I have gone through the process below, and have highlighted each tab referenced.

It starts with the Register tab.
A user enters text within the Register (giant table of data), and on each new row in column B, it will execute the below automatically, column B consists of a drop-down which a user must select, hence the below is linked to that cell. I want the links to be automatically created when a user begins to start a new entry.

The Create_Links is whereby it automatically creates a folder upon the server in a specific location with the value in column A for that particular row.

Code:
Public Sub Create_Links()

    Dim lastRow As Long
    Dim cell As Range
    
    'Create hyperlinks in column AW for each cell in column AW starting at AW8
    
    With Worksheets("Register")
        lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        For Each cell In .Range("A8:A" & lastRow)
            .Hyperlinks.Add anchor:=.Cells(cell.Row, "AW"), Address:="", TextToDisplay:="Create " & cell.Value & " folder"
        Next
    End With
    
End Sub

When the user has gotten to the end of that row, there will be a cell with the text "Raise Follow-up Form" if it is required. (after the user has created the folder)
When confirmed it takes the row the user is on, on the Register tab and pastes it to IIR_data tab, which holds that 1 line of data which has been copied.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Selection.Count = 1 Then
    
    If Target.Value = "Raise Follow-up Form" Then
    
        If Not Intersect(Target, Range("AY7:AY99999")) Is Nothing Then
          
          Dim lMaxRows As Long


            If Not Intersect(Target, Columns("AY")) Is Nothing Then
                 Range(Cells(Target.Row, "A"), Cells(Target.Row, "BA")).Copy
                 With Sheets("IIR_data")
                  lMaxRows = .Cells(Rows.Count, "A").End(xlUp).Row
                .Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
                Application.CutCopyMode = False


                End With
                End If
                        
           Call Followup_Confirm
        End If
    End If
End If
       
End Sub

Which also triggers Followup_Confirm, as the below:

Code:
Sub Followup_Confirm()    Dim Msg As String, Ans As Variant


    Msg = "Confirm you would like to raise a Follow-up form"


    Ans = MsgBox(Msg, vbYesNo)


    Select Case Ans


    Case vbYes


        ' Copy date and paste in cell on row
        ActiveCell.FormulaR1C1 = "Form Raised"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=R4C52"


        Selection.Copy


        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False


        ' Select the worksheet that is to be saved
        Sheets("IIR_temp").Select
        
        ActiveSheet.Copy ' Copies active sheet to a new workbook
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Range("A1").Value & Range("B1").Value, 56
        MsgBox ActiveWorkbook.FullName
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
         
    End Select
End Sub

When selected YES to the "Confirm Follow-up" - the data upon the IIR_data is linked to the IIR_temp
This is a template form created from the IIR_data, basically it references cells to complete form in specific locations for the user (IIR_temp is an incident form, taking information from the data on IIR_data (the copied row)).

I want the IIR_temp (the incident form) to be saved to the server within the location on IIR_temp cell A1, with the file name for that document to be from IIR_temp cell B1.


From what I can tell, when I run the macro... because the Public Sub Create_Links (first code box) is Public it runs all the time? Not sure if this is the case, I'm still learning.
But, when the new workbook is created (which your code does do!) it doesn't complete the Range A1 or B1 for the file location or file name or save as, it just jumps back to the Public Create_Links and throws up an error.
However, as this is a new worksheet I'm not sure how it could still be linked, but is is because I have the Main Spreadsheet still open, I believe it still just wants to run the Public Create_Links?


I need to have the Main Spreadsheet open and not close, hence the IIR_temp file is created and saved in its desired location.

I hope this makes more sense to the process?

Thank you,
 
Last edited:
Upvote 0
because the Public Sub Create_Links (first code box) is Public it runs all the time?
No it has to be run manually or called from another sub.

Also the Create_Links code does not create any folders.
From this
it just jumps back to the Public Create_Links
You must have other event code somewhere in there that is probably affecting things.
You can try
Code:
Sub Followup_Confirm()
Dim Msg As String, Ans As Variant

[COLOR=#0000ff]Application.EnableEvents = False[/COLOR]
    Msg = "Confirm you would like to raise a Follow-up form"


    Ans = MsgBox(Msg, vbYesNo)


    Select Case Ans


    Case vbYes


        ' Copy date and paste in cell on row
        ActiveCell.FormulaR1C1 = "Form Raised"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=R4C52"


        Selection.Copy


        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False


        ' Select the worksheet that is to be saved
        Sheets("IIR_temp").Select
        
        ActiveSheet.Copy ' Copies active sheet to a new workbook
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Range("A1").Value & Range("B1").Value, 56
        MsgBox ActiveWorkbook.FullName
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
         
    End Select
[COLOR=#0000ff]Application.EnableEvents = True[/COLOR]
End Sub
but it may cause further problems.
 
Upvote 0
No it has to be run manually or called from another sub.

Okay, is that then manually ran when I select a cell in column B?

Also the Create_Links code does not create any folders.
From this You must have other event code somewhere in there that is probably affecting things.

Yes, sorry, this code here creates the folders;

Code:
Public Sub Create_Folder_and_Link(ByVal Target As Hyperlink)
    
    Dim folder As String
    Dim cell As Range
    
    'Extract folder name XXXXX from the TextToDisplay string which has the format "Create XXXXX folder"
    
    folder = Trim(Split(Target.TextToDisplay, " ")(1))
    If Right(MAIN_FOLDER, 1) = "\" Then
        folder = MAIN_FOLDER & folder & "\"
    Else
        folder = MAIN_FOLDER & "\" & folder & "\"
    End If
    
    'Create folder if it doesn't exist
    
    If Dir(folder, vbDirectory) = "" Then MkDir folder


    'Add in column U cell the hyperlink which opens this folder
    
    With Worksheets("Register")
        lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        For Each cell In .Range("A8:A" & lastRow)
            With Worksheets(Target.Range.Parent.Name)
                .Hyperlinks.Add anchor:=.Cells(Target.Range.Row, "AX"), Address:=folder, TextToDisplay:="Open " & cell.Value & " folder"
             End With
         Next
    End With
End Sub


You can try
Code:
Sub Followup_Confirm()
Dim Msg As String, Ans As Variant

[COLOR=#0000ff]Application.EnableEvents = False[/COLOR]
    Msg = "Confirm you would like to raise a Follow-up form"


    Ans = MsgBox(Msg, vbYesNo)


    Select Case Ans


    Case vbYes


        ' Copy date and paste in cell on row
        ActiveCell.FormulaR1C1 = "Form Raised"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=R4C52"


        Selection.Copy


        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False


        ' Select the worksheet that is to be saved
        Sheets("IIR_temp").Select
        
        ActiveSheet.Copy ' Copies active sheet to a new workbook
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Range("A1").Value & Range("B1").Value, 56
        MsgBox ActiveWorkbook.FullName
        ActiveWorkbook.Close
        Application.DisplayAlerts = True
         
    End Select
[COLOR=#0000ff]Application.EnableEvents = True[/COLOR]
End Sub
but it may cause further problems.

I have tried the code and it throws up the same error with this bit of code below.

Always on the line:
Code:
 With Worksheets("Register")

Code:
Public Sub Create_Links()


    Dim lastRow As Long
    Dim cell As Range
    Application.DisplayAlerts = True
    'Create hyperlinks in column AW for each cell in column AW starting at AW8
    
    With Worksheets("Register")
        lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
        For Each cell In .Range("A8:A" & lastRow)
            .Hyperlinks.Add anchor:=.Cells(cell.Row, "AW"), Address:="", TextToDisplay:="Create " & cell.Value & " folder"
        Next
    End With
    
End Sub

Is there another way that the spreadsheet would check if a value is in column B, and then add
Code:
 .Hyperlinks.Add anchor:=.Cells(cell.Row, "AW"), Address:="", TextToDisplay:="Create " & cell.Value & " folder"
?

Maybe if I knew another way to do this it would fix the error.

Thanks,
 
Last edited:
Upvote 0
Would you be willing to share your workbook via OneDrive, or DropBox?
As I suspect you have other code in there that is affecting things.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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