Solved

VBA Static Date

Posted on 2015-02-17
16
53 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
  • 9
  • 7
16 Comments
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
Which cell formula(s) or code uses Now?
0
 

Author Comment

by:jmac001
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
Which date should be 2/18? Requested Date?
0
 

Author Comment

by:jmac001
Comment Utility
Yes the  Requested Date, would have 2/18 if I added to the list tomorrow.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
Still no success with the current date.  Attaching test workbook added lines 13-18
EE-Test-Snag-Template-V02.xls
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
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
Comment Utility
Thank you so much!
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now