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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you read an excel cell from VB?

Status
Not open for further replies.

f1car

Technical User
Apr 2, 2001
69
US
I still can't get this code to work.

I thought I was pretty close here, but I can't seem to find a way to get Worksheets("Sheet1") to work. I get 'method worksheets failed', after seeing the object browser, I see that I should be using Worksheet instead, but VB won't even recognize it at all.

Dim XLSsheet As Object
Set XLSsheet = GetObject("c:\excelcash1.xls", "excel.sheet")
XLSsheet.Application.Visible = True

For rwIndex = 1 to 4
For colIndex = 1 to 10
With Worksheets("Sheet1").Cells(rwIndex, colIndex)
If .Value < .001 Then .Value = 0
End With
Next colIndex
Next rwIndex

I must have 30 VB books here but they don't cover office objects much, what books are you guys using for this mess?
 
Why dont u use VBasic editor from Excel &quot;ALT + F11&quot;
And u could use code from there...
________

George
 
Never done it the way you have it coded, but here's some old code that I have that used to work. Check this out:

Code:
'you can define the object this way...
Dim objExcel as Object
'or you can do it this way...
'Dim objExcel as new Excel.Application
'as long as you have it in your references...
'If you do it that way, you won't do the CreateObject below

'Create Excel Object.
Set objExcel = CreateObject(&quot;Excel.Application&quot;)
objExcel.Visible = False

'Start by opening the workbook.
objExcel.Workbooks.Open &quot;c:\excelcash1.xls&quot;

With objExcel.ActiveWorkbook.Worksheets(&quot;Sheet1&quot;)
    For rwIndex = 1 to 4
        For colIndex = 1 to 10
            If .Cells(rwIndex, colIndex).Value < .001 Then
                .Cells(rwIndex, colIndex).Value = 0
            End If
        Next colIndex
    Next rwIndex
End With

'Close workbook and save changes
objExcel.Workbooks(&quot;excelcash1.xls&quot;).Close saveChanges:=True
'Quit Excel
objExcel.Quit
Set objExcel = Nothing

Note, this is VB code, not VBScript code. If you need vbscript code, I have some of that too, but you might want to post your question in the Visual Basic forum or the VBA forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top