MeisterConrad
New Member
- Joined
- Jan 17, 2017
- Messages
- 42
- Office Version
- 2007
I've got a UserForm in a financial Workbook that handles transfers from one internal account to another. The idea is to click one of the odd-numbered OptionButtons at the left of the Account List to denote the origin of the transfer, and then click on of the even-numbered OptionButtons at the right side to denote the destination account.
I literally used to have 40 IF/THEN statements that included code for the entire operation - it works, but it's SO much "duplicate" (slightly modified) code. I think I'm on a much more efficient track now, but I'm still having trouble making the code say what I want it to say. Getting this bit right would be a big hurdle for me in this project. I thought I hit the idea spot-on, so I tried just the important piece of the code to test it out. I get an error message saying that "OptionButton" is not defined. I thought vba already knew what an OptionButton is. Doesn't it? Or how do I tell the vba what an OptionButton is?
I'm learning, but I certainly wish I didn't have to learn - and just already knew instead. Thanks for the help. I'm so glad that Mr. Excel exists.
I literally used to have 40 IF/THEN statements that included code for the entire operation - it works, but it's SO much "duplicate" (slightly modified) code. I think I'm on a much more efficient track now, but I'm still having trouble making the code say what I want it to say. Getting this bit right would be a big hurdle for me in this project. I thought I hit the idea spot-on, so I tried just the important piece of the code to test it out. I get an error message saying that "OptionButton" is not defined. I thought vba already knew what an OptionButton is. Doesn't it? Or how do I tell the vba what an OptionButton is?
VBA Code:
Private Sub CommandButton1_Click()
'Dim the variables
Dim OBf As Integer
Dim Tf As Integer
Dim FROMAC As Range
Dim OBt As Integer
Dim Tt As Integer
Dim TOAC As Range
' Build the FROM account
For OBf = 1 To 39 Step 2 ' HERE'S WHERE I NEED HELP:
If OptionButton(OBf).Value = True Then 'I get an error message saying OptionButton isn't defined.
Set Tf = (OBf + 1) / 2
Set FROMAC = "LedgerTable" & Tf
FROMAC.Select
' Export the input from the form into the cells of the Ledger Table.
' Exit the IF/THEN statement, and Quit the FOR/NEXT loop.
' I ALSO NEED HELP WITH THIS:
' I'm confused as to how to phrase it in code, but, if OptionButton 1 is selected as the FROM acct., then LedgerTable1 should pop up on the screen.
' If that all happens, then we're done here and it's time to move on to the TO account stuff.
' We need to quit the IF/THEN and the FOR/NEXT.
' If none of the above happens, THAT's when we go to Next OBf = loop-cycle.
Else
Next OBf
End If
' This is the same as above, just modified for the different account.
' Build the TO account
For OBt = 2 To 40 Step 2
If OptionButton(OBt).Value = True Then
Set Tt = (OBt + 1) / 2
Set TOAC = "LedgerTable" & Tt
TOAC.Select
' Export the input from the form into the cells of the Ledger Table.
' Exit the IF/THEN statement, and Quit the FOR/NEXT loop.
' So, all the TO account business would happen with, say, OptionButton 12, and LedgerTable6 should be what's showing when the code is done.
Else
Next OBt
End If
End Sub
I'm learning, but I certainly wish I didn't have to learn - and just already knew instead. Thanks for the help. I'm so glad that Mr. Excel exists.