By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,160 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How to reference individual cells in Excel via VBA

P: n/a
Hello all,
Could anybody help me referencing individual cells in excel? I don't know
what I do wrong here (but I'm not familiar with excel vba) (Microsoft Excel
10.0 Object library is referenced)
Any help very much appreciated
Thanks
Ronny

Private Sub btnTargetAffGroupEmailImport_Click()
Dim MyXL As Object
Dim ExcelWasNotRunning As Boolean
On Error Resume Next ' Defer error trapping.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear
DetectExcel ' I HAVE PASTED THIS FUNCTION BELOW .....
Set MyXL = GetObject("U:\MIJNTEST.XLS")
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
'HERE I HAVE TO REFERENCE THE INDIVIDUAL CELLS
MyXL.cell(2, 2) = "Een"
MyXL.cell(2, 3) = "Twee"
MyXL.cell(2, 4) = "Drie"
MyXL.cell(2, 5) = "Vier"

If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If

Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.

End Sub
Public Function DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Function
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Function
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
From my file:

See below my SIG
--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

Refer To A Specific Cell In An Excel Worksheet

ObjWkb.Worksheets(1).Cells(Row, Col).Value

Row = Integer, specifying the row number

Col = Integer, specifying the column number
"Ronny Sigo" <ro********@skynet.be> wrote in message
news:40***********************@feed0.news.be.easyn et.net...
Hello all,
Could anybody help me referencing individual cells in excel? I don't know
what I do wrong here (but I'm not familiar with excel vba) (Microsoft Excel
10.0 Object library is referenced)
Any help very much appreciated
Thanks
Ronny

Private Sub btnTargetAffGroupEmailImport_Click()
Dim MyXL As Object
Dim ExcelWasNotRunning As Boolean
On Error Resume Next ' Defer error trapping.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear
DetectExcel ' I HAVE PASTED THIS FUNCTION BELOW .....
Set MyXL = GetObject("U:\MIJNTEST.XLS")
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
'HERE I HAVE TO REFERENCE THE INDIVIDUAL CELLS
MyXL.cell(2, 2) = "Een"
MyXL.cell(2, 3) = "Twee"
MyXL.cell(2, 4) = "Drie"
MyXL.cell(2, 5) = "Vier"

If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If

Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.

End Sub
Public Function DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Function
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Function

Nov 12 '05 #2

P: n/a
Works great! Thank you!

"PC Datasheet" <sp**@nospam.spam> schreef in bericht
news:p8******************@newsread3.news.atl.earth link.net...
From my file:

See below my SIG
--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

Refer To A Specific Cell In An Excel Worksheet

ObjWkb.Worksheets(1).Cells(Row, Col).Value

Row = Integer, specifying the row number

Col = Integer, specifying the column number
"Ronny Sigo" <ro********@skynet.be> wrote in message
news:40***********************@feed0.news.be.easyn et.net...
Hello all,
Could anybody help me referencing individual cells in excel? I don't know what I do wrong here (but I'm not familiar with excel vba) (Microsoft Excel 10.0 Object library is referenced)
Any help very much appreciated
Thanks
Ronny

Private Sub btnTargetAffGroupEmailImport_Click()
Dim MyXL As Object
Dim ExcelWasNotRunning As Boolean
On Error Resume Next ' Defer error trapping.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear
DetectExcel ' I HAVE PASTED THIS FUNCTION BELOW .....
Set MyXL = GetObject("U:\MIJNTEST.XLS")
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
'HERE I HAVE TO REFERENCE THE INDIVIDUAL CELLS
MyXL.cell(2, 2) = "Een"
MyXL.cell(2, 3) = "Twee"
MyXL.cell(2, 4) = "Drie"
MyXL.cell(2, 5) = "Vier"

If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If

Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.

End Sub
Public Function DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Function
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Function


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.