Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Variable Name Changes in Loop Operation


I am testing two variables of similarly named and sequentially
numbered through a do..loop.

I know that I can do that via var() array.

but if I want to change the variable name via string changes, how to
do it?

........
day_1 = date
day_2 = dateadd("d", 7, day_1)

nn=1

do
msgbox "day_" & nn ' not working this way,
nn= nn+1
loop until nn=3

.............

........

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Variable Name Changes in Loop Operation

on 7/25/2011, LiCal supposed :
I am testing two variables of similarly named and sequentially
numbered through a do..loop.

I know that I can do that via var() array.

but if I want to change the variable name via string changes, how to
do it?

.......
day_1 = date
day_2 = dateadd("d", 7, day_1)

nn=1

do
msgbox "day_" & nn ' not working this way,
nn= nn+1
loop until nn=3

............

........


You could use a For...Next loop and use the counter like so...

Dim i As Integer
For i = 1 To 3
MsgBox "Day_" & CStr(i)
Next 'i

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Variable Name Changes in Loop Operation


You could use a For...Next loop and use the counter like so...

* Dim i As Integer
* For i = 1 To 3
* * MsgBox "Day_" & CStr(i)
* Next 'i



I was trying to get the msgbox to show the Date not "Day_1" etc.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Variable Name Changes in Loop Operation

LiCal used his keyboard to write :
You could use a For...Next loop and use the counter like so...

* Dim i As Integer
* For i = 1 To 3
* * MsgBox "Day_" & CStr(i)
* Next 'i



I was trying to get the msgbox to show the Date not "Day_1" etc.


Ok, put the dates in an array and loop the array...

Dim sDates, vaDates, i As Integer
For i = 0 To 14 Step 7
sDates = sDates & "|" & Date + i
Next 'i
sDates = Mid$(sDates, 2)
vaDates = Split(sDates, "|")
For i = LBound(vaDates) To UBound(vaDates)
Debug.Print vaDates(i)
Next 'i

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Variable Name Changes in Loop Operation

You can fine tune that as follows...


Dim sDates, vaDates, i As Integer
For i = 0 To 14 Step 7: sDates = sDates & "|" & Date + i: Next 'i

vaDates = Split(Mid$(sDates, 2), "|")
For i = LBound(vaDates) To UBound(vaDates)
Debug.Print vaDates(i)
Next 'i


BTW, you can change any part of a defined variable after it's been
defined.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Variable Name Changes in Loop Operation

Alternative approach...

Put dates directly into an array, resizing the array as you go.

Sub GetDates()
Dim vaDates(), i As Integer, j As Integer
For i = 0 To 14 Step 7
ReDim Preserve vaDates(j)
vaDates(j) = Date + i: j = j + 1
Next 'i
For i = LBound(vaDates) To UBound(vaDates)
Debug.Print vaDates(i)
Next 'i
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default Variable Name Changes in Loop Operation

VBA doesn't allow you to create variable names this way. But you don't have to,
either. You can use Arithmetic.

Maybe...

Maybe:
msgbox "day_" & date + nn
or
msgbox "day_" & date + (nn * 7)

or with formatting:

msgbox "day_" & format(date + nn, "dd-mmm-yyyy")
or
msgbox "day_" & format(date + (nn * 7), "dd-mmm-yyyy")



On 07/25/2011 21:35, LiCal wrote:

I am testing two variables of similarly named and sequentially
numbered through a do..loop.

I know that I can do that via var() array.

but if I want to change the variable name via string changes, how to
do it?

.......
day_1 = date
day_2 = dateadd("d", 7, day_1)

nn=1

do
msgbox "day_"& nn ' not working this way,
nn= nn+1
loop until nn=3

............

........


--
Dave Peterson
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
Loop with variable name? Mike Excel Discussion (Misc queries) 6 April 25th 09 05:12 AM
Loop with Variable name? Mike Excel Programming 3 April 24th 09 04:20 PM
loop for-next with variable end Valeria Excel Programming 6 November 30th 07 04:14 PM
Change cut/paste operation to cut/insert operation Don Guillett Excel Programming 0 January 17th 07 03:23 PM
For Each ... Next loop - need to reference the loop variable [email protected] Excel Programming 4 July 13th 06 06:12 PM


All times are GMT +1. The time now is 07:30 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"