Cell value upon checked box copied into Userform Textbox - excel

I have several sheets of which house numerous checkboxes,of which when ticked, the value of cells next to the checkboxes will be copied into a textbox.Currently using ActiveX Textbox, below codes work just fine
Sub checkBoxHandler()
ActiveSheet.TextBox1.Text = ActiveSheet.TextBox1.Text & " " & ActiveSheet.Shapes(Application.Caller).TopLeftCell.Offset(1, 1).Value
End Sub
But now i would like to use Userform Textbox since it can float on the workbook. Modification to the codes such as below does not do the trick (the value of cells next to the ticked checkboxes are not copied into the Userform Textbox).
Sub ShowTextBox()
UserForm1.TextBox1.Text = UserForm1.TextBox1.Text & vbLf & ActiveSheet.Shapes(Application.Caller).TopLeftCell.Offset(1, 1).Value
UserForm1.Show vbModeless
End Sub
Does anybody know how to make it works?

I've done a similar project. I think userform does not support that kind of method. so i did this:
i put activex textbox on each of the sheets and named it accordingly (Textbox1,Textbox2..), and set the Visibility property to False
i assign macro (below) to a shape to pass the value of activex textbox to the Userform on every sheet :
Sub hocmsummary()
UserForm1.TextBox1.Text = UserForm1.TextBox1.Text & ActiveSheet.TextBox2.Text
End Sub
Remember to change the Textbox name accordingly.

Related

Can VBA interact with check boxes that were placed without VBA

I am trying to write a macro that will scrape data from a huge set of already existing excel files. These files have the same format just with different info. I am able to pull any data written in a cell, even if it's in the form of a drop down. However, since checkboxes are not tied to a cell range, I'm not sure how to interact with them in VBA.
I've tried using
? Activesheet.checkbox1.value
? Activesheet.checkbox(1).value
but it's not giving me anything.
Is there a way to reference these? I'm guessing they were placed using "developer tab -> insert objects" so there is no userform area in the project tree in VBA
Thanks,
Try this code to find checkboxes on the worksheet:
Sub InspectCheckBoxes()
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
Select Case sh.Type ' see MsoShapeType enumeration
Case msoFormControl
If sh.FormControlType = xlCheckBox Then ' see XlFormControl enumeration
Debug.Print "Found CheckBox as FormControl named '" & sh.Name & "' at left "; sh.Left & "; top " & sh.Top
End If
Case msoOLEControlObject
If InStr(1, sh.OLEFormat.progID, "CheckBox", vbTextCompare) > 0 Then
Debug.Print "Found CheckBox as OLEControlObject named '" & sh.Name & "' at left "; sh.Left & "; top " & sh.Top
End If
End Select
Next
End Sub
Prints:
Found CheckBox as FormControl named 'Check Box 2' at left 207; top 78
Found CheckBox as OLEControlObject named 'CheckBox1' at left 244,5; top 142,5
You didn't specify which type of checkboxes...
Dim cbx As Excel.CheckBox
Dim msfCbx As msforms.CheckBox
Dim ole As OLEObject
Debug.Print "Forms type checkboxes"
For Each cbx In ActiveSheet.CheckBoxes
Debug.Print cbx.Value, cbx.Name, cbx.Caption, cbx.TopLeftCell.Address
Next
Debug.Print "ActiveX checkboxes"
For Each ole In ActiveSheet.OLEObjects
If ole.progID = "Forms.CheckBox.1" Then
Set msfCbx = ole.Object
Debug.Print msfCbx.Value, ole.Name, msfCbx.Caption, ole.TopLeftCell.Address
End If
Next
Both previous answers here assumed your checkboxes were on worksheets. Both checkbox types include a 'linked-cell' property which changes a cell's value.
Checkboxes on a userform are can't be directly linked to cells, as you say. Code in a checkbox event can perform some action, or other code might read the checkbox state later when required,
Unless I'm missing something the only way you might retrieve any useful data, just by reading checkbox values on a loaded userform, is if you can deduce some linked cell range simply from the checkbox's name. It would also seem likely code in the userform's initialize event, or perhaps a 'get data' type button, reads cells and updates checkboxes at runtime.
If that's the case try this - add a button to your form
Private Sub CommandButton1_Click()
Dim ctl As MSForms.Control
For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
Debug.Print ctl.Value, ctl.Name
End If
Next
End Sub

Using userform vbmodeless in a loop

I refer to this:
How to have VBA code wait until vbModeless userform is closed
I tried to apply the code in a class module for my problem.
Problem description:
I have a userform to insert data, mostly fill text in textboxes.
This userform is opened if incorrect data are found in an Excel table.
I run through the rows of the sheet with a For loop.
When it finds incorrect data it opens the userform to input data manually to textboxes.
With Save and Exit (unload me). I save the data to the sheet.
The reason I use the userform vbmodeless is that I want to copy data from another Excel sheet.
This is not possible if I open the userform vbmodal.
Here is an example of the code:
Option Explicit
Public done As Boolean 'Userform UF_Input filled
Sub Userform_vbmodeless_in_Loop()
'Testprogram with userform vbmodeless in a loop
Dim i As Integer
For i = 1 To 4
UF_Input.TextBox1.Text = "That is the " & i & ". Test"
done = False 'per default
'Loop as long as the input userform is not left
Do While done = False
UF_Event.Show vbModeless
DoEvents
UF_Event.Label1.Caption = "Loop: " & i
Loop
'Unload Event userform
Unload UF_Event
Next i
End Sub

Adding a tooltip to a ComboBox in Excel with VBA

I've added two Dropdown (aka ComboBox) to a Sheet
Using this piece of code I can access the Dropdown but how can I add a tooltip on the Dropdown?
The best solution would be to show a different text for every item but if there is only an unique tooltip for the whole dropdown I can change it after selecting every item.
Sub DropDown1_Change()
Dim s As Object
Set s = ActiveSheet.Shapes(Application.Caller)
s.ToolTip = "Example"
Debug.Print s.ControlFormat.Value
End Sub
This is a forms combobox, it would not have a tooltip capability, but you can make it look like it has a tool tip.
Place a hyperlink with a screen tip underneath the combobox, when you mouse over the combobox the screen tip will pop up.
You can place the hyperlink on many cells if you intend on stretching the combobox over many cells.
Like this
Here is a 20 second clip
http://www.screencast.com/t/ZbkEOyXntItk
You can get the range of the combobox with application.caller.
Assign each combobox to this macro, then you would only need one macro.
Sub DoIt()
Dim r As Range
r = ActiveSheet.Shapes(Application.Caller).TopLeftCell
ActiveSheet.Hyperlinks.Add Anchor:=r, Address:=r, ScreenTip:="5435435345", TextToDisplay:="ddddddddddddddddddd"
End Sub
Following is my code:
Private Sub ComboBox1_Click()
' Adding new items
ComboBox1.AddItem ("S")
ComboBox1.AddItem ("M")
If ComboBox1.Text = "S" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Strong" ' Add your text here
End With
End If
If ComboBox1.Text = "M" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Moderate" ' Add your text here
End With
End If
End Sub

Userform showing when not wanted, not called for

im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.

how to load the combobox and then how to use the selected value from it in vba?

a question is : how to load a or give value to a combo box list and then call it in my worksheet and get the selected value from it ?i have a module that i want to call my userform1 which include the combobox in it . but when i debug the program it is just a show of the combo box . i think it doesn't do anything ... thanks for your time ..this is the code for user form:
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "weibull"
.AddItem "log-normal"
.AddItem "gambel"
.Style = fmStyleDropDownList
End With
End Sub
and this is how i ask in my sub to show the combobox:
UserForm1.Show
If ComboBox1.ListIndex = -1 Then
MsgBox "There is no item currently selected.", _
vbInformation, _
"Combo Box Demo"
Exit Sub
End If
MsgBox "You have selected " & ComboBox1.List(ComboBox1.ListIndex) & "." & vbNewLine _
& "It has " & ComboBox1.ItemData(ComboBox1.ListIndex) ", _
vbInformation, _
"Combo Box Demo"
the second part is what i found in net , but it made the program at least to show the combo box !
I thought Siddharth's answer was pretty clear particularly as it was posted from a mobile! However, I had an email from the OP saying he did not understand the answer. I provided the following background which was apparently sufficient to allow him to understand Siddharth's answer and solve his problem. I post it here for the benefit of any other visitor who needs more background on forms than Siddharth provides.
If you select VBA Help and type “userform show” you will get a description of the Show command.
If your user form is named “UserForm1”, you can have statements:
UserForm1.Show
UserForm1.Show vbModal
UserForm1.Show vbModeless
Statements 1 and 2 are equivalent.
The choice of VbModal or vbModeless completely changes the way the user form is controlled.
If a form is shown modeless, the user can see it but cannot access it. If I have a macro that takes a long time, I will use a modeless form to show progress. If I am working down the rows of a large worksheet I might have a form containing:
I am working on row nnnn of mmmm
Each of the boxes is a label. I set the value of the label containing “mmmm” to the number of rows when I start the macro. I set the value of the label containing “nnnn” to the row number at the start of each repeat of my loop. The user sees:
I am working on row 1 of 5123
then I am working on row 2 of 5123
then I am working on row 3 of 5123
and so on.
If it takes the macro five minutes to process every row, this tells the user that something is happening. Without the form, the user might think the macro had failed. With the form, the user knows the macro is busy and they have time to get a fresh cup of coffee.
On the other hand, if the form is shown modal, the macro stops until the user does something that closes or unloads the form with a statement such as:
Unload Me
The positioning of this statement depends on your form. I normally have a Submit button that saves information before ending with this statement.
Once the Unload Me statement is executed, the macro restarts at the statement after the Show statement. When the macro restarts, the form has gone. This is why the form must save anything the macro needs in global variables.
You are trying to access a control when the userform is already closed. And I say closed becuase you are not using vbmodeless to show the form. So the only way the next line after that can run is when the form is closed. Here is what I recommend.
Declare public variables in a module which will hold the relevant values when the useform closes and then use that later. For example
Paste this code in the userform
Option Explicit
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "weibull"
.AddItem "log-normal"
.AddItem "gambel"
.Style = fmStyleDropDownList
End With
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If ComboBox1.ListIndex <> -1 Then
SelectItem = ComboBox1.List(ComboBox1.ListIndex)
pos = ComboBox1.ListIndex + 1
End If
End Sub
And paste this in a module
Option Explicit
Public SelectItem As String, pos As Long
Sub Sample()
'
'~~> Rest of your code
'
SelectItem = "": pos = 0
UserForm1.Show
If pos = 0 Then
MsgBox "There is no item currently selected.", _
vbInformation, "Combo Box Demo"
Exit Sub
End If
MsgBox "You have selected " & SelectItem & "." & vbNewLine & _
"It is at position " & pos, vbInformation, "Combo Box Demo"
'
'~~> Rest of your code
'
End Sub
Also
There is no .Itemdata property of the Combobox. It is available in VB6 but not in VBA. With .Itemdata property of the Combobox, I guess you were trying to get the position?

Resources