Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Error writing value to cell

I want my VBA program to change the value of a cell. The cell
references are correct but the "write" or assignment step doesn't
work.

As a test, this line returns the correct cell location:

A = Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col).Address

After the following test statement, variable A contains the current
value of the cell:

A = Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col).Value

However, if I replace it with this step, something does not work
right:

Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col).Value = "Test"

I added a temporary Stop statement immediately after the line above.
However, if I single-step through the above line, it does not reach
the Stop statement. Instead, excecution jumps to another step before
the assignment.

HELP! I have no idea what to do next.



--
Steven M - lid
(remove wax and invalid to reply)

A fool and his money are soon elected. -- Will Rogers
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 510
Default Error writing value to cell

Hi Steven,

May be check how you have declared your variables ...

HTH
Carim

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Error writing value to cell

Je 9 Apr 2006 07:07:54 -0700, "Carim" skribis:

Hi Steven,

May be check how you have declared your variables ...


I don't understand. How should they be declared?

When I program in VBA for Word, I use "Option Explicit", but I don't
in Excel. Should I?



--
Steven M - lid
(remove wax and invalid to reply)

A fool and his money are soon elected. -- Will Rogers
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Error writing value to cell

If the "A=" statement works, I doubt it is a variable type problem.

Also, I assume the variables have the same value as when the "A=" did work
(otherwise, the comparison to that statement would be irrelevant).

Do you have an error handler in effect. If so, when you say it doesn't reach
that statement, are you sent to the error handler? Perhaps disable the
error handler and see what the error is.

Also,

Is the worksheet protected?

If you go to Tools=Protection, is Unprotect Sheet an option?

If so, unprotect the sheet and try your code.

Other than that, I would expect it to work.

It is always wise to use Option Explicit.

--
Regards,
Tom Ogilvy



"Steven M (remove wax and invalid to reply)"
wrote in message ...
Je 9 Apr 2006 07:07:54 -0700, "Carim" skribis:

Hi Steven,

May be check how you have declared your variables ...


I don't understand. How should they be declared?

When I program in VBA for Word, I use "Option Explicit", but I don't
in Excel. Should I?



--
Steven M - lid
(remove wax and invalid to reply)

A fool and his money are soon elected. -- Will Rogers



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Error writing value to cell

1. There is no error handler
2. Worksheet is not protected,
3. Have added Option Explicit.

I think I found a clue. Here's what VB Help says:

===
Cells Property
Range object: Returns a Range object that represents the cells in the
specified range (in other words, it does nothing). Read-only.
===

I suspect that this expression:

Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col)

actually returns a range that contains a single cell, and not a single
cell.

Still not sure what to do next, but it's given me something to work
with.

Steven



Je Sun, 9 Apr 2006 11:48:10 -0400, "Tom Ogilvy"
skribis:

If the "A=" statement works, I doubt it is a variable type problem.

Also, I assume the variables have the same value as when the "A=" did work
(otherwise, the comparison to that statement would be irrelevant).

Do you have an error handler in effect. If so, when you say it doesn't reach
that statement, are you sent to the error handler? Perhaps disable the
error handler and see what the error is.

Also,

Is the worksheet protected?

If you go to Tools=Protection, is Unprotect Sheet an option?

If so, unprotect the sheet and try your code.

Other than that, I would expect it to work.

It is always wise to use Option Explicit.



--
Steven M - lid
(remove wax and invalid to reply)

A fool and his money are soon elected. -- Will Rogers


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Error writing value to cell


Since you are having trouble stepping through the code, Try playing with
this code. It should be in a general module (insert=Module in the VBE)
rather than in a Worksheet Module or the ThisWorkbook module.

Option Explicit
Sub TestCode()
Dim rw As Long
Dim col As Long
Dim i As Long
Dim j As Long
Dim rng As Range
Dim A As Variant
Dim sh As Worksheet
Dim MyLocRow(1 To 2) As Long
Dim MyLocCol(1 To 2) As Long
Dim offset_Row As Long
Dim Offset_Col As Long

Set sh = Worksheets.Add(After:= _
Worksheets(Worksheets.Count))
With sh.Range("A1:T20")
.Formula = "=Char(column()+64)&row()"
.Formula = .Value
End With
For i = 1 To 2
MyLocRow(i) = Int(Rnd() * 10 + 1)
MyLocCol(i) = Int(Rnd() * 10 + 1)
Next
Offset_Col = Int(Rnd() * 10 + 1)
offset_Row = Int(Rnd() * 10 + 1)

For i = 1 To 2
For j = 1 To 2
rw = MyLocRow(i) + offset_Row
col = MyLocCol(j) + Offset_Col

If rw < 1 Or rw 65536 Then
MsgBox "Invalid row value: " & rw
Exit Sub
End If
If col < 1 Or col 256 Then
MsgBox "Invalid column value: " & col
Exit Sub
End If
On Error Resume Next
Set rng = sh.Cells(rw, col)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "rng was not set successfully"
Exit Sub
End If
A = rng.Value
rng.Value = "Test"

rng.Interior.ColorIndex = Int(Rnd() * 7 + 3)
MsgBox "For rng " & rng.Address(0, 0, xlA1, True) & _
vbNewLine & "Old Value was " & A & _
vbNewLine & "New value is " & rng.Value
Next j
Next i

End Sub


--
regards,
Tom Ogilvy


"Steven M (remove wax and invalid to reply)"
wrote in message ...
1. There is no error handler
2. Worksheet is not protected,
3. Have added Option Explicit.

I think I found a clue. Here's what VB Help says:

===
Cells Property
Range object: Returns a Range object that represents the cells in the
specified range (in other words, it does nothing). Read-only.
===

I suspect that this expression:

Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col)

actually returns a range that contains a single cell, and not a single
cell.

Still not sure what to do next, but it's given me something to work
with.

Steven



Je Sun, 9 Apr 2006 11:48:10 -0400, "Tom Ogilvy"
skribis:

If the "A=" statement works, I doubt it is a variable type problem.

Also, I assume the variables have the same value as when the "A=" did

work
(otherwise, the comparison to that statement would be irrelevant).

Do you have an error handler in effect. If so, when you say it doesn't

reach
that statement, are you sent to the error handler? Perhaps disable the
error handler and see what the error is.

Also,

Is the worksheet protected?

If you go to Tools=Protection, is Unprotect Sheet an option?

If so, unprotect the sheet and try your code.

Other than that, I would expect it to work.

It is always wise to use Option Explicit.



--
Steven M - lid
(remove wax and invalid to reply)

A fool and his money are soon elected. -- Will Rogers



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Error writing value to cell

Hi!

Check if you are in Edit Mode, when you are tring to insert this
value...

Tomer

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Error writing value to cell

Je 9 Apr 2006 07:14:03 -0700, "Tomer" skribis:

Hi!

Check if you are in Edit Mode, when you are tring to insert this
value...


Sorry, I don't understand. What is Edit Mode and how do I switch to
it?

I have another short VBA program that changes the format of cells, and
I don't do anything special to get into "Edit Mode". Here it is

= = = = =

Sub CleanRange()
For Each Piece In Range("Table")
Piece.Select
Selection.Font.Bold = True
If Application.IsNumber(Piece) Then
Selection.Font.ColorIndex = xlAutomatic
Else
Selection.Font.ColorIndex = 3
End If
Next
End Sub

= = = = =

(I don't really have a question about the above code, it works the way
I want it.)



--
Steven M - lid
(remove wax and invalid to reply)

A fool and his money are soon elected. -- Will Rogers
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Error writing value to cell

The error must be before the assignment (which works OK for me) so can you
post some more code? What code causes the jump?

Try putting in the A= ..... immediately prior to the assigment - does this
work?

[Occassionally I have had problems where simply changing the order of
statements can "solve" an "error" .]

"Steven M (remove wax and invalid to repl" wrote:

I want my VBA program to change the value of a cell. The cell
references are correct but the "write" or assignment step doesn't
work.

As a test, this line returns the correct cell location:

A = Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col).Address

After the following test statement, variable A contains the current
value of the cell:

A = Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col).Value

However, if I replace it with this step, something does not work
right:

Worksheets("Sheet1").Cells(myLocRow(i) + Offset_Row, myLocCol(i) +
Offset_Col).Value = "Test"

I added a temporary Stop statement immediately after the line above.
However, if I single-step through the above line, it does not reach
the Stop statement. Instead, excecution jumps to another step before
the assignment.

HELP! I have no idea what to do next.



--
Steven M - lid
(remove wax and invalid to reply)

A fool and his money are soon elected. -- Will Rogers

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error type mismatch in writing macro ub Excel Worksheet Functions 2 September 26th 07 12:48 PM
Issue writing IF OR Error Formula Types lozzam New Users to Excel 1 October 26th 06 08:14 AM
Error from losing power while writing macro. Recovery help? MSgtMark Excel Discussion (Misc queries) 2 February 2nd 06 10:35 PM
Writing a macro to copy first cell down until next cell has data Gerald[_2_] Excel Programming 1 August 10th 05 10:06 PM
Error 50290: Error writing to Worksheet while using an ActiveX Control emblair3 Excel Programming 3 February 24th 04 06:03 PM


All times are GMT +1. The time now is 10:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"