Inelegant code..

KanMan

Board Regular
Joined
Dec 4, 2005
Messages
100
This piece of code looks downright ugly. Any suggestions in making it look more elegant. Thanks.

Sub changeName()
Leap = Range("YEAR").Value Mod 4
Select Case Range("Quarter")
Case "Summer", "Fall"
With ActiveWorkbook.Names
.Add Name:="Day", RefersTo:="=Sheet1!$A$4:$A$95"
.Add Name:="Date", RefersTo:="=Sheet1!$B$4:$B$95"
.Add Name:="AM", RefersTo:="=Sheet1!$C$4:$C$95"
.Add Name:="EVE", RefersTo:="=Sheet1!$E$4:$E$95"
.Add Name:="PM", RefersTo:="=Sheet1!$G$4:$G$95"
End With
Case "Spring"
With ActiveWorkbook.Names
.Add Name:="Day", RefersTo:="=Sheet1!$A$4:$A$94"
.Add Name:="Date", RefersTo:="=Sheet1!$B$4:$B$94"
.Add Name:="AM", RefersTo:="=Sheet1!$C$4:$C$94"
.Add Name:="EVE", RefersTo:="=Sheet1!$E$4:$E$94"
.Add Name:="PM", RefersTo:="=Sheet1!$G$4:$G$94"
End With
Case "Winter"
If Leap <> 0 Then
With ActiveWorkbook.Names
.Add Name:="Day", RefersTo:="=Sheet1!$A$4:$A$93"
.Add Name:="Date", RefersTo:="=Sheet1!$B$4:$B$93"
.Add Name:="AM", RefersTo:="=Sheet1!$C$4:$C$93"
.Add Name:="EVE", RefersTo:="=Sheet1!$E$4:$E$93"
.Add Name:="PM", RefersTo:="=Sheet1!$G$4:$G$93"
End With
Else
With ActiveWorkbook.Names
.Add Name:="Day", RefersTo:="=Sheet1!$A$4:$A$94"
.Add Name:="Date", RefersTo:="=Sheet1!$B$4:$B$94"
.Add Name:="AM", RefersTo:="=Sheet1!$C$4:$C$94"
.Add Name:="EVE", RefersTo:="=Sheet1!$E$4:$E$94"
.Add Name:="PM", RefersTo:="=Sheet1!$G$4:$G$94"
End With
End If
End Select
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi

Code:
Sub changeName() 
Leap = Range("YEAR").Value Mod 4 
Select Case Range("Quarter") 
Case "Summer", "Fall" 
lastrow = 95
Case "Spring" 
lastrow = 94
Case "Winter" 
If Leap <> 0 Then 
lastrow = 93
Else 
lastrow = 94
End If 
End Select 
With ActiveWorkbook.Names 
.Add Name:="Day", RefersTo:="=Sheet1!$A$4:$A$" & lastrow
.Add Name:="Date", RefersTo:="=Sheet1!$B$4:$B$" & lastrow
.Add Name:="AM", RefersTo:="=Sheet1!$C$4:$C$" & lastrow
.Add Name:="EVE", RefersTo:="=Sheet1!$E$4:$E$" & lastrow
.Add Name:="PM", RefersTo:="=Sheet1!$G$4:$G$" & lastrow
End With 

End Sub

Tony
 
Upvote 0

Forum statistics

Threads
1,196,078
Messages
6,013,301
Members
441,760
Latest member
Sharina

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top