+ Reply to Thread
Results 1 to 5 of 5

VBA: automatically input non-updating dates?

  1. #1
    Registered User
    Join Date
    10-30-2010
    Location
    DC, USA
    MS-Off Ver
    Mac Excel 2011
    Posts
    4

    VBA: automatically input non-updating dates?

    Hello All,

    I need to set up an if-then code so that if I put data into a cell between column E15:E115, the date I entered the data will appear in the corresponding cell number in column "C". I need to do the same for data in M15:M115 and column "K". The date cannot change once input. For example, if I type something in E15 on 30.10.2010, I need 30.10.2010 to appear in C15 and to not update the next time I open the file.

    I found a code for this purpose, but it is not working properly. Here it is:

    Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Range("E15:E115")) Is Nothing Then

    With Target(1, -1)

    .Value = Date

    .EntireColumn.AutoFit

    End With

    End If

    End Sub

    This code results in one of two problems: 1) If the column between these two columns is empty (column “D”), then I can only type one character into E15, for example, before I am locked from typing anymore. In other words, it locks my keyboard for everything but typing in the formula bar, and this includes hitting 'enter' or 'tab' to leave the formula bar. It is very inconvenient. 2) If, however, the column between these two columns, “D”, has anything in it, the code does absolutely nothing.

    I am hoping this is not a 2011 Excel issue for Mac because the only reason I bought it was to use the VBA ability for this. Hopefully someone has a correction to this code or a suggestion.

    Please help.

    Thank you
    Last edited by Jedouard; 10-30-2010 at 08:02 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: VBA: automatically input non-updating dates?

    Hi Jedouard
    I thought we had a few more months for the release of Excel 2011 for the Mac. I guess not by reading
    http://www.macworld.com/article/1551...el2011faq.html

    Welcome to the world of Excel VBA.

    Your code needs to be behind the worksheet, not a module, as I assume it is.
    Try putting your code in an event called Worksheet_SelectionChange instead of Worksheet_Change.

    That should trigger the event after you move your cursor to another cell instead of just typing a singel character.

    I believe the With Target(1,-1) puts the stuff entered one row down and one column back. If you are in E15 and want it to get into C15, try Target(0,-2).

    I hope that helps. Now you can start reading a lot of the VBA hints and tricks that work on the PC and they should also work on the MAC.

    More on http://www.mvps.org/dmcritchie/excel/event.htm
    Last edited by MarvinP; 10-30-2010 at 08:31 PM.

  3. #3
    Registered User
    Join Date
    10-30-2010
    Location
    DC, USA
    MS-Off Ver
    Mac Excel 2011
    Posts
    4

    Re: VBA: automatically input non-updating dates?

    Hi Marvin,

    Your solution is a step in the right direction. Now I can type fully and the correct column does display the date. However, the problem now is that the date immediately displays before typing anything, i.e., as soon as I select the field. Is there any way to make it so that it only places the date in once I have actually typed something in the cell and then moved out of the cell, not just selected it? I believe there is probably an event called Worksheet

    If I were the only one working with this spreadsheet, it would not be a problem, but I have a lot of people inexperienced with Excel who are going to be clicking everywhere and freak out when all these dates start appearing.

    Also, I wanted to let you know that the (1,-1) ended up being right. (1,1) is apparently the cell you are in, (0,-2) would be three columns to the right and one row down.

  4. #4
    Registered User
    Join Date
    10-30-2010
    Location
    DC, USA
    MS-Off Ver
    Mac Excel 2011
    Posts
    4

    Re: VBA: automatically input non-updating dates?

    Hi again Marvin,

    I actually got it to work. Surprisingly it was with Worksheet_Change rather than Worksheet_SelectionChange.

    The date now only appears after having input info into the cell and then leaving the cell. There is one problem, but it is more aesthetic in nature:. For some reason, when you select the a cell in the input ("if") column, the entire date ("then") column becomes highlighted in a blue border. I do not care much about this, but if you have an idea how to avoid this, I will give it a shot.

    Here is the event code so far:

    Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Range("M15:M115")) Is Nothing Then

    With Target(1, -1)

    .Value = Date

    .EntireColumn.AutoFit

    End With

    End If

    If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Range("E15:E115")) Is Nothing Then

    With Target(1, -1)

    .Value = Date

    .EntireColumn.AutoFit

    End With

    End If

    End Sub

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: VBA: automatically input non-updating dates?

    Hi,
    Right before your End Sub put a line like:
    Range("E14").select

    That should keep the entire column from looking like it is selected.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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