Solved

reading from a column in excel workbook

Posted on 2000-05-11
6
167 Views
Last Modified: 2010-05-02
how to read in values from a specific column in excel workbook 1 by 1 to a variable
0
Comment
Question by:bazukei
  • 2
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:ture
ID: 2799265
bazukei,

This procedure loops through A1 - A20 and reads each value into the variable x, one at a time.

(For this to work in VB, you must set a reference to the MS Excel library.)

Sub ReadExcelValues()

Dim xl As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim r As Long
Dim x As Variant

Set wb = xl.Workbooks.Open("c:\test\test.xls")
Set ws = wb.Worksheets("Sheet1")
For r = 1 To 20
  x = ws.Cells(r, 1)
  MsgBox x
Next r
wb.Close SaveChanges:=False
xl.Quit
Set wb = Nothing
Set xl = Nothing

End Sub

Ture Magnusson
Karlstad, Sweden
0
 
LVL 1

Accepted Solution

by:
manchula earned 20 total points
ID: 2799267
Try this code. This will use the opened Excel File to read the value. This example gives the first cell (row 1, column 1) value.

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet


Set xlApp = GetObject(, "Excel.Application")
Set xlBook = xlApp.Workbooks(1)
Set xlSheet = xlBook.Sheets(1)

'Use xlSheets.Cells(RowIndex, ColIndex)
MsgBox xlSheet.Cells(1, 1)


0
 

Author Comment

by:bazukei
ID: 2799408
Adjusted points from 10 to 20
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:bazukei
ID: 2799409
thanks
both answers are good but

ture .. your code won't get me to the right sheet
my desired sheet is 3 and column is X
from row 2 to 921

manchula.. your code won't get me to X column and after 389 rows it won't display anything from the cell eventhough i have the data

please try again

thank you all very much

0
 
LVL 1

Expert Comment

by:manchula
ID: 2799438
bazukei, i did not find anything wrong in that code. it works even for more than 5000 rows.

can you please tell me in detail about your code, or how you want to pick up the data.
0
 
LVL 22

Expert Comment

by:ture
ID: 2799449
bazukei,

I hope that this suits you better:

This procedure opens the workbook c:\test\test.xls and reads each value in Sheet3, range X2:X921 into the variable x

Sub ReadExcelValues()

Dim xl As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim r As Long
Dim x As Variant

Set wb = xl.Workbooks.Open("c:\test\test.xls")
Set ws = wb.Worksheets("Sheet3")
For r = 2 To 921
  x = ws.Cells(r, 24)
  MsgBox x
Next r
wb.Close SaveChanges:=False
xl.Quit
Set wb = Nothing
Set xl = Nothing

End Sub

/Ture
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

821 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