I’m trying to write a quick little macro that asks the user for an input, and then copies this to a specific cell (B14 in Sheet1). Here’s what I’ve got so far:
Option Explicit
Sub updatesheet()
Dim vReply As String
vReply = InputBox("Enter period (format: Q4 2010) to update, or hit enter to escape")
If vReply = vbNullString Then Exit Sub
Sheets("Sheet1").Activate
ActiveSheet.Range("B14").Value = vReply
End Sub
I was also wondering if there is some way I can include a check to make sure the user input is in the correct format, and if not, flags up an error and asks the user to re-enter?
Help greatly appreciated 🙂
something like this, you were very close (rather than
Inputboxyou just needed to usevReplywhen writing to Sheet1 B14)Updated Overhauled to de-hmmm:
Application.InputBoxrather than ‘InputBox’ as this provides the coder with more optionality. But nice to have in this instance rather than critcal"^Q[1-4]\s20[11-13]{2}$". The “q” test is case insensitiveInt((Month(Now()) - 1) / 3) + 1 & " " & Year(Now())returns Q4 2011 . You can remove this prompt if desiredDoloop is used to test invalid strings, if an invalid string is supplied than thestrTitlevariable of “Please retry”” is used to let the user know that prior attempts were invalid (the msg doesn’t show the first time through as the user is yet to make a mistake)Pressing Cancel triggers a separate exit message to let the user know the code has terminated early