?
Solved

calling excel files from visual basic

Posted on 2002-06-30
13
Medium Priority
?
354 Views
Last Modified: 2013-11-25
i have about 25 master excel files, which i want to call from a single vb form. also whenever , i call one of these files, only a copy of that files should be activated, so that even if i make changes, it will not affect the master file. after doing some changes, i want to save the modified file as a different name.

0
Comment
Question by:kamal3965
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 7121054
Hi kamal3965,

Save all your excel master files as excel templates
then let your program load them all up in a combo or listbox
and then use a bit of code like

Private Sub Command1 Click()

  Dim xlApp As Excel.Application
  Dim xlBook As Excel.Workbook
  Dim xlSheet As Excel.Worksheet
 
  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open(App.Path & "\my.xlt")
  Dim i As Integer
  For i = 1 To 10
    xlBook.Worksheets("My Sheet").Copy  
          before:=xlBook.Worksheets("My Sheet")
    Set xlSheet = xlBook.ActiveSheet
    xlSheet.Range("A1").Offset(1, 0).Value = i
  Next i
 
  xlApp.Application.DisplayAlerts = False
  xlBook.Worksheets("My Sheet").Activate
  xlApp.ActiveWindow.SelectedSheets.Delete
 
  xlBook.Worksheets.Add before:=xlBook.Worksheets("My Sheet (2)")
 
  xlBook.SaveAs FileName:=App.Path & "\my.xls", FileFormat  
            :=xlNormal, Password:="", WriteResPassword:="",  
            ReadOnlyRecommended:=False, CreateBackup:=False
           
  xlBook.Close
  xlApp.Close
 
  xlApp.Quit
  Set xlApp = Nothing
  Set xlBook = Nothing
  Set xlSheet = Nothing

End Sub

HAGD:O)Bruintje
0
 

Author Comment

by:kamal3965
ID: 7121133
as i am new to visual basic how do i let my program load excel templates in a combo or listbox

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7122498
If you have all your 25 files in one folder, Dir$ function would populate your combo/list box in an easy way.

dim ffile as string
const MYFOLDER as string="c:\mytemplates\"

ffile=dir$(MYFOLDER & "*.xlt",vbarchive) ' or *.xls
do while ffile<>""
   combox1.additem MYFOLDER & ffile
   ffile=dir$()
loop
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7122502
And instead of .Open use .Add method. This will ensure that you open a copy not "real" one.
0
 

Author Comment

by:kamal3965
ID: 7145347
how do you execute a excel file, which i am selecting a list/combo box.

i.e. i should execute only that excel file which i am selecting in list box/combo box


0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 200 total points
ID: 7146017
As bruintje already told you but instead of .Open, use .Add method.

Dim xlApp As Excel.Application
 Dim xlBook As Excel.Workbook
 Dim xlSheet As Excel.Worksheet
 
 Set xlApp = CreateObject("Excel.Application")
 Set xlBook = xlApp.Workbooks.Add(combobox1.text)
...
....
....
'more code
......
....
0
 

Author Comment

by:kamal3965
ID: 7159123
Thanks everyone who have responded to my query
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7159382
That's fine. did you get all working OK?
0
 

Author Comment

by:kamal3965
ID: 7161254
actually i am not able to open the excel files when i double click an excel file in a list box.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7170233
what did you put in the eventhandler on the double click?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7170255
As Bruintje right pointed, don't forget to add the full path to items in combo as i already told you (see my code above)
...
...
...
ffile=dir$(MYFOLDER & "*.xlt",vbarchive) ' or *.xls
...
...
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7970943
Hi kamal3965,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: bruintje and Richie_Simonetti

kamal3965, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8019468
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

bruintje points for you at:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20529302.html
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question