PC Review


Reply
Thread Tools Rate Thread

Condensing working code

 
 
Brad
Guest
Posts: n/a
 
      19th Nov 2008
The macro below works, but can it be simplified?

What this macro is doing is putting an "H" (for holidays) in the appropriate
cells.

Sub HolidaySelection()
Dim i As Long
Dim j As Long
Range("startpoint").Select
Range("B11:AF22").ClearContents
For i = 1 To 12
For j = 1 To 31
If
Application.WorksheetFunction.CountIf(Worksheets("Holidays").Range("F5:F13"),
DateSerial(Range("VacYear"), i, j)) Then
ActiveCell.Offset(i, j) = "H"
End If
Next j
Next i
End Sub

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      19th Nov 2008
Your code is looping 31&12 times for 372 tiumes. My code is looping 9 times
(F5 to F13)

Sub HolidaySelection()
Dim i As Long
Dim j As Long
Dim cell as Range

for each cell in Worksheets("Holidays").Range("F5:F13")
MyMonth = Month(cell)
MyDay = Day(cell)
Range("startpoint").offset(MyMonth,MyDay) = "H"
next cell

"Brad" wrote:

> The macro below works, but can it be simplified?
>
> What this macro is doing is putting an "H" (for holidays) in the appropriate
> cells.
>
> Sub HolidaySelection()
> Dim i As Long
> Dim j As Long
> Range("startpoint").Select
> Range("B11:AF22").ClearContents
> For i = 1 To 12
> For j = 1 To 31
> If
> Application.WorksheetFunction.CountIf(Worksheets("Holidays").Range("F5:F13"),
> DateSerial(Range("VacYear"), i, j)) Then
> ActiveCell.Offset(i, j) = "H"
> End If
> Next j
> Next i
> End Sub
>

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      19th Nov 2008
I didn't declare the variable properly

Sub HolidaySelection()
Dim MyMonth As Long
Dim MyDay As Long
Dim cell as Range

for each cell in Worksheets("Holidays").Range("F5:F13")
MyMonth = month(cell)
MyDay = day(cell)
Range("startpoint").offset(MyMonth,MyDay)
next cell

"Brad" wrote:

> The macro below works, but can it be simplified?
>
> What this macro is doing is putting an "H" (for holidays) in the appropriate
> cells.
>
> Sub HolidaySelection()
> Dim i As Long
> Dim j As Long
> Range("startpoint").Select
> Range("B11:AF22").ClearContents
> For i = 1 To 12
> For j = 1 To 31
> If
> Application.WorksheetFunction.CountIf(Worksheets("Holidays").Range("F5:F13"),
> DateSerial(Range("VacYear"), i, j)) Then
> ActiveCell.Offset(i, j) = "H"
> End If
> Next j
> Next i
> End Sub
>

 
Reply With Quote
 
Brad
Guest
Posts: n/a
 
      20th Nov 2008
Much better - Thank you!!


"Joel" wrote:

> I didn't declare the variable properly
>
> Sub HolidaySelection()
> Dim MyMonth As Long
> Dim MyDay As Long
> Dim cell as Range
>
> for each cell in Worksheets("Holidays").Range("F5:F13")
> MyMonth = month(cell)
> MyDay = day(cell)
> Range("startpoint").offset(MyMonth,MyDay)
> next cell
>
> "Brad" wrote:
>
> > The macro below works, but can it be simplified?
> >
> > What this macro is doing is putting an "H" (for holidays) in the appropriate
> > cells.
> >
> > Sub HolidaySelection()
> > Dim i As Long
> > Dim j As Long
> > Range("startpoint").Select
> > Range("B11:AF22").ClearContents
> > For i = 1 To 12
> > For j = 1 To 31
> > If
> > Application.WorksheetFunction.CountIf(Worksheets("Holidays").Range("F5:F13"),
> > DateSerial(Range("VacYear"), i, j)) Then
> > ActiveCell.Offset(i, j) = "H"
> > End If
> > Next j
> > Next i
> > End Sub
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Condensing VBA code Brad Microsoft Excel Misc 3 9th Sep 2009 05:51 PM
Condensing code Brad Microsoft Excel Programming 1 9th Dec 2008 05:27 PM
Condensing Code =?Utf-8?B?c3RldmU=?= Microsoft Excel Programming 1 23rd Aug 2006 08:37 PM
Need help condensing with-end with code excelnut1954 Microsoft Excel Programming 2 1st May 2006 03:49 PM
Trouble Condensing Code Ikaabod Microsoft Excel Programming 3 11th Apr 2006 09:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:28 AM.