Results 1 to 6 of 6

Thread: Opening Excel Workbook with Different Name

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    79

    Opening Excel Workbook with Different Name

    Hi I need to re-use code as much as possible. I need to use the same procedure to open three different workbooks (one at a time) but it is not working.

    This is what I have
    Code:
    Public Sub AddRow(RowData As String, NameFile As String)
        Dim oXLApp As Excel.Application       
        Dim oXLBook As Excel.Workbook
        Dim objExcelCI As Excel.Chart
        Dim oXLsheet As Excel.Worksheet
          
        Set oXLApp = New Excel.Application  'Create a new instance of Excel
        Set oXLBook = oXLApp.Workbooks.Open("C:\log\NameFile.xls")  
        Set oXLsheet = oXLBook.Worksheets(1)
    
    End Sub
    How should I define NameFile (String?) so that I could use it in this line
    Set oXLBook = oXLApp.Workbooks.Open("C:\log\NameFile.xls")
    with one of three names I have.
    How should I call this function from any other sub?

    Thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Opening Excel Workbook with Different Name

    NameFile needs to be string like you have it but also used as a variable, which it is but not in your open statement, by escaping the double quotes like so...
    Code:
    Set oXLBook = oXLApp.Workbooks.Open("C:\log\" & NameFile & ".xls")
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Opening Excel Workbook with Different Name

    Also, you shouldnt be creating a new excel application object everytime you call the funciton. It will really be slow and take up resources.

    Dimension the variables that need to persist in a module and just test to see if they are created yet or not and create based upon need.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    79

    Re: Opening Excel Workbook with Different Name

    Quote Originally Posted by RobDog888
    NameFile needs to be string like you have it but also used as a variable, which it is but not in your open statement, by escaping the double quotes like so...
    Code:
    Set oXLBook = oXLApp.Workbooks.Open("C:\log\" & NameFile & ".xls")
    NameFile As String ' is this correct?

    Code:
    Public Function ProcessBuffer()
    
    Dim NameFile As String
    
    NameFile = AllData
        AddRow sNewVariable, NameFile
    End Function
    If I call the AddRow function to open a File named AllData, it gives an error highlighting AllData, saying "Variable not defined"
    How should I define AllData when is a constant file name?
    How should I do this?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Opening Excel Workbook with Different Name

    1. Yes
    2. Const AllData As String = "Blah"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Opening Excel Workbook with Different Name

    if alldata is a constant namefile will always be the same value, so will always open the same file, you need to make namefile = to the name of the correct workbook each time.

    if you want to process the same 3 files every time then possibly pass a string or array of the 3 file names, then make a loop in the addrow sub to open each of the 3 files in turn
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width