Inserting date

stanco

New Member
Joined
Mar 16, 2019
Messages
48
Hi, I know there are a lot of discussion on inserting static date using vba, but I need something different. i need one that would insert a dummy date whenever the next cell is empty.

to be more specific, I need a code to reflect the date of the respective surveys in the respective column whenever i input the result, but will display a dummy date when the result is blank. (the surveys are done on ad hoc basis, so some people might have done 3, but some have not even started on it).

I intend to use pivot table to count the number of the survey done over time, so i can't leave any blank in the survey date column.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Survey 1 Date[/TD]
[TD]Result[/TD]
[TD]Survey 2 Date[/TD]
[TD]Result[/TD]
[TD]Survey Date 3[/TD]
[TD]Result[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

appreciate it if anyone can assist or suggest something for me please.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
Sub AddDummyDate()
    Dim rng As Range, Col As Long
    Set rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
    For Col = 2 To 6 Step 2
        On Error Resume Next
        rng.Offset(, Col - 1).SpecialCells(xlCellTypeBlanks) = DateSerial(1999, 12, 31)
        On Error GoTo 0
    Next Col
End Sub

BEFORE

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]Name[/td][td]Survey 1 Date[/td][td]Result[/td][td]Survey 2 Date[/td][td]Result[/td][td]Survey Date 3[/td][td]Result[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]Name01[/td][td]
04/03/2019​
[/td][td][/td][td]
09/03/2019​
[/td][td][/td][td]
11/03/2019​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]Name02[/td][td]
05/03/2019​
[/td][td][/td][td]
10/03/2019​
[/td][td][/td][td]
12/03/2019​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]Name03[/td][td]
06/03/2019​
[/td][td][/td][td]
11/03/2019​
[/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]Name04[/td][td]
07/03/2019​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]Name05[/td][td]
08/03/2019​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]Name06[/td][td]
09/03/2019​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet2[/td][/tr][/table]

AFTER

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]Name[/td][td]Survey 1 Date[/td][td]Result[/td][td]Survey 2 Date[/td][td]Result[/td][td]Survey Date 3[/td][td]Result[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]Name01[/td][td]
04/03/2019​
[/td][td][/td][td]
09/03/2019​
[/td][td][/td][td]
11/03/2019​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]Name02[/td][td]
05/03/2019​
[/td][td][/td][td]
10/03/2019​
[/td][td][/td][td]
12/03/2019​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]Name03[/td][td]
06/03/2019​
[/td][td][/td][td]
11/03/2019​
[/td][td][/td][td]
31/12/1999
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]Name04[/td][td]
07/03/2019​
[/td][td][/td][td]
31/12/1999
[/td][td][/td][td]
31/12/1999
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]Name05[/td][td]
08/03/2019​
[/td][td][/td][td]
31/12/1999
[/td][td][/td][td]
31/12/1999
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]Name06[/td][td]
09/03/2019​
[/td][td][/td][td]
31/12/1999
[/td][td][/td][td]
31/12/1999
[/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet2[/td][/tr][/table]
 
Upvote 0
The above put a date in the column whenever the date is blank

Having read your post again, I think you want date put in if adjacent RESULT column is blank

Code:
Sub AddDummyDate()
    Dim rng As Range, Col As Long
    Set rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
    For Col = 2 To 6 Step 2
        On Error Resume Next
        rng.Offset(, Col).SpecialCells(xlCellTypeBlanks).Offset(, -1) = DateSerial(1999, 12, 31)
        On Error GoTo 0
    Next Col
End Sub

But this does not ensure that there is a date in every cell
 
Last edited:
Upvote 0
If you want BOTH the Date and Result to be blank before amending (eg to prevent a previously entered date being overwritten)
Code:
Sub AddDummyDate()
    Dim rng As Range, rng1 As Range, rng2 As Range, Col As Long
    Set rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
    For Col = 2 To 6 Step 2
        On Error Resume Next
        Set rng1 = rng.Offset(, Col).SpecialCells(xlCellTypeBlanks).Offset(, -1)
        Set rng2 = rng.Offset(, Col - 1).SpecialCells(xlCellTypeBlanks)
        Intersect(rng1, rng2) = DateSerial(1999, 12, 31)
        On Error GoTo 0
    Next Col
End Sub

This would not add a date if Result contains text but Date is empty
 
Last edited:
Upvote 0
Finally

You may want to add default dates automatcally whenever a new record is added in column A

Place this in SHEET module
(right-click on sheet tab \ View Code \ paste code below into code window \ {ALT}{F11} to go back to Excel)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim offS As Long
    If Target.Row < 2 Then Exit Sub   [COLOR=#006400][I]'to ignore headers in row 1[/I][/COLOR]
    If Not Intersect(Range("A:A"), Target) Is Nothing Then
        For offS = 1 To 5 Step 2
                Target.Offset(, offS) = DateSerial(1999, 12, 31)
        Next
    End If
End Sub
 
Upvote 0
Hi Yongle, thank you very much for the codes. I will try it out over the weekends.

On the topic of date, I do not know if this is possible, but let say i have to prepare a token to those who have done two or more surveys. but because there are thousands of rows (i know i can do a filter but i need to do this quite often and i am hoping to make it more "automatic").

Is it possible to extract the entire row from the table out to a fresh sheet whenever i input the start and end date?

taking your table for example (which i have amended slightly), my start date = 10/03/2019 and end date = 18/03/2019

The original table
[TABLE="class: head"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[TH][/TH]
[TH]A[/TH]
[TH]B[/TH]
[TH]C[/TH]
[TH]D[/TH]
[TH]E[/TH]
[TH]F[/TH]
[TH]G[/TH]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
1
[/TD]
[TD]Name[/TD]
[TD]Survey 2 Date[/TD]
[TD]Result[/TD]
[TD]Survey 3 Date[/TD]
[TD]Result[/TD]
[TD]Survey 4 Date[/TD]
[TD]Result[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
2
[/TD]
[TD]Name01[/TD]
[TD]
04/03/2019​
[/TD]
[TD]123[/TD]
[TD]
09/03/2019​
[/TD]
[TD]124[/TD]
[TD]
11/03/2019​
[/TD]
[TD]125[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
3
[/TD]
[TD]Name02[/TD]
[TD]
05/03/2019​
[/TD]
[TD]130[/TD]
[TD]
10/03/2019​
[/TD]
[TD]129[/TD]
[TD]
12/03/2019​
[/TD]
[TD]128[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
4
[/TD]
[TD]Name03[/TD]
[TD]
06/03/2019​
[/TD]
[TD]107[/TD]
[TD]
11/03/2019​
[/TD]
[TD]123[/TD]
[TD]
31/12/1999
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
5
[/TD]
[TD]Name04[/TD]
[TD]
07/03/2019​
[/TD]
[TD]125[/TD]
[TD]
1/02/2019​
[/TD]
[TD][/TD]
[TD]
18/03/2019​
[/TD]
[TD]122[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
6
[/TD]
[TD]Name05[/TD]
[TD]
08/03/2019​
[/TD]
[TD]164[/TD]
[TD]
31/12/1999
[/TD]
[TD][/TD]
[TD]
31/12/1999
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
7
[/TD]
[TD]Name06[/TD]
[TD]
09/03/2019​
[/TD]
[TD]125[/TD]
[TD]
31/12/1999
[/TD]
[TD][/TD]
[TD]
31/12/1999
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid"]
<tbody>[TR]
[TD]Sheet: Sheet2

[/TD]
[/TR]
</tbody>[/TABLE]


Results should shows on a fresh sheet
Start Date:10/03/2019
End Date: 18/03/2019

[TABLE="class: head"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
[TH][/TH]
[TH]A[/TH]
[TH]B[/TH]
[TH]C[/TH]
[TH]D[/TH]
[TH]E[/TH]
[TH]F[/TH]
[TH]G[/TH]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
1
[/TD]
[TD]Name[/TD]
[TD]Survey 2 Date[/TD]
[TD]Result[/TD]
[TD]Survey 3 Date[/TD]
[TD]Result[/TD]
[TD]Survey 4 Date[/TD]
[TD]Result[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
2
[/TD]
[TD]Name01[/TD]
[TD]

[/TD]
[TD][/TD]
[TD]

[/TD]
[TD][/TD]
[TD]
11/03/2019​
[/TD]
[TD]125[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
3
[/TD]
[TD]Name02[/TD]
[TD]

[/TD]
[TD][/TD]
[TD]
10/03/2019​
[/TD]
[TD]129[/TD]
[TD]
12/03/2019​
[/TD]
[TD]128[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
4
[/TD]
[TD]Name03[/TD]
[TD]

[/TD]
[TD][/TD]
[TD]
11/03/2019​
[/TD]
[TD]123[/TD]
[TD]

[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]
5
[/TD]
[TD]Name04[/TD]
[TD]

[/TD]
[TD][/TD]
[TD]

[/TD]
[TD][/TD]
[TD]
18/03/2019​
[/TD]
[TD]122[/TD]
[/TR]
[TR]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E0E0F0]#E0E0F0[/URL] "]

[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid"]
<tbody>[TR]
[TD]Sheet: Sheet2

[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I am away for several days so cannot help now.

But my immediate reaction would be to use Advanced Filter, which can cope with the filtering requirements and can filter results to another sheet
And it could easily be automated with a simple macro

Have a look at this https://www.contextures.com/xladvfilter01.html

Set up criteria range looking similar to this

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
G
[/th][th]
H
[/th][th]
I
[/th][th]
J
[/th][th]
K
[/th][th]
L
[/th][th]
M
[/th][th]
N
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td][/td][td][/td][td]Survey 2 Date[/td][td]Survey 2 Date[/td][td]Survey 3 Date[/td][td]Survey 3 Date[/td][td]Survey 4 Date[/td][td]Survey 4 Date[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td=bgcolor:#C6E0B4]From[/td][td=bgcolor:#C6E0B4]
10/03/2019​
[/td][td]>=10/03/2019[/td][td]<=18/03/2019[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td=bgcolor:#C6E0B4]To[/td][td=bgcolor:#C6E0B4]
18/03/2019​
[/td][td][/td][td][/td][td]>=10/03/2019[/td][td]<=18/03/2019[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]>=10/03/2019[/td][td]<=18/03/2019[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]

This a totally different question from what the title suggests - probably best if you start a new thread to get the help you want.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top