VBA Static Date

Hi Experts,

I've researched and entered some changes to the exisiting VBA without success. On the Issue Log tab I would like to have the Requested Date (Column H) to populate the Date from N2 if column B (Snag Item #) is populate.   If Column A (Issue Log #) is populate but Column B is not populated I would like that days date  to be populate (Now) and be static.
EE-Test-Snag-Template-V02.xls
jmac001Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Martin LissConnect With a Mentor Older than dirtCommented:
My code was only updating the date if columns A or B were changed. Change this

If Not Intersect(Target, Range("A9:B" & LastRow)) Is Nothing Then

to

If Not Intersect(Target, Range("A9:P" & LastRow)) Is Nothing Then

and remove or modify the comment above it. Now if anything is changed when you manually add the data, the date code will be triggered.
0
 
Martin LissOlder than dirtCommented:
Here is the first part (I added lines 28 to 34) but I don't understand what "I would like that days date  to be populate (Now) and be static" means.

Private Sub Worksheet_Change(ByVal Target As Range)

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

Dim WS As Worksheet
Dim LastRow As Long
Dim I As Long

'Set ws = ThisWorkbook.Sheets("ISSUE LOG")
Set WS = Sheets("ISSUE LOG")

' get the last row from column A that has a value
LastRow = WS.Range("C" & WS.Rows.Count).End(xlUp).Row
If LastRow < Target.Row Then LastRow = Target.Row

' use the last row to determine how far down to extend the formula
'    ws.Range("A9:A" & lastRow).Formula = "=row()"

If Not Intersect(Target, WS.Columns(3)) Is Nothing Then
    WS.Range("A9:A" & LastRow).Formula = "=IF(C9<>"""",COUNTA($C$9:C9)&""."","""")"
    WS.Range("H9:H" & LastRow).Formula = "=$C$4"
End If

If Not Intersect(Target, WS.Columns(2)) Is Nothing Then
    If Target <> "" Then
        WS.Cells(Target.Row, 6) = WS.Range("N2")
    Else
         WS.Cells(Target.Row, 6) = ""
    End If
End If


For I = 9 To LastRow
    If WS.Range("C" & I).Value <> "" Then
        WS.Range("H" & I).Value = WS.Range("C4") & Chr(10) & WS.Range("C5") & Chr(10) & WS.Range("C6") & " " & WS.Range("E6")
        WS.Range("H" & I).RowHeight = 71
    Else
        WS.Range("H" & I).Value = ""
        WS.Range("H" & I).RowHeight = 15
    End If
Next I

With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub

Open in new window

0
 
jmac001Author Commented:
Hi Martin,

Sorry if I confused you... when I was researching I saw that the Now function was being used.  I just wanted to make sure that if I add a line item tomorrow that it would have 2/18/2015 as the date and all of the other dates would remain whatever date they were when the line item was created.  All of my attempts keep changing all of the line items to the current date.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Martin LissOlder than dirtCommented:
Which cell formula(s) or code uses Now?
0
 
jmac001Author Commented:
None it was a test workbook that I didn't upload.  Not sure if the Now function is the correct function to use was just going based on the forums that I had read.  If there is a better way to get a static date I am welcome to your recommendation.
0
 
Martin LissOlder than dirtCommented:
Which date should be 2/18? Requested Date?
0
 
jmac001Author Commented:
Yes the  Requested Date, would have 2/18 if I added to the list tomorrow.
0
 
Martin LissOlder than dirtCommented:
Give this a try.

Private Sub Worksheet_Change(ByVal Target As Range)

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

Dim WS As Worksheet
Dim LastRow As Long
Dim I As Long

'Set ws = ThisWorkbook.Sheets("ISSUE LOG")
Set WS = Sheets("ISSUE LOG")

' get the last row from column A that has a value
LastRow = WS.Range("C" & WS.Rows.Count).End(xlUp).Row
If LastRow < Target.Row Then LastRow = Target.Row

' use the last row to determine how far down to extend the formula
'    ws.Range("A9:A" & lastRow).Formula = "=row()"

If Not Intersect(Target, WS.Columns(3)) Is Nothing Then
    WS.Range("A9:A" & LastRow).Formula = "=IF(C9<>"""",COUNTA($C$9:C9)&""."","""")"
    WS.Range("H9:H" & LastRow).Formula = "=$C$4"
End If

If Not Intersect(Target, WS.Columns(2)) Is Nothing Then
    If Target <> "" Then
        WS.Cells(Target.Row, 6) = WS.Range("N2")
    Else
        If Target.Offset(0, -1) <> "" Then
            WS.Cells(Target.Row, 6) = Now
        Else
            WS.Cells(Target.Row, 6) = ""
        End If
    End If
End If


For I = 9 To LastRow
    If WS.Range("C" & I).Value <> "" Then
        WS.Range("H" & I).Value = WS.Range("C4") & Chr(10) & WS.Range("C5") & Chr(10) & WS.Range("C6") & " " & WS.Range("E6")
        WS.Range("H" & I).RowHeight = 71
    Else
        WS.Range("H" & I).Value = ""
        WS.Range("H" & I).RowHeight = 15
    End If
Next I

With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub

Open in new window

0
 
jmac001Author Commented:
Hi Martin,

The date is populating when the issue from the snag list is copied over.  However is you enter the issue directly on the Issue Log when column A is populated and column b is not, the date is not populating
0
 
Martin LissOlder than dirtCommented:
I you populate the issue log manually and both columns a and b are populated, which date should be use - Now or N2?
0
 
Martin LissOlder than dirtCommented:
To be sure I think I should ask this way. In each situation below I assume that the the Requested date should either be the current date or the date that's in N2. Please specify which one when...

a is blank and b is not blank
a is not blank and be is blank
a is not blank and b is not blank
0
 
jmac001Author Commented:
a is blank and b is not blank  ---- a should always be populated, but on the off chase that it is not = N2

a is not blank and b is blank = current date

a is not blank and b is not blank = N2

Hope this helps
0
 
Martin LissOlder than dirtCommented:
Private Sub Worksheet_Change(ByVal Target As Range)

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

Dim WS As Worksheet
Dim LastRow As Long
Dim I As Long

'Set ws = ThisWorkbook.Sheets("ISSUE LOG")
Set WS = Sheets("ISSUE LOG")

' get the last row from column A that has a value
LastRow = WS.Range("C" & WS.Rows.Count).End(xlUp).Row
If LastRow < Target.Row Then LastRow = Target.Row

' use the last row to determine how far down to extend the formula
'    ws.Range("A9:A" & lastRow).Formula = "=row()"

If Not Intersect(Target, WS.Columns(3)) Is Nothing Then
    WS.Range("A9:A" & LastRow).Formula = "=IF(C9<>"""",COUNTA($C$9:C9)&""."","""")"
    WS.Range("H9:H" & LastRow).Formula = "=$C$4"
End If

' Look to see if a cell in either columns A or B has been changed
If Not Intersect(Target, Range("A9:B" & LastRow)) Is Nothing Then
    Select Case True
         ' a is blank and b is not blank  ---- a should always be populated,
         ' but on the off chance that it is not set to N2
        Case WS.Cells(Target.Row, 1) = "" And WS.Cells(Target.Row, 2) <> ""
             WS.Cells(Target.Row, 6) = WS.Range("N2")
        ' a is not blank and b is blank set to current date
        Case WS.Cells(Target.Row, 1) <> "" And WS.Cells(Target.Row, 2) = ""
             WS.Cells(Target.Row, 6) = Now
        ' a is not blank and b is not blank set to N2
        Case WS.Cells(Target.Row, 1) <> "" And WS.Cells(Target.Row, 2) <> ""
             WS.Cells(Target.Row, 6) = WS.Range("N2")
        ' a is blank and b is blank set to blank
        Case WS.Cells(Target.Row, 1) = "" And WS.Cells(Target.Row, 2) = ""
             WS.Cells(Target.Row, 6) = ""
    End Select

End If

For I = 9 To LastRow
    If WS.Range("C" & I).Value <> "" Then
        WS.Range("H" & I).Value = WS.Range("C4") & Chr(10) & WS.Range("C5") & Chr(10) & WS.Range("C6") & " " & WS.Range("E6")
        WS.Range("H" & I).RowHeight = 71
    Else
        WS.Range("H" & I).Value = ""
        WS.Range("H" & I).RowHeight = 15
    End If
Next I

With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub

Open in new window

0
 
jmac001Author Commented:
Still no success with the current date.  Attaching test workbook added lines 13-18
EE-Test-Snag-Template-V02.xls
0
 
jmac001Author Commented:
Thank you so much!
0
 
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.