Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Loop a specific number of times

Hi,

I have been reading the threads on looping and I can't find an answer to
this: how can I loop a specific number of times? I have a macro that I
repeat daily but on Fridays I need to do the same macro for Fri, Sat, Sun,
and possibly any holidays on Mon. I have a WORKDAY formula that tells me how
many days I need to repeat the process (3 for a normal weekend, 4 if there is
a holiday on Mon).

Here is my current VBA for the weekend that only does 3 days:

Rows("7:11").Select
Selection.Copy
Range("A6:A20").Select
Selection.Insert Shift:=xlDown
'Clears memo#, income, and any messages
Range("B6:B20,M6:M20,P6:P20").Select
Selection.ClearContents
'Changes the color of Saturday to red
Range("G11:I15").Select
Selection.Font.ColorIndex = 3
'Saturday
Selection.FormulaArray = "=TODAY()+1"
Range("G16:I20").Select
'Sunday
Selection.FormulaArray = "=TODAY()+2"
Range("M21").Select
ActiveCell = "Prepared By: " & Application.UserName
Rows("22:22").Select
Selection.Insert Shift:=xlDown
Range("M6").Select

Is there a 'smart' loop that knows how many days to copy the selected
information contained in rows 7:11?

Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,104
Default Loop a specific number of times

A FOR structure is used when you know the number of times you want to repeat
an operation

For J = to 10
do this
do that
Next J


Sorry, I do not have time to put this in the context of what you already
have. See if it helps and them come back with more questions
best wishes
--
Bernard V Liengme
Microsoft Excel MVP
http://people.stfx.ca/bliengme
remove caps from email

"andy" wrote in message
...
Hi,

I have been reading the threads on looping and I can't find an answer to
this: how can I loop a specific number of times? I have a macro that I
repeat daily but on Fridays I need to do the same macro for Fri, Sat, Sun,
and possibly any holidays on Mon. I have a WORKDAY formula that tells me
how
many days I need to repeat the process (3 for a normal weekend, 4 if there
is
a holiday on Mon).

Here is my current VBA for the weekend that only does 3 days:

Rows("7:11").Select
Selection.Copy
Range("A6:A20").Select
Selection.Insert Shift:=xlDown
'Clears memo#, income, and any messages
Range("B6:B20,M6:M20,P6:P20").Select
Selection.ClearContents
'Changes the color of Saturday to red
Range("G11:I15").Select
Selection.Font.ColorIndex = 3
'Saturday
Selection.FormulaArray = "=TODAY()+1"
Range("G16:I20").Select
'Sunday
Selection.FormulaArray = "=TODAY()+2"
Range("M21").Select
ActiveCell = "Prepared By: " & Application.UserName
Rows("22:22").Select
Selection.Insert Shift:=xlDown
Range("M6").Select

Is there a 'smart' loop that knows how many days to copy the selected
information contained in rows 7:11?

Thanks!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Loop a specific number of times

bit of a stab in the dark but see if following gives some ideas how to solve
your problems.
You will note that I have removed all the "select" statements ascyou rarely
need to do this.

Sub loopData()
Dim Mysheet As Worksheet
Dim MyRange As Range

Set Mysheet = ThisWorkbook.Worksheets("Sheet1") '<< change sheet name
'as required

With Mysheet

'get range where number of days stored
Set MyRange = .Range("D1") '<< change range as required

'check value not empty
If MyRange.Value < "" Then


For na = 1 To MyRange.Value

.Rows("7:11").Copy

.Range("A6:A20").Insert Shift:=xlDown

'Clears memo#, income, and any messages
.Range("B6:B20,M6:M20,P6:P20").ClearContents

'Changes the color of Saturday to red
With .Range("G11:I15")

.Font.ColorIndex = 3
'Saturday
.FormulaArray = "=TODAY()+1"

End With

'Sunday
.Range("G16:I20").FormulaArray = "=TODAY()+2"


.Range("M21").Value = "Prepared By: " & Application.UserName


.Rows("22:22").Insert Shift:=xlDown

Next na

Else

MsgBox "No Value Set In Range"

End If

End With

End Sub
--
jb


"andy" wrote:

Hi,

I have been reading the threads on looping and I can't find an answer to
this: how can I loop a specific number of times? I have a macro that I
repeat daily but on Fridays I need to do the same macro for Fri, Sat, Sun,
and possibly any holidays on Mon. I have a WORKDAY formula that tells me how
many days I need to repeat the process (3 for a normal weekend, 4 if there is
a holiday on Mon).

Here is my current VBA for the weekend that only does 3 days:

Rows("7:11").Select
Selection.Copy
Range("A6:A20").Select
Selection.Insert Shift:=xlDown
'Clears memo#, income, and any messages
Range("B6:B20,M6:M20,P6:P20").Select
Selection.ClearContents
'Changes the color of Saturday to red
Range("G11:I15").Select
Selection.Font.ColorIndex = 3
'Saturday
Selection.FormulaArray = "=TODAY()+1"
Range("G16:I20").Select
'Sunday
Selection.FormulaArray = "=TODAY()+2"
Range("M21").Select
ActiveCell = "Prepared By: " & Application.UserName
Rows("22:22").Select
Selection.Insert Shift:=xlDown
Range("M6").Select

Is there a 'smart' loop that knows how many days to copy the selected
information contained in rows 7:11?

Thanks!

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
Why can the number of iterations repeat many times as a loop?? Jorge Luis Excel Discussion (Misc queries) 0 February 1st 08 04:15 PM
Loop Macro a variable number of times thesaxonuk Excel Discussion (Misc queries) 11 October 31st 06 06:05 PM
Count the number of times a cell value is within a specific range Everett Excel Worksheet Functions 4 September 2nd 06 10:54 PM
Count number of times a specific number is displayed in a cell ran subs Excel Worksheet Functions 1 June 27th 05 05:01 PM
Count number of times a specific number is displayed in cells subs[_2_] Excel Programming 1 June 27th 05 03:15 PM


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