+ Reply to Thread
Results 1 to 19 of 19

VBA - default contents of a cell based on numerical value of another

Hybrid View

  1. #1
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    VBA - default contents of a cell based on numerical value of another

    Hi Guys,

    I'm pretty new to VBA and am trying to make something work...

    My code is below; it does not work.

    This is probably because the syntax is wrong, but hopefully you can see what I'm trying to do...

    I want the cell in column F to read "Cold" if the cell to the left of it (column E) has a date in it which is over 90 days old.

    If its under 90 days, then i want it to do nothing as there is a data validation drop down box.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Sheets("Sheet1")
    Dim lvalue As Date
    lvalue = Now
    If .Cells("E1").Value > lvalue - 90 Then
    Else
    If .Cells("E1").Value < lvalue - 90 Then .Cells("F1") = "COLD"
    End If
    End With
    End Sub
    Last edited by zbor; 07-11-2016 at 07:51 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Easy VBA problem (for some)

    Hi,

    Are you certain you actually need a macro? Where's the Data validation? Is that in F1 and other column F cells.

    What event should trigger this macro? At the moment it will fire whenever any cell on the sheet is changed.
    I'm guessing that when you pick a value from a dropdown in column F, then, and only then if the date is older than 90 days do you wish to change the data validation to Cold irrespective of what might have been selected in the drop down.

    If so then use

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("F:F")) Is Nothing Then
            If Target.Cells(, 0) < Date - 90 Then
                Application.EnableEvents = False
                Target = "Cold"
                Application.EnableEvents = True
            End If
        End If
    End Sub
    Otherwise upload the workbook.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: Easy VBA problem (for some)

    You've understood what I want, but I can't get it to work for some reason.

    Not sure how to upload a workbook, so its here... https://www.dropbox.com/s/8qn87qlng8...safe.xlsm?dl=0

    I have Data Validation in Column F and conditional formatting in column E.

    If the date in "E" is over 90 days then it turns red, i want the word in "F" to also read "cold" at this point. doesn't have to be a macro if you have an easier way...

    thank you by the way

  4. #4
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: Easy VBA problem (for some)

    Password on the file is "Miami"
    Last edited by jjjjjjjjunit; 06-13-2016 at 07:56 AM.

  5. #5
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    VBA - default contents of a cell based on numerical value of another

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Are you certain you actually need a macro? Where's the Data validation? Is that in F1 and other column F cells.

    What event should trigger this macro? At the moment it will fire whenever any cell on the sheet is changed.
    I'm guessing that when you pick a value from a dropdown in column F, then, and only then if the date is older than 90 days do you wish to change the data validation to Cold irrespective of what might have been selected in the drop down.

    If so then use

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("F:F")) Is Nothing Then
            If Target.Cells(, 0) < Date - 90 Then
                Application.EnableEvents = False
                Target = "Cold"
                Application.EnableEvents = True
            End If
        End If
    End Sub
    Otherwise upload the workbook.
    Hi Richard,

    The above script gives me the desired outcome, but it is triggered in the wrong way. I need it to be automated, so that as soon as a date becomes older than 90 days, it triggers the even (change value of adjacent cell ("E") to "cold").

    In its current format, it merely restricts the user entry options to "cold", but the user still has to actively attempt to change the value in that cell.

    any suggestions? I can't seem to crack it.
    Last edited by jjjjjjjjunit; 07-11-2016 at 07:43 AM. Reason: incorrect title

  6. #6
    Valued Forum Contributor
    Join Date
    01-08-2011
    Location
    Portsmouth, England
    MS-Off Ver
    Excel 2007 to 2016
    Posts
    456

    Re: Easy VBA problem (for some)

    Try this in column F:

    =IF(TODAY()-90>E29,"Cold","")
    Excel is a constant learning process and it's great to help each other. If any of us have helped you today, a click on the "reputation" star on the left is appreciated.

  7. #7
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: Easy VBA problem (for some)

    problem with this, is that as soon as it is manually corrected to something else from the drop down box, then the formula will go. I need the formula to kick in again after another 90 days pass since editing.

  8. #8
    Valued Forum Contributor
    Join Date
    01-08-2011
    Location
    Portsmouth, England
    MS-Off Ver
    Excel 2007 to 2016
    Posts
    456

    Re: Easy VBA problem (for some)

    The formula solution won't work if you are still able to overwrite it with a selection from a drop down list, so it looks like Richard's vba suggestion is better.

    He posted a couple of questions about how you want to trigger the change.

  9. #9
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: Easy VBA problem (for some)

    Richards solution is what I was gunning for, though I put it into my worksheet and it didn't work; looking at it, it doesn't factor in column E at all (which is where the date is..

    Richard: Dates are in column"E" and data validation is in column "F". I basically want each celling in "F" to default to "Cold" when its neighbouring cell in "E" contains a date that is over 90 days.

    that make sensE?

  10. #10
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Easy VBA problem (for some)

    Quote Originally Posted by jjjjjjjjunit View Post
    Richards solution is what I was gunning for, though I put it into my worksheet and it didn't work; looking at it, it doesn't factor in column E at all (which is where the date is..

    Richard: Dates are in column"E" and data validation is in column "F". I basically want each celling in "F" to default to "Cold" when its neighbouring cell in "E" contains a date that is over 90 days.

    that make sensE?
    It does factor in column E although it's not immediately obvious. The macro fires when you make a change in column F. The code
    Formula: copy to clipboard
    If Target.Cells(, 0)


    test to see if the cell relative to the target on the same row - that's the default "," in the parentheses, and in the column to the left, that's the "0" is 90 days ago or more. i.e. the cell one cell to the left of an F cell is an E cell.

  11. #11
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: Easy VBA problem (for some)

    Hi Richard, Thankyou for the explanation... I'm on the same page now; the code works perfectly, and in response to your very first question, how can I change this so that it is triggered automatically when the date exceeds 90 days? Do I simply make the macro fire when the workbook is opened?

  12. #12
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,043

    Re: Easy VBA problem (for some)

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title go to your first post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Never use Merged Cells in Excel

  13. #13
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: Easy VBA problem (for some)

    My apologies.. I was merely hoping to attract someones attention by stating that it was an easy solve for someone proficient in VBA. I haven't been able to change the title in the initial post, as there is no "Edit" button, but I have changed the most recent.

  14. #14
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,043

    Re: VBA - default contents of a cell based on numerical value of another

    I've change it now for you.
    People here are not attracted by complexity but by challenge. Proper title can narrow interested solvers much better.

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA - default contents of a cell based on numerical value of another

    Hi,

    I think the only real alteration required is to monitor column E rather than column F.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cell As Range
        
        On Error GoTo catch
        If Not Intersect(Target, Range("E:E")) Is Nothing Then
                Application.EnableEvents = False
            For Each cell In Intersect(Target, Range("E:E")).Cells
                If IsDate(cell.Value) Then
                    If cell.Value < Date - 90 Then cell.Offset(, 1).Value2 = "Cold"
                End If
            Next cell
        End If
    
    catch:
        Application.EnableEvents = True
    End Sub

  16. #16
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: VBA - default contents of a cell based on numerical value of another

    Quote Originally Posted by xlnitwit View Post
    Hi,

    I think the only real alteration required is to monitor column E rather than column F.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cell As Range
        
        On Error GoTo catch
        If Not Intersect(Target, Range("E:E")) Is Nothing Then
                Application.EnableEvents = False
            For Each cell In Intersect(Target, Range("E:E")).Cells
                If IsDate(cell.Value) Then
                    If cell.Value < Date - 90 Then cell.Offset(, 1).Value2 = "Cold"
                End If
            Next cell
        End If
    
    catch:
        Application.EnableEvents = True
    End Sub
    thank you xlnitwit, however i think i may have made a mistake in the explanation.

    Col "E" = a date

    Col "F" = a word

    if the date in "E" = over 90 days old, then "F" should = the word "cold"(unless is already reads "Cold")

  17. #17
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA - default contents of a cell based on numerical value of another

    No, that was clear. The code monitors column E for changes in the date entered, and will subsequently update column F if necessary.

  18. #18
    Registered User
    Join Date
    05-25-2015
    Location
    london
    MS-Off Ver
    2007
    Posts
    54

    Re: VBA - default contents of a cell based on numerical value of another

    I see.. I was testing it incorrectly...

    This now works when I change the date. I suppose the only way to test if its automated is by setting some dates to 89 days and waiting until tomorrow?

    Also, is there a way I can add a caveat to this? i.e. If "F" = "do not contact" the do nothing?
    Last edited by jjjjjjjjunit; 07-11-2016 at 12:14 PM.

  19. #19
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA - default contents of a cell based on numerical value of another

    You can certainly add a caveat. I fear I may have misjudged your requirement though. If you require the colouring to be 'live' so to speak, you will need to determine what should trigger the changes. I suspect the Workbook_Open event would also be necessary. To that end, add this code to a normal module:

    Sub colourDates(DateRange As Range)
        Dim cell                  As Range
        On Error GoTo catch
        Application.EnableEvents = False
        For Each cell In DateRange.Cells
    
            If IsDate(cell.Value) Then
                If cell.Value < Date - 90 And LCase$(cell.Offset(, 1).Value) <> "do not contact" Then
                    cell.Offset(, 1).Value2 = "Cold"
                End If
            End If
        Next cell
    
    catch:
        Application.EnableEvents = True
    End Sub
    You may then alter the earlier Worksheet_Change code to simply this:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("E:E")) Is Nothing Then
            colourDates Intersect(Target, Range("E:E")).Cells
        End If
    End Sub
    and lastly change the Workbook_Open code to use the new routine as well:
    Private Sub Workbook_Open()
        Dim lastRow               As Long
        Sheets("SalesLog").Visible = True
        Sheets("Orders").Visible = True
        Sheets("Reporting").Visible = True
        Sheets("Settings").Visible = xlHidden
        Sheets("DataBase").Visible = True
        Sheets("Trends").Visible = True
        Sheets("Compare").Visible = True
        Sheets("Trend Data").Visible = xlHidden
        Sheets("Client File").Visible = True
    
        Sheets("Reporting").Activate
    
    
        Sheets("Warning").Visible = xlVeryHidden
        With Sheets("Database")
            If .AutoFilterMode Then
                If .FilterMode Then .ShowAllData
            End If
            lastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
            colourDates .Range("E5:E" & lastRow)
        End With
    End Sub
    Don
    Please remember to mark your thread 'Solved' when appropriate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Easy If problem
    By lky2k23 in forum Excel General
    Replies: 2
    Last Post: 10-28-2011, 06:41 AM
  2. Easy problem
    By bradyj2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2011, 02:34 PM
  3. Calculation problem - Easy one?
    By Andydayuk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2010, 01:19 AM
  4. easy vba problem
    By northernstar197 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-24-2007, 03:49 PM
  5. Problem with Formulas - Probably an easy way around it!!! HELP
    By scottp in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-17-2006, 02:12 AM
  6. easy (?) subtraction problem
    By Tim Marsh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-07-2006, 02:30 PM
  7. Easy IF/Else Problem
    By Paul987 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-21-2006, 10:45 AM
  8. [SOLVED] easy problem for programmers
    By Freddy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-09-2005, 12:06 PM

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