|
-
Jun 12th, 2000, 03:19 PM
#1
Thread Starter
Fanatic Member
I have a project to manipulate Excel in VB.
I can open and read basic column info into VB arrays but there is a lot more to the RANGE object etc (from looking in the object browser)
anyone know where there is some documentation or tips web pages on reading excel data in VB??
I'm after getting details like:
Sheet count
First Row in row count (the row count figure starts at the first row with data, so if only row 23 - 25 have data, return of rowcount is 3)
First Col in col count (same as above)
data type in cell
just for starters anyway
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jun 12th, 2000, 04:01 PM
#2
Hyperactive Member
-
Jun 12th, 2000, 04:16 PM
#3
Thread Starter
Fanatic Member
THX Dood,
I used "ActiveSheet.UsedRange.Rows.Count" previously and it returned the number of used rows.
The first row was blank in that column so it counted from row 2
How do I find out where the data starts? If I ran a loop to grab the data into an array and started at row 1 then I'd miss the last row!
columns had the same problem.
I have to read a excel sheets into a DB but the format is not specified.
Dynamic DB I can handle, Dynamic Excel is a *****!
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jun 12th, 2000, 04:35 PM
#4
Hyperactive Member
-
Jun 12th, 2000, 05:23 PM
#5
Thread Starter
Fanatic Member
I don't think so,
The Excel sheets don't need to be opened to view and won't be being edited while I read them.
I just need to read the data from an entire sheet to write to a DB but the sheet may be of any size.
EG:
500 rows
20 cols
first row starts at row 10
first col starts at col B
basically a square table
read to 2d array or array of UDT
User will select xls file from dialog control.
I can open it
I can read a set number of rows
I'm just not sure how to get the rows I want because the row and col counts in the range object ironically enough, don't specify a range! just the number of rows but not the first and last.
I need this info from the objects because the software only displays progress bars for what it's doing, the user can't check or correct the fact that the rows are correct.
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jun 12th, 2000, 05:48 PM
#6
Hyperactive Member
I think I'm missing something....
UsedRange is a property of Worksheet, so if you can open the sheet, you can used UsedRange. The address of used range can be used both to determine the number of rows and columns and where they start.
From you example in R1C1 style:
UsedRange.Address(ReferenceStyle:=xlR1C1) = "R10C2:R509C21"
UsedRange.Rows.Count = 500
UsedRange.Columns.Count = 20
The UsedRange is intrinsically located.
Try:
Code:
(psuedo code)
OpenWorkbook
Dim MyRange as Range
Dim MyCell as Cell
Set MyRange = MySheet.UsedRange
For Each MyCell in MyRange
MsgBox Location MyCell.Address & " " & MyCell.Value
Next MyCell
This will cycle through all the cells in a range, by column, then row (i.e. A1, A2, A3, B1, B2, B3 ... etc)
If you want to 'locate yourself' within your used range use:
UsedRange.Cells(1,1)
this is the upper left corner of your range.
To traverse the range you can then use:
UsedRange.Cells(1,1).Offset(r,c)
Offset is a row and column offset from the original location.
It should also be relatively easy, knowing the bounds and location of your UsedRange, to construct subranges relating to rows and/or columns if necessary.
[Edited by Judd on 06-13-2000 at 06:52 AM]
      
Dan
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
-
Jun 12th, 2000, 07:26 PM
#7
Thread Starter
Fanatic Member
THANK YOU
Sorry for not catching on earlier...
Onya, You're a Bloody legend!
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jun 12th, 2000, 08:01 PM
#8
Hyperactive Member
-
Jun 13th, 2000, 10:09 AM
#9
Thread Starter
Fanatic Member
It Worked
Thanks Judd,
Here's what I ended up with.
I think you saved me about 3 days of pissing about by myself... Think I'll take a long lunch 
Code:
' Module Code
Public Type Excell_Cell
Address As String
value As String
End Type
Public Cells() As Excell_Cell
'Form Code
Private Sub Command1_Click()
Dim FileName As String
Dim wkbObj As Workbook
Dim MyRange As Range
Dim MyCell As Range ' you wrote "cell" but I couldn't find that object
Dim i As Long
Diag1.ShowOpen
FileName = Diag1.FileName
If Len(FileName) < 3 Then Exit Sub
Set wkbObj = GetObject(FileName)
Set MyRange = wkbObj.Worksheets(1).UsedRange
ReDim Cells(MyRange.Cells.Count)
For i = 1 To MyRange.Cells.Count
Cells(i).Address = MyRange.Cells(i).Address
Cells(i).value = MyRange.Cells(i).value
Next
End Sub
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jun 13th, 2000, 05:25 PM
#10
Hyperactive Member
-
Jun 13th, 2000, 05:59 PM
#11
Thread Starter
Fanatic Member
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
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
|