View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Dave Dave is offline
external usenet poster
 
Posts: 1,388
Default How Do you Change the text/value of Range in a Function?

Is it possible to change the values of a range within a function? For example:

This Works...

Sub MyFunction()
Dim i As Integer
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Range("A1:A5")
Set rng2 = Range("G1:G5")
rng1.Select
rng2.Select
rng2.Clear

For i = rng1.Row To rng1.Rows.Count
rng2.Cells(i, 1) = rng1.Cells(i, 1)
rng2.Cells(i, 2) = rng1.Cells(i, 2)
Next
End Sub

But this doesn't...
Function MyFunction()
Dim i As Integer
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Range("A1:A5")
Set rng2 = Range("G1:G5")
rng1.Select
rng2.Select
rng2.Clear

For i = rng1.Row To rng1.Rows.Count
rng2.Cells(i, 1) = rng1.Cells(i, 1)
rng2.Cells(i, 2) = rng1.Cells(i, 2)
Next
End Function

The function will have an error at the statement:
rng2.Cells(i, 1) = rng1.Cells(i, 1)

Can someone show me how to accomplish this?

TIA