Solved

reading from a column in excel workbook

Posted on 2000-05-11
6
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month6 days, 8 hours left to enroll

634 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