+ Reply to Thread
Results 1 to 7 of 7

Locating Column Position Based on Date

  1. #1
    Forum Contributor
    Join Date
    10-28-2009
    Location
    Portland, Maine
    MS-Off Ver
    Excel 2003
    Posts
    102

    Locating Column Position Based on Date

    Hello,
    I have an issue with a piece of code I'm trying to put together.

    I have a End User defined date with have have set as a variable ChangeDt.

    I need to find the column position on a worksheet (sheet2) based on this date.

    The reason this isn't straight forward for me is that it will be rare that I will have the same date on sheet2 as was entered.

    The dates on sheet2 represent payroll periods and an employee may activate, cancel or put their benefits on hold.

    If an employee called and changed their benefit on 7/3/11 this will fall between the payroll dates of 6/30/11 (column S) and 7/7/11 (column T). Since we won't change the status of the current pay period the column that should be located is Column T for the next pay period. If the change occurs on the start of the pay period (6/30/11) then column S should be found.

    I want to set this column to a variable like Col.

    Does this make sense?

    Any thoughts or suggestions are welcome!

    I've attached my workbook for reference.
    Attached Files Attached Files
    Last edited by yunesm; 08-14-2011 at 09:15 AM. Reason: SOLVED

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Locating Column Position Based on Date

    Hello yunesm,

    This macro will return either the column number the date is in or a zero if the date was not found.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 08-13-2011 at 06:03 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    10-28-2009
    Location
    Portland, Maine
    MS-Off Ver
    Excel 2003
    Posts
    102

    Re: Locating Column Position Based on Date

    Thanks Leith! Thanks for the help.

    I'm doing something wrong. I put the function that you provided in a general module and then added the following code within my sheet module (Sheet1).

    Please Login or Register  to view this content.
    The code doesn't seem to be calling the macro appropriately becuase I'm not getting anything in the message box (I'm also not getting any errors).

    I also tried to change the fuction to a sub and inserted it into the code within my sheet module.

    Thoughts on what I'm going wrong?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Locating Column Position Based on Date

    Hello yunesm,

    If you have placed the macro in standard VBA module like "Module1" etc. then that is where the code should be. Once the code is copied to a standard module, it is visible to all other parts of the VBA Project. So, you don't need to use Application.Run to invoke the macro. The macro takes one argument: the date you want to find the column for.

    Change your code to what is below and it should work fine.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    10-28-2009
    Location
    Portland, Maine
    MS-Off Ver
    Excel 2003
    Posts
    102

    Re: Locating Column Position Based on Date

    Hey Leith,
    I've attached the workbook so you can see what I've done.

    I do have the function in "Module1" but I'm not getting the correct result in the message box.

    At the end of this code I'm using the message box to check the column position before I write the code that I'm actually going to use it in. I'm hoping to get the column position or the 0 in the message box; however, when I run this code all I get is the date for MyDate.

    Please Login or Register  to view this content.
    Sorry for what is likely a silly question. I appreciate all of your help!

    Thanks!
    Attached Files Attached Files

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Locating Column Position Based on Date

    Hello yunesm,

    I am not sure what you did. Copy the code below and paste it into Module1. Run the macro TestA. This works fine on my computer.
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    10-28-2009
    Location
    Portland, Maine
    MS-Off Ver
    Excel 2003
    Posts
    102

    Re: Locating Column Position Based on Date

    Looks like all I needed was some sleep... I have it working now.

    Thanks again for all of your help...!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1