LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Help needed to writing a macro to put the year in column B of a tableusing Table nomenclature & the Evaluate method

I have an Excel Table named "Dates" consisting of 2 columns: column A and column B.

Column A is a list of DATES. I want column B to be a list of the YEARS associated with the dates in column A. The year in cell B2 would be the result of the formula
=year(A2) etc.

The table name is "Dates"
Column A is also named "Dates" and
Column B is named "YY"

I would like an Excel VBA macro to do the following:
For each date in column A of the table "Dates", write the corresponding Year of that date in column B named "YY" but with the following constraints:

1. The macro should NOT use any loops.
2. I want the macro statements to be expressed in terms of Table references such as "Dates[YY]" rather than Range references such as "Range "b2"

The following macro (Solution1) which I found at http://www.ozgrid.com/forum/showthread.php?t=172347 is very close to what I want.

Sub Solution1()
With Range("a2", Range("a" & Rows.Count).End(xlUp))
.Offset(, 1).Value = _
Evaluate("if(" & .Address & "<"""",Year(" & .Address & "),"""")")
End With
End Sub

Solution1 is very fast but it does NOT use table references such as Dates[YY] so for me it is not ideal.

Another macro "Solution2" is also very close to what I want:

Sub Solution2()
Range("Dates[YY]").Value = "=Year(Dates[Dates])"
Range("Dates[YY]").Value = Range("Dates[YY]").Value
End Sub

I do not like solution2 because it is slower than solution1. It is slow because the first statement (Range("Dates[YY]").Value = "=Year(Dates[Dates])") only writes a formula such "=year(A2) in column A and then requires a second statement to convert the formulas in column B to year values such as 2014, 2015 etc. It requires 2 steps rather than just 1.

There has to be a way to write a macro with the speed of solution1 which uses table references like in solution2 rather range references but I can not figure out how to do that. Can you?

Any help or comments will be appreciated.
 
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
Help needed Writing formula that totals data at end of column sid[_3_] Excel Programming 0 May 12th 06 02:39 PM
Help needed Writing formula that totals data at end of column sid[_3_] Excel Programming 0 May 12th 06 02:34 PM
Help needed with macro: Writing contents of two cells to two new columns Big B Excel Programming 0 November 24th 05 04:27 PM
VB evaluate a value in a table's column and display msgbox AusTexRich Excel Discussion (Misc queries) 8 October 10th 05 10:23 PM


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

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

About Us

"It's about Microsoft Excel"