Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default How to define a Range with variant?

Hi all,

I would like to define a range with variant in VBA program. For example,
inside a for-loop, when i=1, the selected range is Range("A1":"F1"); when
i=2, the selected range will be Range("A2":F2") and so on. But I don't know
how to define such "Range". Can anyone help me? Thanks a lot!!

C.F. Yiu


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default How to define a Range with variant?

One way:

For i = 1 to 20
Range("A" & i & ":F" & i).Select
Next i

another:

For i = 1 to 20
Range("A" & i).Resize(1, 6).Select
Next i

another:

For i = 1 to 20
Cells(i, 1).Resize(1, 6).Select
Next i



In article ,
"Yiu Choi Fan" wrote:

Hi all,

I would like to define a range with variant in VBA program. For example,
inside a for-loop, when i=1, the selected range is Range("A1":"F1"); when
i=2, the selected range will be Range("A2":F2") and so on. But I don't know
how to define such "Range". Can anyone help me? Thanks a lot!!

C.F. Yiu

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to define a Range with variant?


Set rng = Range("A" & i & ": " & "F" & i )

or

Set rng = Range("A" & i).Resize(1,6)

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Yiu Choi Fan" wrote in message
...
Hi all,

I would like to define a range with variant in VBA program. For

example,
inside a for-loop, when i=1, the selected range is Range("A1":"F1"); when
i=2, the selected range will be Range("A2":F2") and so on. But I don't

know
how to define such "Range". Can anyone help me? Thanks a lot!!

C.F. Yiu




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default How to define a Range with variant?

Sub test()
Dim i As Long, rng As Range

i = 2

Set rng = Range("A1:F1").Offset(i - 1, 0)
MsgBox rng.Address
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Yiu Choi Fan" wrote in message
...
Hi all,

I would like to define a range with variant in VBA program. For

example,
inside a for-loop, when i=1, the selected range is Range("A1":"F1"); when
i=2, the selected range will be Range("A2":F2") and so on. But I don't

know
how to define such "Range". Can anyone help me? Thanks a lot!!

C.F. Yiu




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default How to define a Range with variant?

Thanks for all your suggestions!!


"Yiu Choi Fan" wrote in message
...
Hi all,

I would like to define a range with variant in VBA program. For

example,
inside a for-loop, when i=1, the selected range is Range("A1":"F1"); when
i=2, the selected range will be Range("A2":F2") and so on. But I don't

know
how to define such "Range". Can anyone help me? Thanks a lot!!

C.F. Yiu






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to define a Range with variant?

try something like this (play with the syntax to get it right)

'your i will become the variable in your cell reference a1:f1 now is ai:fi
'so you need to have it start at 1
i = 1
'do your loop
for-loop
'use the & to concatonate within the range definition splicing in your
'variable i within your range declaration
range("a"&i&":f"&i&"")
'up your counter
i=i+1
'loop
loop

best of luck.

-seth


"Yiu Choi Fan" wrote in message ...
Hi all,

I would like to define a range with variant in VBA program. For example,
inside a for-loop, when i=1, the selected range is Range("A1":"F1"); when
i=2, the selected range will be Range("A2":F2") and so on. But I don't know
how to define such "Range". Can anyone help me? Thanks a lot!!

C.F. Yiu

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default How to define a Range with variant?

Just another one...

Sub Demo()
Dim R As Long
With Range("A:F")
For R = 1 To 10
.Rows(R).Select
Next R
End With
End Sub

Dana DeLouis

"Yiu Choi Fan" wrote in message
...
Hi all,

I would like to define a range with variant in VBA program. For

example,
inside a for-loop, when i=1, the selected range is Range("A1":"F1"); when
i=2, the selected range will be Range("A2":F2") and so on. But I don't

know
how to define such "Range". Can anyone help me? Thanks a lot!!

C.F. Yiu




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
Define a range jlclyde Excel Discussion (Misc queries) 2 April 17th 08 08:26 PM
How do I assign range to variant and use Mike H[_2_] Excel Discussion (Misc queries) 7 June 7th 07 01:40 AM
Define a range based on another named range Basil Excel Worksheet Functions 2 February 21st 05 01:47 PM
Best way to paste a variant array into a range? AnneB Excel Programming 2 April 28th 04 09:57 PM
Testing if Variant is Range or Double? Don Wiss Excel Programming 3 January 26th 04 12:35 AM


All times are GMT +1. The time now is 03:46 AM.

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"