Reputation: 81
I've created a macro in VBA that finds the word "Viewpoint" and produces a userform that looks in the surrounding cells (to the "North", "East", "South" or "West") and "converts" a 4-letter code into a 4-digit number, as long as the cell is "convertible".
It works using an array in the surrounding cells. As an example, the following would allow the user to convert the "North" and "East" cells, but would not convert "West" (being "nonconvertible") and would produce nothing for "South":
In this example, upon loading the macro, the user is presented with the following userform. Clicking on the "Convert North" or "Convert East" buttons produces a message box with the converted code, while clicking on "Convert West" produces a message box with the word "Nonconvertible".
However, rather than producing a message box, I'd like to produce a second userform, based on the user's choice from the first userform. e.g. If a user selects "Convert North", they'd receive a further userform with the converted code (as a label) and a couple of further options (as buttons, ideally just based on further indexes in the array). Of course this would mean that there would need to be some way of referencing the first userform in the second userform. I've had a look at other answers on here but can't seem to find one that works here. What would be a good way of doing this?
Here's the module code so far. Note that "optNorth", "optSouth", etc. refer to the userform buttons, while "lblNorth" etc. refer to the userform labels. Finally, feel free to let me know if you notice any way this code could be made more efficient:
Private Sub Surroundings(OFFSET_CELL As Range, LBL As Object, OPT As Object, DIRECTION As String)
Dim VIEWPOINT As Range
Dim LAST_ROW As Integer
Dim ARR() As String
' Set ARR to use the "Split" function in the destination cell, making the "\" a delimiter
ARR = Split(OFFSET_CELL, "\")
' Set the range in which to search for "Viewpoint"
Set VIEWPOINT = Sheet1.Range("A1:Z26").Find(What:="Viewpoint", LookAt:=xlWhole, MatchCase:=True)
' If there is a "\" in the adjacent cell...
If InStr(1, OFFSET_CELL, "\") > 0 Then
' show a label with the DIRECTION and array index 0 (the 4-letter code)
LBL.Caption = DIRECTION & " is " & (ARR(0))
' show a button label with the word "Convert" and the DIRECTION:
OPT.Caption = "Convert " & DIRECTION
' In any other situation...
Else
LBL.Caption = DIRECTION & " is nothing."
OPT.Caption = "Nothing"
OPT.Enabled = False
End If
End Sub
Public Sub Surroundings_Userform()
Dim VIEWPOINT As Range
' Set the range in which to search for "Viewpoint"
Set VIEWPOINT = Sheet1.Range("A1:Z26").Find(What:="Viewpoint", LookAt:=xlWhole, MatchCase:=True)
' Load up the userform
Load SurroundingsUserForm
' Call the variable code in the Surroundings sub
Call Surroundings(VIEWPOINT.Offset(-1, 0), SurroundingsUserForm.lblNorth, SurroundingsUserForm.optNorth, "North")
Call Surroundings(VIEWPOINT.Offset(1, 0), SurroundingsUserForm.lblSouth, SurroundingsUserForm.optSouth, "South")
Call Surroundings(VIEWPOINT.Offset(0, 1), SurroundingsUserForm.lblEast, SurroundingsUserForm.optEast, "East")
Call Surroundings(VIEWPOINT.Offset(0, -1), SurroundingsUserForm.lblWest, SurroundingsUserForm.optWest, "West")
SurroundingsUserForm.Show
Set VIEWPOINT = Nothing
End Sub
And here's the Form code:
Private Sub OptionButton(OPT_BUTTON As String, OFFSET_ROW As Integer, OFFSET_COLUMN As Integer)
Dim VIEWPOINT As Range
Dim ARR() As String
Dim OFFSET_CELL As String
' Set the range in which to search for "Viewpoint")
Set VIEWPOINT = Sheet1.Range("A1:Z26").Find(What:="Viewpoint", LookAt:=xlWhole, MatchCase:=True)
Select Case True
Case OPT_BUTTON
' Define OFFSET_CELL as per the option button
OFFSET_CELL = VIEWPOINT.Offset(OFFSET_ROW, OFFSET_COLUMN)
' The array uses "\" as a delimiter
ARR = Split(OFFSET_CELL, "\")
' If array index 1 is "Convertible", show a message box with array index 2
If ARR(1) = "Convertible" Then
MsgBox (ARR(2))
' If array index 1 is "Nonconvertible", show a message box with "Nonconvertible"
ElseIf ARR(1) = "Nonconvertible" Then
MsgBox ("Nonconvertible")
' In any other situation, unload the userform
Else
Unload Me
End If
End Select
Unload Me
End Sub
Private Sub optNorth_Click()
Call OptionButton(optNorth, -1, 0)
End Sub
Private Sub optSouth_Click()
Call OptionButton(optSouth, 1, 0)
End Sub
Private Sub optEast_Click()
Call OptionButton(optEast, 0, 1)
End Sub
Private Sub optWest_Click()
Call OptionButton(optWest, 0, -1)
End Sub
Private Sub optClose_Click()
Unload Me
End Sub
Upvotes: 0
Views: 754
Reputation: 9878
If I've understood your question correctly please take a look at the example below.
I've set up the first userform as below
and placed the following code behind it
Option Explicit
Private Sub CommandButton1_Click()
Call Click("North")
End Sub
Private Sub CommandButton2_Click()
Call Click("South")
End Sub
Private Sub Click(str As String)
Dim ufrm2 As UserForm2
Set ufrm2 = New UserForm2
ufrm2.CoOrd = str
ufrm2.Show
End Sub
In the second form I've placed a textbox
with the following code
Option Explicit
Private vCoOrd As String
Property Let CoOrd(v As String)
vCoOrd = v
End Property
Property Get CoOrd() As String
CoOrd = vCoOrd
End Property
Private Sub UserForm_Activate()
Label1.Caption = CoOrd
End Sub
Giving the following results:
This works by setting the CoOrd
property of UserForm2
from UserForm1
before Activating
the second userform. This way I'm able to pass values from UserForm1
to UserForm2
before the userform is displayed to the user
Upvotes: 1