Home All Groups Group Topic Archive Search About

Excel Range will not put data into correct Cell

Author
20 Jan 2006 5:08 PM
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

Author
20 Jan 2006 5:15 PM
Cor Ligthert [MVP]
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


Show quoteHide quote
"XxLicherxX" <goldhor***@aol.com> schreef in bericht
news:1137776909.792975.17690@g43g2000cwa.googlegroups.com...
> 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
>
Author
20 Jan 2006 5:27 PM
Steve Long
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

Show quoteHide quote
"XxLicherxX" <goldhor***@aol.com> wrote in message
news:1137776909.792975.17690@g43g2000cwa.googlegroups.com...
> 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
>
Author
20 Jan 2006 6:33 PM
XxLicherxX
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
Author
20 Jan 2006 6:53 PM
XxLicherxX
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.
Author
23 Jan 2006 9:39 PM
kevininstructor@state.or.us
This might help
http://support.microsoft.com/?scid=kb;EN-US;301982

If not I have demo code, let me know.

Show quoteHide quote
"XxLicherxX" <goldhor***@aol.com> wrote in message
news:1137776909.792975.17690@g43g2000cwa.googlegroups.com...
> 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
>
Author
24 Jan 2006 2:31 PM
XxLicherxX
Thanks Kevin,

That will come in handy.