This has got to be ridiculously easy, but I just cannot figure it out searching the web.
I have an Excel macro that performs various data entry/manipulation tasks. There is a point in the macro where I want the user to have the option of using data in column A or column B for calculations. All I want is to call a userform with two command buttons which pass their "true" or "false" value to the main macro and then perform "if" statements based on this information.
The trouble is I cannot get the userform to "tell" the macro anything. I'm sure this is a major noob question and I'm missing the method, but I cannot get this to work. Thank you!
Edit, I've attached the userform code below by request:
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub OptionButton1_Click()
End Sub
Private Sub OptionButton2_Click()
End Sub
Private Sub UserForm_Initialize()
End Sub
you could avoid userform for that and use a simple message box:
If MsgBox("use data in column A or B [Yes=A, NO=B]", vbYesNo) = vbYes Then
' code for using data in column A
Else
' code for using data in column B
End If
Related
So I'm attempting to filter a large section of data in a spreadsheet with checkboxes. So far around 80 individual checkboxes. I'm wondering if there is anyway to refer to the checkbox (or any other control) name within the sub, as a specific reference, i.e. thiscontrol.name. At present the first checkbox reads:
Private Sub_F1_Click()
StringVariableForLaterUse ="F1"
If F1.Value = True Then
'Display Data Relevant to F1
End If
End
I'm wondering if I can use
StringVariableForLaterUse = ThisControl.Name
and
If ThisControl.Value = True Then
I'd then be able to replicate this a further 78 times. Currently pure laziness factor, however I want my Subs to be self sufficient as possible. Any thoughts folks?
You can never use a string like an object... But, for an ActiveXcheck box, use the next workaround code.
Public chk1 As Shape
Private Sub CheckBox1_Click()
Set chk1 = ActiveSheet.Shapes(CheckBox1.Name)
MsgBox chk1.OLEFormat.Object.Object.value
End Sub
You can use now chk1 in another Sub inside the sheet module.
You can also refer to it from a module Sub, referencing the sheet, too:
Sub testSheetChk()
Debug.Print Worksheets("Sheet Name").chk1.OLEFormat.Object.Object.value
End Sub
But, this will work only after you firstly run (once) the click event, in order to alocate a value to the object variable.
I have a table that lists Departments and Passwords in SQL Server. I am creating a Userform UI so that users are able to view list, amend an entry or delete an entry.
I have made it look like this:
I managed to set up the ADODB connection when a user opens the form, it would automatically load the list using:
Private Sub Userform_Activate()
My rational is that I want a user to select from the list box, it would display the Department and Password in textbox1 and textbox2, respectively.
The user then can either Update the record or Delete the record. This would be a very straight-forward SQL operation.
What I am hitting a road block with at the moment is actually getting the data displayed in Textbox1 and Textbox2.
Code
Private Sub Select_Click()
If Me.ListBox1.Selected(i) Then
Me.TextBox1 = Me.ListBox1.List(i, 0)
Me.TextBox2 = Me.ListBox1.List(i, 1)
End If
End Sub
Any advise would be appreciated.
Furthermore, is my logic sound? Or is there a better way of doing things?
I know this is 2-in-1 question, but thanks!
Just tested and this works.
Private Sub ListBox1_Click()
TextBox1.Value = ListBox1.List(ListBox1.ListIndex, 0)
TextBox2.Value = ListBox1.List(ListBox1.ListIndex, 1)
End Sub
I have a VBA excel userform that is misbehaving. Basically I want my code to pause until a command button on a userform is pressed. Any ideas?
In a module:
Public okayclicked as boolean
Sub MyThing
UserForm1.Show
Do Until okayclicked
DoEvents
Loop
UserForm1.Hide
End Sub
In my userform:
Sub CommandButton1_Click()
okayclicked = True
End Sub
Not sure what else to try to make this work. Any help is appreciated! Thanks!
'module
Sub MyThing1
UserForm1.Show
End Sub
Public Sub MyThing2
'....rest of code
End Sub
'userform
Sub CommandButton1_Click()
MyThing2
Me.Hide
End Sub
Either use a MsgBox to ask for the user input, or set your form to be Modal (MSDN link).
A Modal form will allow you to continue to work in the form, and any code underlying the form will continue to run, but the main code that calls the form will be 'blocked'.
Should the underlying Excel sheets be modified during the period of pause, you can add code when the button press is detected (i.e. when the Form is closed or then the MsgBox OK button is pressed) to update the relevant parts of the Form.
But we have digressed from the original simple question and many other factors (such as how the underlying sheets will be modified when the form is open) come into play.
Consider if a change in the underlying sheet has to change the form immediately or if it can occur when the user resumes the form/code.
Consider if the functions you want can be done natively in the spreadsheet and minimise the use/functionality of the form.
Consider other event driven options where the code is broken into natural sections, functions, subroutines or modules and each section only runs when triggered by the right event (sheet change, user input, subroutine finished, form exit etc.)
I have 10 text boxes on an Excel VBA Userform, and when I switch away from the window (say, to do other work in a different program) and come back, I want the focus to be on the textbox I was last using.
I think this should be an obvious question to an experienced person, but I can't find a well-asked question about this topic anywhere. If anyone can direct me to a proper answer, give me a searchable topic, or a good piece of code, that would be golden. I'm a sponge, I'll take anything.
Thank you!
-Chris
Cursor focus is not an event in VBA, thus it is a bit tough to do it. BUT you can use the _Change event and remember the last changed Textbox, which is quite close to you want.
You need a public variable in a module, for the name of the last changed TextBox. Thus, the next time the Form is activated, you may use a simple select case and use .SetFocus to the corresponding name of the form. The code below works with two TextBoxes.
In the form:
Option Explicit
Private Sub TextBox1_Change()
strLastTb = "TextBox1"
End Sub
Private Sub TextBox2_Change()
strLastTb = "TextBox2"
End Sub
Private Sub UserForm_Activate()
Select Case strLastTb
Case "TextBox1"
TextBox1.SetFocus
Case "TextBox2"
TextBox2.SetFocus
End Select
End Sub
In a module:
Option Explicit
Public strLastTb As String
In general, you may create a function that concatenates TextBox&Digit, so you will not be required to write 100s lines if you have 50 TextBoxes. And it would be looking better.
I need to run a macro after some data in a table/pivot table is refreshed. My table is reading data from a database, so I want the macro to run after the refresh. How would I do this using VBA. I've tried the following.
Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable)
Application.Run "overrideManagers" 'Is this right?
End Sub
Sub overrideManagers() 'Macro to be run upon refresh of data
MsgBox "Hello"
End Sub
I don't get what was expected. I just get the refresh, but without the MsgBox. Even if I move the MsgBox to this.
Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable)
MsgBox "Hello"
End Sub
I get the same results. This leads me to think that my original method for catching the update is wrong. Any ideas?
This may be a little lengthy answer. I'll try to explain clearly.
Below are the steps I followed
Created a new excel workbook with a simple table and data ( Sheet1)
Created a pivot table for that data ( Pivot table in another sheet - Sheet2)
3. Added the following code in Macro editor under Sheet2
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
MsgBox "update"
End Sub
4. Selected Sheet2, right click pivot table and refresh. Message box displayed
Hope this helps.