Solved

VBA Static Date

Posted on 2015-02-17
16
62 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 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
Independent Software Vendors: 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

740 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