|
-
Sep 8th, 2007, 12:51 PM
#1
Thread Starter
Lively Member
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
-
Sep 8th, 2007, 12:54 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 8th, 2007, 12:57 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 8th, 2007, 01:23 PM
#4
Thread Starter
Lively Member
Re: Opening Excel Workbook with Different Name
 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?
-
Sep 8th, 2007, 01:59 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 8th, 2007, 05:49 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|