Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import Excel data to simple array in VB 5

Status
Not open for further replies.

Dewey

Programmer
Jan 25, 2000
21
US
Elementary but i am trying to do the following:<br>From an Excel 2000 file &quot;c:\reports\march00.worksheet01.xls&quot;, I need to load into a simple array in VB6 the grid of &quot;A53:G78&quot; so that I can strip some data out. <br>I am somewhat lost and confused. I can create a connection string using the ADODC control but can't get an SQL statement to work to access the worksheet ( this might be my fault and pure syntax).<br>I think using simple ADO access would be easier.&nbsp;&nbsp;I am ready for a primer in it.&nbsp;&nbsp;Any takers,<br><br>Thanks in advance....Dewey <p>Dewey Davis<br><a href=mailto:ddavis@dfn.com>ddavis@dfn.com</a><br><a href= > </a><br>
 
So let have a look at your SQL...<br> <p>DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br> Ask me how Bar-codes can help you be more productive.
 
I have tried many like:<br><br>SELECT [A53:G78] FROM &quot;c:\Reports\March00.Worksheet01.xls&quot; <p>Dewey Davis<br><a href=mailto:ddavis@dfn.com>ddavis@dfn.com</a><br><a href= > </a><br>
 
Create a reference to the Excel Object Library (use Project:References on the VB menu bar) and then in your code:&nbsp;&nbsp;&nbsp;&nbsp;<br><FONT FACE=monospace><br>Dim i As Integer, j As Integer<br>Dim aryExcel()<br>Dim appExcel As Excel.Application<br>Set appExcel = New Excel.Application<br>ReDim aryExcel(7, 26)<br>appExcel.Workbooks.Open &quot;c:\reports\march00.worksheet01.xls&quot;<br><br>For i = 1 To 7<br>&nbsp;&nbsp;&nbsp;&nbsp;For j = 53 To 78<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aryExcel(i, j - 52) = appExcel.Range(Chr(i + 64) &).Value<br>&nbsp;&nbsp;&nbsp;&nbsp;Next j<br>Next i<br></font>
 
Nohandle:<br>Thanks for the response.&nbsp;&nbsp;I tried the following:Option Explicit<br><br><br><br>Private Sub Command1_Click()<br>Dim i As Integer, j As Integer<br>Dim aryExcel()<br>Dim appExcel As Excel.Application<br>Set appExcel = New Excel.Application<br>ReDim aryExcel(7, 26)<br>appExcel.Workbooks.Open &quot;c:\reports\march00.worksheet01.xls&quot;<br><br>For i = 1 To 7<br>&nbsp;&nbsp;&nbsp;&nbsp;For j = 53 To 78<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aryExcel(i, j - 52) = appExcel.Range(Chr(i + 64)).Value<br>&nbsp;&nbsp;&nbsp;&nbsp;Next j<br>Next i<br>End Sub<br><br>I keep gettting a workbook not found message.&nbsp;&nbsp;I know its there and spelled correctly.&nbsp;&nbsp;Also I don't understand the &quot;&&quot; on the 4th from bottom line of code. I keep getting a syntax error there also.<br><br>Thanks again for the help this far..............Dewey<br><br> <p>Dewey Davis<br><a href=mailto:ddavis@dfn.com>ddavis@dfn.com</a><br><a href= > </a><br>
 
Sorry - my typo....<br><br>It should read :<br>aryExcel(i, j - 52) = appExcel.Range(Chr(i + 64) & j).Value<br><br>The Chr converts the digit i to its equivalent letter (1 becomes a, 2 becomes b, etc) so the Range is acting on a Cell reference (a1, b2, etc). When i is 1 and j is 53 you have: appExcel.Range(a53).Value
 
NoHandle(one more time)<br>I edited the typo, but I get the VB error '1004' when I try to run. It says that the file &quot;c:\reports\march00.worksheet01.xls&quot; can not be found.<br><br>I&nbsp;&nbsp;Know it is there and spelled correctly.&nbsp;&nbsp;I understand your code now, just can't figure out why it doesn't see the file.( I have set the References to Excel Object Library 9.0)<br>Thanks again.... Dewey <p>Dewey Davis<br><a href=mailto:ddavis@dfn.com>ddavis@dfn.com</a><br><a href= > </a><br>
 
Sorry, I can't help you. If I correctly name my Excel 2000 spreadsheet file, the code works; if I spell it incorrectly, it doesn't (with Error 1004 as you got). I'm also using the Excel Object Library 9.0<br><br>I also should have said that you need to close the appexcel object and remove references to it once you have finished as follows:<br>&nbsp;&nbsp;&nbsp;&nbsp;appExcel.Workbooks.Close<br>&nbsp;&nbsp;&nbsp;&nbsp;Set appExcel = Nothing<br>&nbsp;
 
NoHandle,<br>thanks again.<br>I finally dropped the &quot;.worksheet01&quot; out of the workbook name, which then gave me:<br>appExcel.Workbooks.Open &quot;c:\reports\march00.xls&quot;<br>It now accesses the workbook and I need to figure out code to address each worksheet in the workbook,( there are 31 that I need to loop through).&nbsp;&nbsp;I'm still working on it. Got a be a way.&nbsp;&nbsp;<br>Thought this might interest you.<br>Any more ideas, don't hesitate to let me know.<br>Thanks, Dewey <p>Dewey Davis<br><a href=mailto:ddavis@dfn.com>ddavis@dfn.com</a><br><a href= > </a><br>
 
Try appExcel.workbook(x)<br><br>there may or may not be stuff after that to open it but i know it is something along that lines <p>Karl Pietri<br><a href=mailto:lordhuh.pota.to>lordhuh.pota.to</a><br><a href= > </a><br>
 
To cycle through the worksheets, you may want to set up an object reference to the sheet. <br><br>Set ds = appExcel.Worksheets(&quot;Sheet1&quot;)<br><br>Then, the info will be read in much faster.<br><br>aryExcel(i, j - 52) = ds.Range(Chr(i + 64) & j).Value<br><br>You can cycle through the sheets:<br>For i = 1 to 10<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set ds = appExcel.Worksheets(&quot;Sheet&quot; & i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Do your stuff here<br>Next i<br><br>or you can do something like this:<br>For Each w In Worksheets<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aryExcel(i, j - 52) = w.Range(Chr(i + 64) & j).Value<br>Next w<br><br>It all depends on your setup.<br>Hope this helps a bit!<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top