View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Andy Andy is offline
external usenet poster
 
Posts: 8
Default Which VBA code is better

Thanks Dave for the detailed explanation.

"Dave Peterson" wrote in message
...
I'd either version, but I'd modify them slightly.

Option Explicit
Sub Test1()
Dim myRow As Long
myrow = somenumberhere
with activesheet
.Range("A1").value = .Range("ItemTitle").Offset(myRow).value
.Range("B1").value = .Range("QtyTitle").Offset(myRow).value
end with
End Sub

I wouldn't use "As Integer". "As long" is faster for the computer to work
with. And I'd qualify the ranges (like Bob wrote) and I'd specify the
property
that I wanted.

And I'd add "option explicit" to the top of the module--I want to be
forced to
declare my variables.



Andy wrote:

Hi,

I have the following codes and would like to know which one is more
effective and the difference.

Sub Test1()
Dim myRow As Single

Range("A1") = Range("ItemTitle").Offset(myRow)
Range("B1") = Range("QtyTitle").Offset(myRow)

End Sub

Sub Test2()
Dim myRow As Single

Range("A1") = Cells(myRow, Range("ItemTitle").Column)
Range("B1") = Cells(myRow, Range("QtyTitle").Column)

End Sub

I like Test1 more as it is easier to write and seems to me that Test2 may
need a worksheet qualifier if it's referencing another worksheet.

Thanks
Andy, HK


--

Dave Peterson