Excel Range will not put data into correct Cell

  • Thread starter Thread starter XxLicherxX
  • Start date Start date
X

XxLicherxX

Hello everyone - new to VB.net working with Excel

I am trying to populate data into cells in an Excel spreadsheet using
Range.Value. No matter what cell I give as an argument to the Range
object, Excel always puts the value in "A1".

Here is the code I am having problems with. I am not getting any
errors.

oWBK = oXL.Workbooks.Open("C:\Data.xls")
oWS = oXL.Worksheets(1)
oWS.Range("C3").Value = "PUT IT IN THE RIGHT SPOT!!!"

The resulting Excel sheet will have the text "PUT IT IN THE RIGHT
SPOT!!!" in Cell "A1".

What am I doing wrong?

Thanks
 
Hi,

Although this is probably for VBNet, will asking this kind of questions
first in the one of the most active newsgroups

microsoft.public.excel.programmer

Give you an better change on an answer and if it than does not fit in VBNet
ask it here.

I hope this helps,

Cor
 
How about this syntax:
if you can gain access to the Cells collection, you should be able to do
something like:

Cells(row, col).Value = "My good value"

HTH
Steve
 
Hey Guys,

Thanks for your responses. I have worked on it a little more and the
problem seems to be with the way I create the Excel file. I was doing
this:

FileOpen(1,"C:\data.xls")
FileClose(1)

when I commented this stuff out, everything started working correcly,
however, I need to be able to create a new file from this application.
So I tried using

Sub CreateXLFile()

Dim oExcel = CreateObject("Excel.Application")

With oExcel
.SheetsInNewWorkbook = 3
oExcel = .Workbooks.Add
.Save("C:\Data.xls")
.Quit()
End With

End Sub

That does not work. It creates the file just fine, but does not write
to it. This is what I am using to write to the file: (Same as Above)

oWBK = oXL.Workbooks.Open("C:\Data.xls")
oWS = oXL.Worksheets(1)
oWS.Range("C3").Value = "PUT IT IN THE RIGHT SPOT!!!"

I then open the excel file and it is blank. The only time I can get
this to work correctly is if I manually create the file using Windows
Explorer ("Right Click -> Excel File, etc).

What am I doing wrong again?

Thanks
 
Ok, I looked around a bit more and it is in fact writing data, however
it is saving it to the My Documents folder under Book1.xls, Book2.xls,
etc. This doesn't make any sense to me. I never specify the My
Documents directory anywhere in the program. Also when the program is
running I get a prompt telling me that "C:\Data.xls" already exists and
would I like to replace it.
 
Back
Top