PC Review


Reply
Thread Tools Rate Thread

Column Heading Loop

 
 
Richard
Guest
Posts: n/a
 
      7th Apr 2009
Hi

I have written some code, which works, but I am sure should be much shorter
and simpler.

What I need to do is from column B in row 2 is enter the months from the
begining of the current year to date, in the format JANUARY [2009], c2 would
be FEBRUARY [2009] etc. I am currently calculating the month number ie april
= 4 and then have a series of IF's so that I get the correct number of
columns set up.

January is always added in B2 the rest use the following


If strMonthNum = 2 Then
range("C2").Formula = "February [" & strYear & "]"
End If

If strMonthNum = 3 Then
range("C2").Formula = "February [" & strYear & "]"
range("D2").Formula = "March [" & strYear & "]"
End If


If strMonthNum = 4 Then
range("C2").Formula = "February [" & strYear & "]"
range("D2").Formula = "March [" & strYear & "]"
range("E2").Formula = "April [" & strYear & "]"
End If



etc etc etc.


What is the best way to reduce the number of lines of code?

Thanks
Richard

 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      7th Apr 2009
Try this..

Sub FillMonths()
For intTemp = 1 To 12
Cells(2, 1 + intTemp) = MonthName(intTemp) & " [" & Year(Date) & "]"
Next
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Richard" wrote:

> Hi
>
> I have written some code, which works, but I am sure should be much shorter
> and simpler.
>
> What I need to do is from column B in row 2 is enter the months from the
> begining of the current year to date, in the format JANUARY [2009], c2 would
> be FEBRUARY [2009] etc. I am currently calculating the month number ie april
> = 4 and then have a series of IF's so that I get the correct number of
> columns set up.
>
> January is always added in B2 the rest use the following
>
>
> If strMonthNum = 2 Then
> range("C2").Formula = "February [" & strYear & "]"
> End If
>
> If strMonthNum = 3 Then
> range("C2").Formula = "February [" & strYear & "]"
> range("D2").Formula = "March [" & strYear & "]"
> End If
>
>
> If strMonthNum = 4 Then
> range("C2").Formula = "February [" & strYear & "]"
> range("D2").Formula = "March [" & strYear & "]"
> range("E2").Formula = "April [" & strYear & "]"
> End If
>
>
>
> etc etc etc.
>
>
> What is the best way to reduce the number of lines of code?
>
> Thanks
> Richard
>

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      7th Apr 2009
Jacob

Brilliant thanks.

"Jacob Skaria" wrote:

> Try this..
>
> Sub FillMonths()
> For intTemp = 1 To 12
> Cells(2, 1 + intTemp) = MonthName(intTemp) & " [" & Year(Date) & "]"
> Next
> End Sub
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Richard" wrote:
>
> > Hi
> >
> > I have written some code, which works, but I am sure should be much shorter
> > and simpler.
> >
> > What I need to do is from column B in row 2 is enter the months from the
> > begining of the current year to date, in the format JANUARY [2009], c2 would
> > be FEBRUARY [2009] etc. I am currently calculating the month number ie april
> > = 4 and then have a series of IF's so that I get the correct number of
> > columns set up.
> >
> > January is always added in B2 the rest use the following
> >
> >
> > If strMonthNum = 2 Then
> > range("C2").Formula = "February [" & strYear & "]"
> > End If
> >
> > If strMonthNum = 3 Then
> > range("C2").Formula = "February [" & strYear & "]"
> > range("D2").Formula = "March [" & strYear & "]"
> > End If
> >
> >
> > If strMonthNum = 4 Then
> > range("C2").Formula = "February [" & strYear & "]"
> > range("D2").Formula = "March [" & strYear & "]"
> > range("E2").Formula = "April [" & strYear & "]"
> > End If
> >
> >
> >
> > etc etc etc.
> >
> >
> > What is the best way to reduce the number of lines of code?
> >
> > Thanks
> > Richard
> >

 
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
In a table produce an value by column heading and row heading =?Utf-8?B?bmFmbGFu?= Microsoft Excel Worksheet Functions 1 27th Dec 2005 05:18 PM
Find Column heading and then Delete entire column Kobayashi Microsoft Excel Programming 4 17th Oct 2005 09:09 PM
Go to column heading on start where column heading matches Today() =?Utf-8?B?SmFtZXMgRA==?= Microsoft Excel Worksheet Functions 1 3rd Jul 2004 03:13 PM
Excel 2000 - Missing row heading, but have column heading Peter Microsoft Excel Misc 1 24th Jun 2004 02:02 AM
Changing column/row heading color when a cell in that column/row is selected Christi Microsoft Excel Worksheet Functions 2 13th Jul 2003 12:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:40 AM.