Solved

VBA Static Date

Posted on 2015-02-17
16
71 Views
Last Modified: 2016-02-10
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
0
Comment
Question by:jmac001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40614430
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
 

Author Comment

by:jmac001
ID: 40614496
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
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40614511
Which cell formula(s) or code uses Now?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jmac001
ID: 40614656
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
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40614694
Which date should be 2/18? Requested Date?
0
 

Author Comment

by:jmac001
ID: 40614703
Yes the  Requested Date, would have 2/18 if I added to the list tomorrow.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40614732
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
 

Author Comment

by:jmac001
ID: 40615142
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
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40615161
I you populate the issue log manually and both columns a and b are populated, which date should be use - Now or N2?
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40615458
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
 

Author Comment

by:jmac001
ID: 40616851
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
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40616939
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
 

Author Comment

by:jmac001
ID: 40617010
Still no success with the current date.  Attaching test workbook added lines 13-18
EE-Test-Snag-Template-V02.xls
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40617036
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
 

Author Closing Comment

by:jmac001
ID: 40617507
Thank you so much!
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40617513
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

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question