473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using VB6.0 to import Excel files

I have a Project at hand "Importing Excel files into a VBA" but i dont know how to go about it. Please can someone put me through. Though i tried Importing wizard but am not getting the result i wanted. Please what do u suggest.
Feb 1 '07 #1
3 21312
hariharanmca
1,977 1GB
I have a Project at hand "Importing Excel files into a VBA" but i dont know how to go about it. Please can someone put me through. Though i tried Importing wizard but am not getting the result i wanted. Please what do u suggest.
give refrence to your VBA and you can import data from Exel to VBA
Feb 1 '07 #2
Dököll
2,364 Expert 2GB
I do not know how to go about this either laparico2002. I did find this for you though: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1123176&SiteID=1

TIP: If you try your problem/question into google, you are likely to find some answers.

Good luck!

Dököll
Feb 4 '07 #3
First add refrence Microsoft Excel 9.0 Object library
then use idea from this code i think its gonna help u

Private Sub Command1_Click()
On Error GoTo err:

Call StartExcelS
Call CreateWorkSheetS
Call PopulateWorkSheetS
Call SaveWorkSheetS
Call CloseWorkSheetS

Exit Sub
err:
MsgBox "Error Number: " & err.Number & "; " & err.Description, vbOKOnly + vbCritical
End Sub
Private Sub StartExcelS()
On Error GoTo err:
l5.Caption = "Opening Excel"
Set Excel = GetObject(, "Excel.Application") ' Create Excel Object.
'Well you have to do like this.
'Above line if I used CreateObject, 1st time it would
'work fine but the second time my program would
'hang.Well I found this the easiest way to do it.
'But you can do it another way if you like.


'By default after creating the Excel it will
'not be shown on the screen.
'I you want to show it then
'Excel.Visible = True ' Show Excel

Exit Sub
err:
Set Excel = CreateObject("Excel.Application") 'Create Excel Object.

End Sub

Private Sub CreateWorkSheetS()
l5.Caption = "Creating Excel Worksheet"
Set ExcelWBk = Excel.Workbooks.Open(App.Path & "\dummy.xls") 'Add this Workbook to Excel.
Set ExcelWS = ExcelWBk.Sheets(3) 'ActiveSheet ' Add this sheet to this Workbook
End Sub

Private Sub PopulateWorkSheetS()
Dim row As Integer
Dim temp As String

row = 8 ' This is the row, start from 2nd row bec 1st row is header.
rsAllInOne.Open "SELECT * From BillData Where UtilityCompanyCode <> 4 And ModeOfPayment = 1 Order By UtilityCompanyCode", cn, adOpenKeyset, adLockOptimistic


While rsAllInOne.EOF <> True ' populate with first 100 records
'Total field is 12 so
rsCompanyName.Open "Select * from CompanyName Where CompanyID = " & rsAllInOne.Fields(4).Value, cn, adOpenKeyset, adLockOptimistic
If rsCompanyName.RecordCount > 0 Then

ExcelWS.Cells(row, 2) = rsAllInOne.Fields(5).Value
ExcelWS.Cells(row, 3) = rsCompanyName.Fields(1).Value
ExcelWS.Cells(row, 4) = rsAllInOne.Fields(7).Value
ExcelWS.Cells(row, 5) = rsAllInOne.Fields(1).Value
ExcelWS.Cells(row, 6) = rsAllInOne.Fields(12).Value
ExcelWS.Cells(row, 7) = rsAllInOne.Fields(2).Value

row = row + 1 ' increment row

l5.Caption = "Adding Records Please Wait: " & row & " records added"

End If
rsAllInOne.MoveNext
rsCompanyName.Close
Wend
rsAllInOne.Close

Set ExcelWS = ExcelWBk.Sheets(4)

row = 8 ' This is the row, start from 2nd row bec 1st row is header.

rsAllInOne.Open "SELECT * From BillData Where UtilityCompanyCode <> 4 And ModeOfPayment = 2", cn, adOpenKeyset, adLockOptimistic


While rsAllInOne.EOF <> True ' populate with first 100 records
'Total field is 12 so
rsCompanyName.Open "Select * from CompanyName Where CompanyID = " & rsAllInOne.Fields(4).Value, cn, adOpenKeyset, adLockOptimistic
If rsCompanyName.RecordCount > 0 Then

ExcelWS.Cells(row, 3) = rsAllInOne.Fields(5).Value
ExcelWS.Cells(row, 4) = rsCompanyName.Fields(1).Value
ExcelWS.Cells(row, 6) = rsAllInOne.Fields(7).Value
ExcelWS.Cells(row, 7) = rsAllInOne.Fields(1).Value
ExcelWS.Cells(row, 8) = rsAllInOne.Fields(12).Value
ExcelWS.Cells(row, 9) = rsAllInOne.Fields(2).Value

row = row + 1 ' increment row

l5.Caption = "Adding Records Please Wait: " & row & " records added"

End If
rsAllInOne.MoveNext
rsCompanyName.Close
Wend
rsAllInOne.Close

Set ExcelWS = ExcelWBk.Sheets(5)

row = 8 ' This is the row, start from 2nd row bec 1st row is header.
rsAllInOne.Open "SELECT * From BillData Where UtilityCompanyCode = 4 And ModeOfPayment = 1", cn, adOpenKeyset, adLockOptimistic


While rsAllInOne.EOF <> True ' populate with first 100 records
'Total field is 12 so
rsCompanyName.Open "Select * from CompanyName Where CompanyID = " & rsAllInOne.Fields(4).Value, cn, adOpenKeyset, adLockOptimistic
If rsCompanyName.RecordCount > 0 Then

ExcelWS.Cells(row, 2) = rsAllInOne.Fields(5).Value
ExcelWS.Cells(row, 3) = rsCompanyName.Fields(1).Value
ExcelWS.Cells(row, 4) = rsAllInOne.Fields(7).Value
ExcelWS.Cells(row, 5) = rsAllInOne.Fields(1).Value
ExcelWS.Cells(row, 6) = rsAllInOne.Fields(12).Value
ExcelWS.Cells(row, 7) = rsAllInOne.Fields(2).Value

row = row + 1 ' increment row

l5.Caption = "Adding Records Please Wait: " & row & " records added"

End If
rsAllInOne.MoveNext
rsCompanyName.Close
Wend
rsAllInOne.Close

Set ExcelWS = ExcelWBk.Sheets(6)

row = 8 ' This is the row, start from 2nd row bec 1st row is header.

rsAllInOne.Open "SELECT * From BillData Where UtilityCompanyCode = 4 And ModeOfPayment = 2", cn, adOpenKeyset, adLockOptimistic


While rsAllInOne.EOF <> True ' populate with first 100 records
'Total field is 12 so
rsCompanyName.Open "Select * from CompanyName Where CompanyID = " & rsAllInOne.Fields(4).Value, cn, adOpenKeyset, adLockOptimistic
If rsCompanyName.RecordCount > 0 Then

ExcelWS.Cells(row, 3) = rsAllInOne.Fields(5).Value
'ExcelWS.Cells(row, 4) = rsCompanyName.Fields(1).Value
ExcelWS.Cells(row, 5) = rsAllInOne.Fields(7).Value
ExcelWS.Cells(row, 6) = rsAllInOne.Fields(1).Value
ExcelWS.Cells(row, 7) = rsAllInOne.Fields(12).Value
ExcelWS.Cells(row, 8) = rsAllInOne.Fields(2).Value

row = row + 1 ' increment row

l5.Caption = "Adding Records Please Wait: " & row & " records added"

End If
rsAllInOne.MoveNext
rsCompanyName.Close
Wend
rsAllInOne.Close

End Sub


Private Sub SaveWorkSheetS()
' Save the workbook on the desktop
'I didn't had time so I have not added export feature.
'If you want to export it into another format then just
'change this line.
'e.g
'ExcelWBk.SaveAs "c:\windows\desktop\Demo.txt", xlCSV
l5.Caption = "Saving Excel"
ExcelWBk.SaveAs App.Path & "\" & Text1.Text & Mid(Time, 7, 2) & "final.xls"
'text1.Text = ""
End Sub

Private Sub CloseWorkSheetS()
' Close the WorkBook
ExcelWBk.Close
' Quit Excel app
Excel.Quit
l5.Caption = ""
'Text1.Text = ""
MsgBox "You can find the saved Excel Sheet at " & App.Path, vbInformation + vbOKOnly

End Sub


I have a Project at hand "Importing Excel files into a VBA" but i dont know how to go about it. Please can someone put me through. Though i tried Importing wizard but am not getting the result i wanted. Please what do u suggest.
Feb 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jack | last post by:
Windows 2K Pro Access/Excel 2003 Hi there, I have a large number of password protected Excel Workbooks. The files are protected by code that runs when the workbook opens. The code asks the...
4
by: Jae | last post by:
I'm writing a web application that exports and imports excel files. The application gets a list of users and their info and displays it in a datagrid .The user then selects to save the file as a...
2
by: amy | last post by:
Hi, all: i am a new end user of access, now I have many excel files need to import to One table in access (combine all excel files into one table in excel). In excel files, some columns will have...
0
by: amy | last post by:
Hi, all: > i am a new end user of access, now I have many excel files need to > import to One table in access (combine all excel files into one table > in excel). In excel files, some columns will...
1
by: mednyk | last post by:
It's very simple import, without any data modifications; from several excel files to one table. I tried the wisard and it gives me selection for only one file. I am not used to create DTS...
3
by: ninrulz | last post by:
I will try to explain my situation. I know that it is hard to offers solutions without fully understanding what people would like to achieve. I receive 2 csv files every month. The csv files...
11
by: MD | last post by:
Hello, I need to import a sheet of 884 different excel-file with same lay- out. The sheet name is 'Totaal' and is the same in all different files. Is there a script (module) in order to: 1....
5
geolemon
by: geolemon | last post by:
Import text wizard says: I'm banging my head on this one, here's why: I've been importing files using this process and data format, with success! I created a temporary table in Access to...
5
by: Albert-jan Roskam | last post by:
Hi, I wrote the program below to merge all xls files in a given directory into one multisheet xls file. It uses xlwt and xlrd. The xls files I use for input are generated by Spss. When I open and...
0
by: Albert-jan Roskam | last post by:
Hi John, Thanks! Using a higher xlrd version did the trick! Regarding your other remarks: -yep, input files with multiple sheets don't work yet. I kinda repressed that ;-) Spss outputs only...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.