Is it possible to create a new workbook from within vba with some extra bits..

SiEn

New Member
Joined
Mar 28, 2023
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hello all, wonder if anyone can help. before i delve to deep into this i will try and explain what i am after..
I have a userform that returns results based on the form back onto a sheet at this point it also generates the next ID number.
From this i then have a workbook used as a base model that will then be copied into this reference ID and used. (This is my job book for each job I do)
just thinking of trying to save time here...

Is it possible when i enter those details and it generates that ID if its possible to copy and rename a workbook (so the base workbook) into the generated ID format.

If that is, is also then possible to create an automatic hyperlink to that workbook from the ID field within excel.?

I fully appreciate i may be asking a bit much here..

Any help gratefully received.

Simon
 
If you find out about macros and Sharepoint please post to this thread so I can see the response(s).
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I read that you have to provide the open command with the URL address to access files on Sharepoint. I do not use Sharepoint so I cannot test.
 
Upvote 0
so because i sync sharepoint to my desktop i get a local copy that it uploads so i have used that path, however i dont know how i could change that path for other users who will be on our sharepoint site, is there anyway of coding the username somehow? i mean i could potentially add a box of the user entering to potentially fill this bit in?

Code Changed...
VBA Code:
ThisWorkbook.Path & "\"

to

"C:\Users\my.name\my.company.name\Order Book - General\"

This now completly works for me.
 
Upvote 0
I read that you have to provide the open command with the URL address to access files on Sharepoint. I do not use Sharepoint so I cannot test.
I couldnt get anything to work, i tried about 20 different options i could find on the web!
 
Upvote 0
Right so I need some more help and I have a feeling you will be able to Jim... (Fingers Crossed)
So I now have an input box on my userform that the raiser inputs their name from a drop down, this name is the correct path for the user part... however when i run it with the value from the box it just inputs it as text (as it would as its in quotes) how do i manipulate the value to that part of the path...

VBA Code:
 ----------------------------
'         Initializations
'   ----------------------------
    
'   Path to/folder containing the jobs workbooks.
    sJobWorkbookPath = "C:\Users\Me.cbInputName.Value\XXXXX\Order Book - General\"
    
'   "Base" file name for the job file that will be created.
    sJobWorkbookBaseName = "Quote "

'   File name extension for the job file that will be created.
    sJobWorkbookNameExt = ".xlsm"

'   Full name of the Job file to create.
    sJobWorkbookName = sJobWorkbookBaseName & psID & sJobWorkbookNameExt
    
'   Path to/folder containing the template file.
    sTemplateWorkbookPath = "C:\Users\Me.cbInputName.Value\XXXXX\Order Book - General\"

'   Name of the template workbook.
    sTemplateWorkbookName = "QuoteSheetMaster.xlsm"
    
'   Worksheet in this workbook that is changing.
    Set wsTargetWorksheet = prChangedCell.Parent

The XXXXX is my company name (Hidden)
The Me.cbInputName.Value part is the value from the drop down combi box... can i split the path up to somehow get that text value in there?
Simon
 
Upvote 0
I cannot find the cbInputName dropdown in the forms that are in the workbook that I worked on. Did you add that after you got my code? Do you mean that there are quote marks in the name as it comes from the form?

If I understand what you are after you need to feed the name (for the path) into my sub. Here is what that would look like. Note the third parameter sent into the sub named psNamePath.

VBA Code:
' ----------------------------------------------------------------
' Procedure Name: MakeNewJobWorkbook
' Purpose: Creat new job workbook, add hyperlink to the file into cell whose content changed.
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter psID (String): This is the job ID input by user.
' Parameter prChangedCell (Range): The cell whose value was changed.
' Author: Jim
' Date: 4/20/2023
' ----------------------------------------------------------------

Sub MakeNewJobWorkbook(psID As String, prChangedCell As Range, psNamePath As String)

'   ----------------------------
'          Declarations
'   ----------------------------
    
'   Worksheet in this workbook that user is changing.
    Dim wsTargetWorksheet As Worksheet

'   Path to the job files.
    Dim sJobWorkbookPath As String

'   The "base" name of the job workbook to save.
    Dim sJobWorkbookBaseName As String
    
'   The file name extension for the job workbook to save.
    Dim sJobWorkbookNameExt As String
    
'   Name of the the job workbook -- before ID is added -- to be created with extension.
    Dim sJobWorkbookName As String
    
'   Location of/path to the template file.
    Dim sTemplateWorkbookPath As String
    
'   File name -- with file name extension -- of the template workbook.
    Dim sTemplateWorkbookName As String
    
'   Variable used to message user.
    Dim sMsg As String
    
'   Used for user response to questions.
    Dim vAns As Variant
    
'   ----------------------------
'         Initializations
'   ----------------------------
    
'   Path to/folder containing the jobs workbooks.
    sJobWorkbookPath = "C:\Users\" & psNamePath & "\XXXXX\Order Book - General\"
    
'   "Base" file name for the job file that will be created.
    sJobWorkbookBaseName = "Job "

'   File name extension for the job file that will be created.
    sJobWorkbookNameExt = ".xlsx"

'   Full name of the Job file to create.
    sJobWorkbookName = sJobWorkbookBaseName & psID & sJobWorkbookNameExt
    
'   Path to/folder containing the template file.
    sTemplateWorkbookPath = ThisWorkbook.Path & "\"

'   Name of the template workbook.
    sTemplateWorkbookName = "QuoteBookTemplate.xlsx"
    
'   Worksheet in this workbook that is changing.
    Set wsTargetWorksheet = prChangedCell.Parent
    
'   -----------------------------------------
'         Handle Job File Already Exists
'   -----------------------------------------
    
'   Check if the job workbook to be saved already exists and whether user wants to replace it.
    If Dir(sJobWorkbookPath & sJobWorkbookName) <> "" _
     Then
        sMsg = "The file named " & sJobWorkbookName & " exists. Overwrite?"
        
        vAns = MsgBox(sMsg, vbYesNo)
        
'       If user said no to overwrite then bail out of sub.
        If vAns = vbNo Then Exit Sub
        
'       Delete the file before creating a new version.
        Kill (sJobWorkbookPath & sJobWorkbookName)
        
    End If

'   Open the template workbook. It becomes the ActiveWorkbook.
    Workbooks.Open sTemplateWorkbookPath & sTemplateWorkbookName
    
'   -----------------------------
'         Save New Job File
'   -----------------------------
    
'   Save copy of the template whose name is
    With ActiveWorkbook
        .SaveAs sJobWorkbookPath & sJobWorkbookName
        .Close
    End With
    
'   ----------------------------------------------------
'         Add Link That Opens the New Job Workbook
'   ----------------------------------------------------
   
'   With the worksheet into which the new ID was placed...
    With wsTargetWorksheet
            
'       Activate it
        .Activate
    
'       Then place hyperlink to the newly created job workbook file
'       into the cell into which the new ID was input.
        .Hyperlinks.Add Anchor:=prChangedCell, _
            Address:=sJobWorkbookPath & sJobWorkbookName, _
            TextToDisplay:=psID
            
         prChangedCell.Value = psID
    
    End With
    
End Sub

Call the sub like this.

VBA Code:
Call MakeNewJobWorkbook(P & nextID, ActiveCell.Offset(0, 1), Me.cbInputName.Value)
 
Upvote 0
Jim, you sir are a total legend! works and every user has managed to add a job and get a copy etc....

This is exactly what I wanted... now I wonder if I could ask another question about this...

Would it be possible to have a sub folder for each Type (At the moment Type1, Type2, Type3, Type4, Type5 and Type 6) which is gained from the cbType field and the copy be put in there and the hyperlink to that location? i.e seperate out all of my quotes into the individial folders (This is something i have just thought of) also when that book opens for the quotes. You now have renamed it as per the quote ref (Ref ID) which is perfect, it has just dauned on me that there is potentially a couple of boxes that could be filled from that submission.... would that be possible?
For instance when the quotebook opens with the new ref (eg M-00001)
Cell F3 contains the RefID
Cell F5 would contain the data from txtJobTitle
Cell F7 would contain the data from cbEstimator

Not sure how easy that is, i am going to have a play tonight with it, I will also upload the latest working copies (incl my stripped down quote sheet)

I simply cannot thank you enough for all of this hard work you have done, it blows me away the support you have offered.

Simon
 
Upvote 0
I hope that I got this right. Here is modified code to address the type subfolder.

VBA Code:
Option Explicit

' ----------------------------------------------------------------
' Procedure Name: MakeNewJobWorkbook
' Purpose: Creat new job workbook, add hyperlink to the file into cell whose content changed.
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter psID (String): This is the job ID input by user.
' Parameter prChangedCell (Range): The cell whose value was changed.
' Parameter psNamePath (String): The name of the person used in the path.
' Parameter psTypePath (String): The job type used in the path.
' Author: Jim
' Date: 4/20/2023
' ----------------------------------------------------------------

Sub MakeNewJobWorkbook( _
    psID As String, _
    prChangedCell As Range, _
    psNamePath As String, _
    psTypePath As String)

'   ----------------------------
'          Declarations
'   ----------------------------
    
'   Worksheet in this workbook that user is changing.
    Dim wsTargetWorksheet As Worksheet

'   Path to the job files.
    Dim sJobWorkbookPath As String

'   The "base" name of the job workbook to save.
    Dim sJobWorkbookBaseName As String
    
'   The file name extension for the job workbook to save.
    Dim sJobWorkbookNameExt As String
    
'   Name of the the job workbook -- before ID is added -- to be created with extension.
    Dim sJobWorkbookName As String
    
'   Location of/path to the template file.
    Dim sTemplateWorkbookPath As String
    
'   File name -- with file name extension -- of the template workbook.
    Dim sTemplateWorkbookName As String
    
'   Variable used to message user.
    Dim sMsg As String
    
'   Used for user response to questions.
    Dim vAns As Variant
    
'   ----------------------------
'         Initializations
'   ----------------------------
    
'   Path to/folder containing the jobs workbooks.
    sJobWorkbookPath = "C:\Users\" & psNamePath & "\XXXXX\Order Book - General\" & psTypePath & "\"
    
'   "Base" file name for the job file that will be created.
    sJobWorkbookBaseName = "Job "

'   File name extension for the job file that will be created.
    sJobWorkbookNameExt = ".xlsm"

'   Full name of the Job file to create.
    sJobWorkbookName = sJobWorkbookBaseName & psID & sJobWorkbookNameExt
    
'   Path to/folder containing the template file.
    sTemplateWorkbookPath = "C:\Users\" & psNamePath & "\XXXXX\Order Book - General\"

'   Name of the template workbook.
    sTemplateWorkbookName = "QuoteSheetMaster.xlsm"
    
'   Worksheet in this workbook that is changing.
    Set wsTargetWorksheet = prChangedCell.Parent
    
'   -----------------------------------------
'         Handle Job File Already Exists
'   -----------------------------------------
    
'   Check if the job workbook to be saved already exists and whether user wants to replace it.
    If Dir(sJobWorkbookPath & sJobWorkbookName) <> "" _
     Then
        sMsg = "The file named " & sJobWorkbookName & " exists. Overwrite?"
        
        vAns = MsgBox(sMsg, vbYesNo)
        
'       If user said no to overwrite then bail out of sub.
        If vAns = vbNo Then Exit Sub
        
'       Delete the file before creating a new version.
        Kill (sJobWorkbookPath & sJobWorkbookName)
        
    End If

'   Open the template workbook. It becomes the ActiveWorkbook.
    Workbooks.Open sTemplateWorkbookPath & sTemplateWorkbookName
    
'   -----------------------------
'         Save New Job File
'   -----------------------------
    
'   Save copy of the template whose name is
    With ActiveWorkbook
        .SaveAs sJobWorkbookPath & sJobWorkbookName
        .Close
    End With
    
'   ----------------------------------------------------
'         Add Link That Opens the New Job Workbook
'   ----------------------------------------------------
   
'   With the worksheet into which the new ID was placed...
    With wsTargetWorksheet
            
'       Activate it
        .Activate
    
'       Then place hyperlink to the newly created job workbook file
'       into the cell into which the new ID was input.
        .Hyperlinks.Add Anchor:=prChangedCell, _
            Address:=sJobWorkbookPath & sJobWorkbookName, _
            TextToDisplay:=psID
            
         prChangedCell.Value = psID
    
    End With
    
End Sub

Call the modified sub like this.

VBA Code:
    Call MakeNewJobWorkbook( _
        P & nextID, _
        ActiveCell.Offset(0, 1), _
        Me.cbInputName.Value, _
        Me.cbType.Value)
 
Upvote 0
Jim, again thank you, I have got everything working, had to make a couple of changes to the hyperlink for SharePoint and managed to get the data I need on the new created workbook.

Will attach what i have shortly
 
Upvote 0

Forum statistics

Threads
1,215,637
Messages
6,125,964
Members
449,276
Latest member
surendra75

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