Set range to value from another sheet

G

Gustaf

What's wrong with this code?

Range("Sheet1!B4:AE4") = Cells(2, 1)

This is written in the Sheet2 code, and it's supposed to change a range
of cells on Sheet1 to a single value set on Sheet2. I get the 1004 error
(i.e. undefined error).

Gustaf
 
I

Incidental

Hi Gustaf

I'm not sure if you can apply a value to a whole range without
iterating through it but the code below will do what you want

Option Explicit
Dim MyCell, MyRng As Range
Dim MyStr As String

Private Sub CommandButton1_Click()

Set MyRng = Sheets("Sheet1").[B4:AE4]

MyStr = Cells(2, 1).Value

For Each MyCell In MyRng

MyCell.Value = MyStr

Next

End Sub

Hope this helps

Steve
 
G

Gustaf

Incidental said:
I'm not sure if you can apply a value to a whole range without
iterating through it but the code below will do what you want

If done on the same sheet, you can apply a value to a whole range. So
this is something that happens only when you try apply it on another
sheet. But thanks for the solution. I'll try that.

Gustaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top