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.



enter image description here



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?










share|improve this question




















  • 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

















up vote
0
down vote

favorite












I am working on a user form that will record counts at certain times of the day.



enter image description here



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?










share|improve this question




















  • 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















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.



enter image description here



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?










share|improve this question















I am working on a user form that will record counts at certain times of the day.



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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














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.






share|improve this answer





















  • 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










  • nvm. figured out the offset
    – mattrweaver
    Nov 9 at 15:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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










  • nvm. figured out the offset
    – mattrweaver
    Nov 9 at 15:53















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.






share|improve this answer





















  • 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










  • nvm. figured out the offset
    – mattrweaver
    Nov 9 at 15:53













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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










  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff