Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Simplify Code

Hello,

I have the following code to define a range based on month which is
sorted date wise.

is there a way to avoid repeating the code 12 times for all months?

also if there is no dates for a specific month say for november to
avoid causing an error while selecting the range since the range will
be empty?

Thanks


Dim iStart As Long
Dim iEnd As Long
Dim Rng As Range

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngJan"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngFeb"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngMar"

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Simplify Code

Dim iStart As Long
Dim iEnd As Long
Dim Rng As Range
Dim i As Long

With Sheets("Daily")
For i = 1 To 12
iStart = _
.Evaluate("=MIN(IF(MONTH(DlyAll)=" & i & ",ROW(DlyAll)))")
iEnd = _
.Evaluate("=MAX(IF(MONTH(DlyAll)=1,ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rng" & Format(DateValue("01-" & i), "mmm")
Next i
End With


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Soniya" wrote in message
oups.com...
Hello,

I have the following code to define a range based on month which is
sorted date wise.

is there a way to avoid repeating the code 12 times for all months?

also if there is no dates for a specific month say for november to
avoid causing an error while selecting the range since the range will
be empty?

Thanks


Dim iStart As Long
Dim iEnd As Long
Dim Rng As Range

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngJan"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngFeb"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngMar"



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Simplify Code

Thanks for your help.

Unfortunately it is not working for me ????

to avoid the error of having empty range i put on error resume next



Bob Phillips wrote:
Dim iStart As Long
Dim iEnd As Long
Dim Rng As Range
Dim i As Long

With Sheets("Daily")
For i = 1 To 12
iStart = _
.Evaluate("=MIN(IF(MONTH(DlyAll)=" & i & ",ROW(DlyAll)))")
iEnd = _
.Evaluate("=MAX(IF(MONTH(DlyAll)=1,ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rng" & Format(DateValue("01-" & i), "mmm")
Next i
End With


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Soniya" wrote in message
oups.com...
Hello,

I have the following code to define a range based on month which is
sorted date wise.

is there a way to avoid repeating the code 12 times for all months?

also if there is no dates for a specific month say for november to
avoid causing an error while selecting the range since the range will
be empty?

Thanks


Dim iStart As Long
Dim iEnd As Long
Dim Rng As Range

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngJan"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngFeb"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngMar"


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Simplify Code

In what way?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Soniya" wrote in message
oups.com...
Thanks for your help.

Unfortunately it is not working for me ????

to avoid the error of having empty range i put on error resume next



Bob Phillips wrote:
Dim iStart As Long
Dim iEnd As Long
Dim Rng As Range
Dim i As Long

With Sheets("Daily")
For i = 1 To 12
iStart = _
.Evaluate("=MIN(IF(MONTH(DlyAll)=" & i &

",ROW(DlyAll)))")
iEnd = _
.Evaluate("=MAX(IF(MONTH(DlyAll)=1,ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rng" & Format(DateValue("01-" & i), "mmm")
Next i
End With


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Soniya" wrote in message
oups.com...
Hello,

I have the following code to define a range based on month which is
sorted date wise.

is there a way to avoid repeating the code 12 times for all months?

also if there is no dates for a specific month say for november to
avoid causing an error while selecting the range since the range will
be empty?

Thanks


Dim iStart As Long
Dim iEnd As Long
Dim Rng As Range

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=1, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngJan"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=2, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngFeb"

iStart = _
Sheets("Daily").Evaluate("=MIN(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
iEnd =
Sheets("Daily").Evaluate("=MAX(IF(MONTH(DlyAll)=3, ROW(DlyAll)))")
Set Rng = Range("A" & iStart & ":A" & iEnd)
Rng.Name = "rngMar"




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
Can I simplify a VB code?????? hoyos Excel Discussion (Misc queries) 1 November 6th 09 09:06 PM
Simplify code Inkel Excel Worksheet Functions 3 March 27th 09 05:52 PM
Need to simplify code alexwren Excel Discussion (Misc queries) 7 August 15th 06 08:07 PM
Simplify this code Scott Excel Programming 2 February 8th 06 03:56 AM
simplify code matt Excel Discussion (Misc queries) 3 September 28th 05 11:53 PM


All times are GMT +1. The time now is 03:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"