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

I get an error 1004 when I execute the VBA function below. I've tried
everyting, Range = ID, cells(1) = ID, Range("test").Cells(1).Value = ID, you
name it. Always the same error.

Range "Test' is a named range of one cell on sheet 1 that contains the value
997.

The Function below reads the correct value out of the named range into the
var ID, but errors out when I write to the cell. Cell is not locked. Sheet
is not protected. Trust Center allows VBA code. Code is in a seperate
module, not on the sheet code.

Purpose is to increment a value, and save it in a cell on the sheet.

I call the function from another cell, like this: =IF(B4="","",NewID())

I've been at this for hours, researching what could be wrong. About ready to
toss the computer out the window. Anyone have any ideas?

Function NewID()
Dim ID As Long

ID = Range("Test").Cells(1).Value
ID = ID + 1
Range("Test").Cells(1).Value = ID 'This line throws the error

NewID = ID

End Function

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 207
Default Error 1004 when cell set to a value

Bob A,
This is the solution I came up with:

Sub Get_NewID()
Dim ID As Long, NewID As Long

ID = Worksheets("Sheet1").Range("A1").Value
NewID = ID + 1


Range("A1").Select
Selection.Value = NewID


End Sub
"Sheet1" you should change to the name of your sheet. What this doesis this:
If 1 is in cell A1, this program reads the "1" and adds 1 to it.
Is that what you want?
hth
"Bob A" wrote:

I get an error 1004 when I execute the VBA function below. I've tried
everyting, Range = ID, cells(1) = ID, Range("test").Cells(1).Value = ID, you
name it. Always the same error.

Range "Test' is a named range of one cell on sheet 1 that contains the value
997.

The Function below reads the correct value out of the named range into the
var ID, but errors out when I write to the cell. Cell is not locked. Sheet
is not protected. Trust Center allows VBA code. Code is in a seperate
module, not on the sheet code.

Purpose is to increment a value, and save it in a cell on the sheet.

I call the function from another cell, like this: =IF(B4="","",NewID())

I've been at this for hours, researching what could be wrong. About ready to
toss the computer out the window. Anyone have any ideas?

Function NewID()
Dim ID As Long

ID = Range("Test").Cells(1).Value
ID = ID + 1
Range("Test").Cells(1).Value = ID 'This line throws the error

NewID = ID

End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Error 1004 when cell set to a value

User defined Functions return values to cells that hold the formula--they can't
touch the values in other cells.

Maybe it's time to look at worksheet events that can do what you want???


Bob A wrote:

I get an error 1004 when I execute the VBA function below. I've tried
everyting, Range = ID, cells(1) = ID, Range("test").Cells(1).Value = ID, you
name it. Always the same error.

Range "Test' is a named range of one cell on sheet 1 that contains the value
997.

The Function below reads the correct value out of the named range into the
var ID, but errors out when I write to the cell. Cell is not locked. Sheet
is not protected. Trust Center allows VBA code. Code is in a seperate
module, not on the sheet code.

Purpose is to increment a value, and save it in a cell on the sheet.

I call the function from another cell, like this: =IF(B4="","",NewID())

I've been at this for hours, researching what could be wrong. About ready to
toss the computer out the window. Anyone have any ideas?

Function NewID()
Dim ID As Long

ID = Range("Test").Cells(1).Value
ID = ID + 1
Range("Test").Cells(1).Value = ID 'This line throws the error

NewID = ID

End Function


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Error 1004 when cell set to a value

its simply
Range("test") = ID

yoo don't need the .Value as it is the default method, but its better
codeing as its easier to unserstand, so

Range("test").Value = ID

you could also have used

Range("test").Value = Range("test").Value +1




"Bob A" wrote:

I get an error 1004 when I execute the VBA function below. I've tried
everyting, Range = ID, cells(1) = ID, Range("test").Cells(1).Value = ID, you
name it. Always the same error.

Range "Test' is a named range of one cell on sheet 1 that contains the value
997.

The Function below reads the correct value out of the named range into the
var ID, but errors out when I write to the cell. Cell is not locked. Sheet
is not protected. Trust Center allows VBA code. Code is in a seperate
module, not on the sheet code.

Purpose is to increment a value, and save it in a cell on the sheet.

I call the function from another cell, like this: =IF(B4="","",NewID())

I've been at this for hours, researching what could be wrong. About ready to
toss the computer out the window. Anyone have any ideas?

Function NewID()
Dim ID As Long

ID = Range("Test").Cells(1).Value
ID = ID + 1
Range("Test").Cells(1).Value = ID 'This line throws the error

NewID = ID

End Function

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Error 1004 when cell set to a value

Ps. I should have said that UDF that are used in cells in worksheets can only
return values to the cells that hold that formula.

Functions used in your code (called by other functions/subs) can do what you
want.

But no you can't have a UDF in a cell call another function that tries to break
this. Excel is pretty smart.

Dave Peterson wrote:

User defined Functions return values to cells that hold the formula--they can't
touch the values in other cells.

Maybe it's time to look at worksheet events that can do what you want???

Bob A wrote:

I get an error 1004 when I execute the VBA function below. I've tried
everyting, Range = ID, cells(1) = ID, Range("test").Cells(1).Value = ID, you
name it. Always the same error.

Range "Test' is a named range of one cell on sheet 1 that contains the value
997.

The Function below reads the correct value out of the named range into the
var ID, but errors out when I write to the cell. Cell is not locked. Sheet
is not protected. Trust Center allows VBA code. Code is in a seperate
module, not on the sheet code.

Purpose is to increment a value, and save it in a cell on the sheet.

I call the function from another cell, like this: =IF(B4="","",NewID())

I've been at this for hours, researching what could be wrong. About ready to
toss the computer out the window. Anyone have any ideas?

Function NewID()
Dim ID As Long

ID = Range("Test").Cells(1).Value
ID = ID + 1
Range("Test").Cells(1).Value = ID 'This line throws the error

NewID = ID

End Function


--

Dave Peterson


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Error 1004 when cell set to a value

change FUNCTION to SUB


"Bob A" wrote:

I get an error 1004 when I execute the VBA function below. I've tried
everyting, Range = ID, cells(1) = ID, Range("test").Cells(1).Value = ID, you
name it. Always the same error.

Range "Test' is a named range of one cell on sheet 1 that contains the value
997.

The Function below reads the correct value out of the named range into the
var ID, but errors out when I write to the cell. Cell is not locked. Sheet
is not protected. Trust Center allows VBA code. Code is in a seperate
module, not on the sheet code.

Purpose is to increment a value, and save it in a cell on the sheet.

I call the function from another cell, like this: =IF(B4="","",NewID())

I've been at this for hours, researching what could be wrong. About ready to
toss the computer out the window. Anyone have any ideas?

Function NewID()
Dim ID As Long

ID = Range("Test").Cells(1).Value
ID = ID + 1
Range("Test").Cells(1).Value = ID 'This line throws the error

NewID = ID

End Function

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 when cell A1 is not active and xlInsideVertical border formatthrowing error 1004 [email protected] Excel Programming 7 August 7th 08 08:43 PM
Runtime error '1004' Cannot change part of a merged cell [email protected] Excel Programming 0 July 11th 08 02:48 AM
Run-time error 1004: Too many different cell formats Randy Excel Programming 1 June 16th 06 03:08 PM
Getting error 1004 trying to write formula in cell Trefor Excel Programming 6 November 23rd 05 11:10 PM


All times are GMT +1. The time now is 08:04 PM.

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

About Us

"It's about Microsoft Excel"