Reputation: 155
This follows on from a previous question here where I asked how to develop an input box
that required a data to be entered in a specific format by the user (eg: Q4 2010)
I now need to take the first 2 character of the user input (always to be Q1, Q2, Q3 or Q4) and change the text in another cell depending on the outcome. Specifically, if the user enters a string starting with Q1 or Q3, I need it to copy <insert text1>
into a cell in Sheet3
, and if they enter Q2 or Q4 it copies <insert text2>
in the same cell instead.
I have no idea how to consider only part of an input, so any help most welcome :)
Upvotes: 1
Views: 287
Reputation: 55682
You could try this to populate A1
of a sheet called Sheet3
with the two strings
[Updated as per msgbox asking for user input in specific format to be bullet proof using Regular Expressions]
Option Explicit
Sub Rattle_and_hmmmm2()
Dim strReply As String
Dim strTitle As String
Dim objRegex As Object
Dim objRegMC As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.ignorecase = True
.Pattern = "^Q([1-4])\s20[10-20]{2}$"
Do
If strReply <> vbNullString Then strTitle = "Please retry"
strReply = Application.InputBox("Enter period (format: Q4 2010) to update, or hit enter to escape", strTitle, "Q" & Int((Month(Now()) - 1) / 3) + 1 & " " & Year(Now()), , , , , 2)
If strReply = "False" Then
MsgBox "User hit cancel, exiting code", vbCritical
Exit Sub
End If
Loop Until .test(strReply)
Set objRegMC = .Execute(strReply)
End With
Select Case objRegMC(0).submatches(0)
Case 2, 4
Sheets("Sheet3").[a1] = "insert text2"
Case 1, 3
Sheets("Sheet3").[a1] = "insert text1"
End Select
Sheets("Sheet1").[b14].Value = UCase$(strReply)
End Sub
Upvotes: 3