Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default Column Heading Loop

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Column Heading Loop

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default Column Heading Loop

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
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
I want column heading non-scrolling and rows of each column scrol Q Abdul New Users to Excel 1 July 22nd 08 03:44 PM
How do I put a heading even with a column? Stressing out New Users to Excel 5 December 15th 07 05:28 PM
In a table produce an value by column heading and row heading naflan Excel Worksheet Functions 1 December 27th 05 05:18 PM
Find Column heading and then Delete entire column Kobayashi[_58_] Excel Programming 4 October 17th 05 09:09 PM
Need Macro to Find Column Heading -- if none, then insert new column Jeff[_43_] Excel Programming 0 December 15th 04 07:08 AM


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

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"