Solved

reading from a column in excel workbook

Posted on 2000-05-11
6
165 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
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points from 10 to 20
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:bazukei
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now