Excel UserForm: if date = today update row, else, create new row
up vote
0
down vote
favorite
I am working on a user form that will record counts at certain times of the day.
I automatically add the date and day to the first two fields. There are different fields for one day of the week compared to the others, hence the if statement:
Private Sub UserForm_Initialize()
If Format(Date, "ddd") <> "Sat" Then
DateWkd.Value = Format(Date, "mm/dd/yy")
DayWkd.Value = Format(Date, "ddd")
Else
DateSat.Value = Format(Date, "mm/dd")
DaySat.Value = Format(Date, "ddd")
End If
End Sub
Since people will be submitting data at different times of the day, how do I find the last row if the day value equals today's day and allow the form inputs to update the row, or create a new row if the date doesn't match?
excel excel-vba userform
add a comment |
up vote
0
down vote
favorite
I am working on a user form that will record counts at certain times of the day.
I automatically add the date and day to the first two fields. There are different fields for one day of the week compared to the others, hence the if statement:
Private Sub UserForm_Initialize()
If Format(Date, "ddd") <> "Sat" Then
DateWkd.Value = Format(Date, "mm/dd/yy")
DayWkd.Value = Format(Date, "ddd")
Else
DateSat.Value = Format(Date, "mm/dd")
DaySat.Value = Format(Date, "ddd")
End If
End Sub
Since people will be submitting data at different times of the day, how do I find the last row if the day value equals today's day and allow the form inputs to update the row, or create a new row if the date doesn't match?
excel excel-vba userform
1
if you know which column you want to be in, you can use cells(rows.count,varDay).end(xlup).row to find the last row in that respective column. Where varDay is the variable column for finding the last day used. you can then check that cell to see if same as today, then determine if values go into last row or last row + 1
– Cyril
Nov 9 at 13:10
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am working on a user form that will record counts at certain times of the day.
I automatically add the date and day to the first two fields. There are different fields for one day of the week compared to the others, hence the if statement:
Private Sub UserForm_Initialize()
If Format(Date, "ddd") <> "Sat" Then
DateWkd.Value = Format(Date, "mm/dd/yy")
DayWkd.Value = Format(Date, "ddd")
Else
DateSat.Value = Format(Date, "mm/dd")
DaySat.Value = Format(Date, "ddd")
End If
End Sub
Since people will be submitting data at different times of the day, how do I find the last row if the day value equals today's day and allow the form inputs to update the row, or create a new row if the date doesn't match?
excel excel-vba userform
I am working on a user form that will record counts at certain times of the day.
I automatically add the date and day to the first two fields. There are different fields for one day of the week compared to the others, hence the if statement:
Private Sub UserForm_Initialize()
If Format(Date, "ddd") <> "Sat" Then
DateWkd.Value = Format(Date, "mm/dd/yy")
DayWkd.Value = Format(Date, "ddd")
Else
DateSat.Value = Format(Date, "mm/dd")
DaySat.Value = Format(Date, "ddd")
End If
End Sub
Since people will be submitting data at different times of the day, how do I find the last row if the day value equals today's day and allow the form inputs to update the row, or create a new row if the date doesn't match?
excel excel-vba userform
excel excel-vba userform
edited Nov 9 at 13:02
asked Nov 9 at 12:43
mattrweaver
306522
306522
1
if you know which column you want to be in, you can use cells(rows.count,varDay).end(xlup).row to find the last row in that respective column. Where varDay is the variable column for finding the last day used. you can then check that cell to see if same as today, then determine if values go into last row or last row + 1
– Cyril
Nov 9 at 13:10
add a comment |
1
if you know which column you want to be in, you can use cells(rows.count,varDay).end(xlup).row to find the last row in that respective column. Where varDay is the variable column for finding the last day used. you can then check that cell to see if same as today, then determine if values go into last row or last row + 1
– Cyril
Nov 9 at 13:10
1
1
if you know which column you want to be in, you can use cells(rows.count,varDay).end(xlup).row to find the last row in that respective column. Where varDay is the variable column for finding the last day used. you can then check that cell to see if same as today, then determine if values go into last row or last row + 1
– Cyril
Nov 9 at 13:10
if you know which column you want to be in, you can use cells(rows.count,varDay).end(xlup).row to find the last row in that respective column. Where varDay is the variable column for finding the last day used. you can then check that cell to see if same as today, then determine if values go into last row or last row + 1
– Cyril
Nov 9 at 13:10
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Will write out a little bit more as comments aren't great for code.
In general, you should appropriately qualify references, so in this case with using your userform, you will need to specify the sheet/etc.
Dim lr as Long, varDay as Long
varDay = 1 'assumes using ColumnA, but you could make this a Find() function if necessary
With Sheets("Data")
lr = .Cells( .Rows.Count, varDay).End(xlUp).Row
If DateTextBox.Value <> .Cells(lr,varDay).Value Then 'Not sure if you want just Date (todays' date, no time) or the value in your input for the comparison (gave arbitrary name for textbox example)
'Do your thing
End if
End With
This would be in your command button for entering data, to determine where it would go. If you need to pull data from the sheet on initialize, you would then set textbox.value = .cell references... note that these two situations are not within the same module.
Thanks for your response and the explanation. That did it.
– mattrweaver
Nov 9 at 13:59
Quick follow-up. If starting a blank sheet with a header row, how to I skip the header row? This wipes out my header row. If I dolr = .Cells(.Rows.Count, varDay).End(xlUp).Row+1
it adds a new row every time I hit submit. Thanks again
– mattrweaver
Nov 9 at 14:20
nvm. figured out the offset
– mattrweaver
Nov 9 at 15:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Will write out a little bit more as comments aren't great for code.
In general, you should appropriately qualify references, so in this case with using your userform, you will need to specify the sheet/etc.
Dim lr as Long, varDay as Long
varDay = 1 'assumes using ColumnA, but you could make this a Find() function if necessary
With Sheets("Data")
lr = .Cells( .Rows.Count, varDay).End(xlUp).Row
If DateTextBox.Value <> .Cells(lr,varDay).Value Then 'Not sure if you want just Date (todays' date, no time) or the value in your input for the comparison (gave arbitrary name for textbox example)
'Do your thing
End if
End With
This would be in your command button for entering data, to determine where it would go. If you need to pull data from the sheet on initialize, you would then set textbox.value = .cell references... note that these two situations are not within the same module.
Thanks for your response and the explanation. That did it.
– mattrweaver
Nov 9 at 13:59
Quick follow-up. If starting a blank sheet with a header row, how to I skip the header row? This wipes out my header row. If I dolr = .Cells(.Rows.Count, varDay).End(xlUp).Row+1
it adds a new row every time I hit submit. Thanks again
– mattrweaver
Nov 9 at 14:20
nvm. figured out the offset
– mattrweaver
Nov 9 at 15:53
add a comment |
up vote
1
down vote
accepted
Will write out a little bit more as comments aren't great for code.
In general, you should appropriately qualify references, so in this case with using your userform, you will need to specify the sheet/etc.
Dim lr as Long, varDay as Long
varDay = 1 'assumes using ColumnA, but you could make this a Find() function if necessary
With Sheets("Data")
lr = .Cells( .Rows.Count, varDay).End(xlUp).Row
If DateTextBox.Value <> .Cells(lr,varDay).Value Then 'Not sure if you want just Date (todays' date, no time) or the value in your input for the comparison (gave arbitrary name for textbox example)
'Do your thing
End if
End With
This would be in your command button for entering data, to determine where it would go. If you need to pull data from the sheet on initialize, you would then set textbox.value = .cell references... note that these two situations are not within the same module.
Thanks for your response and the explanation. That did it.
– mattrweaver
Nov 9 at 13:59
Quick follow-up. If starting a blank sheet with a header row, how to I skip the header row? This wipes out my header row. If I dolr = .Cells(.Rows.Count, varDay).End(xlUp).Row+1
it adds a new row every time I hit submit. Thanks again
– mattrweaver
Nov 9 at 14:20
nvm. figured out the offset
– mattrweaver
Nov 9 at 15:53
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Will write out a little bit more as comments aren't great for code.
In general, you should appropriately qualify references, so in this case with using your userform, you will need to specify the sheet/etc.
Dim lr as Long, varDay as Long
varDay = 1 'assumes using ColumnA, but you could make this a Find() function if necessary
With Sheets("Data")
lr = .Cells( .Rows.Count, varDay).End(xlUp).Row
If DateTextBox.Value <> .Cells(lr,varDay).Value Then 'Not sure if you want just Date (todays' date, no time) or the value in your input for the comparison (gave arbitrary name for textbox example)
'Do your thing
End if
End With
This would be in your command button for entering data, to determine where it would go. If you need to pull data from the sheet on initialize, you would then set textbox.value = .cell references... note that these two situations are not within the same module.
Will write out a little bit more as comments aren't great for code.
In general, you should appropriately qualify references, so in this case with using your userform, you will need to specify the sheet/etc.
Dim lr as Long, varDay as Long
varDay = 1 'assumes using ColumnA, but you could make this a Find() function if necessary
With Sheets("Data")
lr = .Cells( .Rows.Count, varDay).End(xlUp).Row
If DateTextBox.Value <> .Cells(lr,varDay).Value Then 'Not sure if you want just Date (todays' date, no time) or the value in your input for the comparison (gave arbitrary name for textbox example)
'Do your thing
End if
End With
This would be in your command button for entering data, to determine where it would go. If you need to pull data from the sheet on initialize, you would then set textbox.value = .cell references... note that these two situations are not within the same module.
answered Nov 9 at 13:16
Cyril
2,2231821
2,2231821
Thanks for your response and the explanation. That did it.
– mattrweaver
Nov 9 at 13:59
Quick follow-up. If starting a blank sheet with a header row, how to I skip the header row? This wipes out my header row. If I dolr = .Cells(.Rows.Count, varDay).End(xlUp).Row+1
it adds a new row every time I hit submit. Thanks again
– mattrweaver
Nov 9 at 14:20
nvm. figured out the offset
– mattrweaver
Nov 9 at 15:53
add a comment |
Thanks for your response and the explanation. That did it.
– mattrweaver
Nov 9 at 13:59
Quick follow-up. If starting a blank sheet with a header row, how to I skip the header row? This wipes out my header row. If I dolr = .Cells(.Rows.Count, varDay).End(xlUp).Row+1
it adds a new row every time I hit submit. Thanks again
– mattrweaver
Nov 9 at 14:20
nvm. figured out the offset
– mattrweaver
Nov 9 at 15:53
Thanks for your response and the explanation. That did it.
– mattrweaver
Nov 9 at 13:59
Thanks for your response and the explanation. That did it.
– mattrweaver
Nov 9 at 13:59
Quick follow-up. If starting a blank sheet with a header row, how to I skip the header row? This wipes out my header row. If I do
lr = .Cells(.Rows.Count, varDay).End(xlUp).Row+1
it adds a new row every time I hit submit. Thanks again– mattrweaver
Nov 9 at 14:20
Quick follow-up. If starting a blank sheet with a header row, how to I skip the header row? This wipes out my header row. If I do
lr = .Cells(.Rows.Count, varDay).End(xlUp).Row+1
it adds a new row every time I hit submit. Thanks again– mattrweaver
Nov 9 at 14:20
nvm. figured out the offset
– mattrweaver
Nov 9 at 15:53
nvm. figured out the offset
– mattrweaver
Nov 9 at 15:53
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225948%2fexcel-userform-if-date-today-update-row-else-create-new-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
if you know which column you want to be in, you can use cells(rows.count,varDay).end(xlup).row to find the last row in that respective column. Where varDay is the variable column for finding the last day used. you can then check that cell to see if same as today, then determine if values go into last row or last row + 1
– Cyril
Nov 9 at 13:10