Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 298
Default Read Excel data from VB.net using range object - prob with 'undefi

Hi,

I need to read data from hundreds of Excel files (which are not consistently
formatted - # of sheets vary, UsedRange per sheet varies...) and write some
of that data to a sql server. I put together a quicky VB.Net app to do this.
I use the range object with automation from the VB.Net app (VB2005).

....Dim rng As Excel.Range, wkbk As Excel.WorkBook
wkbk = xl.Workbooks.Open("C:\...")
For Each sht As Excel.WorkSheet In wkbk.Sheets
rng = sht.UsedRange
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
Try
str1 = Ctype(rng(i, j), Excel.Range).Value.ToString
Catch Ex As Exception
End Try
Next
Next
....

This routine works fine except for a little overhead from the Inner Try
Catch Block in the For Loop (I also had to use Ctype since VB2005 is OOP). A
problem I was having was that in the UsedRange - some cells were empty and my
vb.net app was crashing with a system error message that the crashing value
was 'Undefined'. So I added the Inner Try Catch block. This fixed the
problem - but has added a little bit of overhead. Someone from the vb.net
group suggested to test values using rng.FormulaR1C1. I have not tried that
yet, but wasn't sure if this was a way (or the way) to go. So I am checking
if anyone in this Excel group works with Excel and .Net (VB/C# either or) and
might have another suggestion or could confirm about using .FormulaR1C1 to
test a value - the goal would be to return an empty string instead of an
'Undefined' value.

Thanks,
Rich
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 Range.ServerActions Property displays read-only data SG Excel Programming 0 January 31st 08 03:33 PM
Prob with ChartSource DataData Range MikeZz Charts and Charting in Excel 3 February 6th 07 06:02 PM
Path name in title bar-READ ONLY prob Deeds Excel Programming 2 October 14th 05 02:59 PM
Possible to read data entered in a worksheet's Form object, using a VBA command? Android[_2_] Excel Programming 2 May 14th 04 03:17 PM
Excel VBA - Read and modify the chart data range rcatarino Excel Programming 0 April 6th 04 01:19 AM


All times are GMT +1. The time now is 04:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"