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
Related
Very specific problem not related directly to code but more to Excel behavior.
When launching a non modal UserForm (vbmodeless) from a minimized WorkBook (not visible on screen), while another WorkBook is on screen, Excel will "link" this UserForm and its focus to the visible one. This means that minimizing or maximizing the second WorkBook will hide and show the UserForm, but doing so with the first WorkBook won't, even if it has been launched by the first WorkBook.
Worse, closing the second WorkBook will close the UserForm.
An example of the issue :
Sub TestSo()
Application.WindowState = xlMinimized
UFTest.Show vbModeless
End Sub
Just create an empty UserForm named UFTest, open 2 Workbooks and display them split mode on screen and then call this Sub. You'll see that the UserForm is now linked to the wrong WorkBook.
Is there any way to prevent this from happening?
Does it do the trick?
Sub TestSo()
UFTest.Show vbModeless
ThisWorkbook.Windows(1).Visible = False
End Sub
Then in Userform restore the visibility
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
I have a userform on an Excel file called "userform":
Private Sub add1_Change()
End Sub
Private Sub add2_Change()
End Sub
Private Sub Calc_Click()
Result.Value = Val(add1.Value) + Val(add2.Value)
End Sub
This userform takes the value from the user and adds them together and shows the result in a textbox.
I want to create a new macro in another workbook named "input". The macro in this workbook should open the userform workbook, enter values in the textbox add1 and add2, then run the userform calculate button.
What I've tried thus far:
Set add1.value to extract a value from say, cell A1, and similarly for add2.value.
Then I created a macro on the input workbook to change the values in cells A1 and A2.
The problem from here is I don't know how to open the userform and click calculate.
Ideally, I would like a macro which opens the userform, enters the data and hits calculate then closes the userform - Rather than editing the userform itself.
You could add the 2 values in the UserForm in this way(its slightly different then you try to do it now):
You use your current code to open the UserForm:
Sub userform()
Workbooks.Open (ThisWorkbook.Path & "\userform.xlsm")
Application.Run "userform.xlsm!Calc"
End Sub
As shown above you don't assign any values this will happen in your userform.xlsm Workbook
Below is the code you put into the sub Initialize of your UserForm:
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("input.xlsx")
Set ws = wb.Worksheets("Input")
Dim i as Integer
Dim k as Integer
UserForm.add1.Value = ws.Range("A2").Value
UserForm.add2.Value = ws.Range("B2").value
UserForm.calc.Value = val(UserForm.add1.Value) + val(UserForm.add2.Value)
End Sub
As shown above calc is changed to a Textbox, therefor you don't need to click a button its directly done when the UserForm is loaded.
You could also use a Label instead of a Textbox.
the code would then change to:
UserForm.calc.Caption = Str( val(UserForm.add1.Value) + val(UserForm.add2.Value) )
#DirkReichel could you elaborate a bit more on this? I've added what you said, but say I wanted to change the value on the add1 textbox how would I call it? Right now, I have this: 'Sub userform() Dim a As Integer Dim b As Integer a = Cells(1, 2) b = Cells(2, 2) Workbooks.Open (ThisWorkbook.Path & "\userform.xlsm") Application.Run "userform.xlsm!Calc" End Sub' the calc macro just opens up the userform, I don't know how to actually "input" data or hit calculate
The answer:
I created 2 WB and just this simple code worked for me ... however: you may need to change the settings of the trust center.
Book1 Module: (the WB with Userform1 holding TextBox1 and CommandButton1)
Option Explicit
Public Function getUF()
Set getUF = UserForm1
End Function
Book2 Module:
Option Explicit
Public ExtUF As Variant
Sub the_UF()
Workbooks.Open "Book1.xlsm"
Set ExtUF = Application.Run("Book1.xlsm!getUF") 'get the Form
Debug.Print ExtUF.TextBox1.Value 'check old value
ExtUF.TextBox1.Value = "dada" 'change it
Debug.Print ExtUF.TextBox1.Value 'check for new value
ExtUF.CommandButton1.Value = True 'hit the button
ExtUF.Show 'show the form
Stop 'to check the userform
ExtUF.Hide 'hide it again
End Sub
Now just run the_UF and check for functionality. If everything does work, adopt it to your code the way you need it.
If you have any questions, just ask ;)
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.
I keep running into this issue. I have a command button that updates the value of some cell, but when you click the button, the value does not update. If you then click on some other cell in the worksheet, THEN the value finally updates. My code looks like this:
Private Sub eight_Click()
Dim oldnum As String
If numset = False Then
oldnum = Range("F4").Value
Range("F4").Value = oldnum + "8"
Else
num1 = CLng(Range("F4").Value)
Range("F4").Value = "8"
numset = False
End If
End Sub
This happens to me 100% of the time on Excel for Mac, and it happens occasionally on Excel 2010 for Windows.
Here is the file: http://wikisend.com/download/906870/Tan.xlsm
Let us say: You have a button (btnCalendarInvoke) that invokes a userform (frmCalendar)
and this userform has the datepicker (dtPicker).
Now if I understand you want to click the button on excel sheet, that invokes the form, you'd select the date from date picker there, and you want the selected date to reflect on the Excel cell spontaneously.
Here is what you can do:
Step 1 - Open code window for Userform from the project explorer and create a Public Variable under General scope.
Public rangeName as String
Step 2 - Now create a Public Sub within UserForm Code that sets this variable as shown below.
Public Sub SetDestinationRangeName(rngName as String)
rangeName = rngName
End Sub
Step 3 - Create code to handle date picker change event as shown below.
Private Sub dtSelect_Change()
Worksheets("Scorecard").Range(rangeName).value = Format(Me.dtSelect, "Short Date").Value
Unload Me
End Sub
Step 4 - Go the excel sheet and write code to handle button click event. as shown below.
Private Sub btnCalendar_Click()
Dim calForm As frmCalendar
Set calForm = New frmCalendar
calForm.SetRangeName ("Your Excel Cell reference Range")
calForm.Show
End Sub
Step 5 - Now test it. Click the button Calendar form appears, select the date from date picker, It closes and updates the excel sheet spontaneously. you not have to make an extra click on the cells as you reported following this approach.
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.