I am struggling to reference a userform name with a variable from cell.value located in range("A20").
Scenario: Double-clicking item in a listbox "completed missions" on userform "NewMission" opens up a "ReviewMission" userform. This "ReviewMission" userform needs to be generic so that I can access it from whichever page I'd like after clicking on the same named listbox, carrying over the listindex of the selected value in the listbox - saved as "referencerow"
currently, the userform name is saved into a cell value, hoping to be later called back as the form of an object.
code:
Private Sub CompletedMissions_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Worksheets("lists").Range("A20").Value = Me.Name 'saves the title "NewMission" to cell
ReviewMission.Show
End Sub
with the second form's initialize:
Private Sub UserForm_Initialize()
Dim ListsSheet As Worksheet
Dim LastRow As Long
Dim aCell As Range
ReferenceRow = UserForms(Range("A20").Value).CompletedMissions.ListIndex + 2
'CODE CONTINUES...
What am I missing here? is there a way to use a public function?
Related
I am creating a Userform where once the userform is initialized, there is a combobox which list a range of information from a specific worksheet that people can select.
Adding just basic information to a combobox I can do, using combobox.additem "1" for example, but to get it to read a range I am getting a sub or function not defined error.
Public Sub UserForm_Initialize()
Dim I As Integer
Dim rulesSheet As Worksheet
Set rulesSheet = Sheets("Rules")
I = 1
Do
comboboxadd
iteminbox = rulesSheet.Range("A" & I)
CostCentreCMBox.AddItem (iteminbox)
I = I + 1
Loop Until iteminbox = " "
CostCentreCMBox.ListIndex = 0
End Sub
I would like my combobox to add items in the range of worksheet "rules" column A, row (as many as that have information in), once userform as me initialized
I'm trying to change values of textbox according to values of combobox. As I'm newcomer in VBA and VBA UserForm I can't fix it myself
Private Sub tbxDivision_Change()
Dim ws as Worksheet
Set ws = Workbooks("...").Worksheets("Sheet1")
If cboResponsible.value = "ENGINEERS" Then
txtDivision.Value = ws.Cells("A7")
Elseif ...
End if
End sub
This is the code that I've tryed to run. I've tried to built this code to textbox body but I think that it's wrong. There is already a macro in combo box, so I don't now how to fix it. Name of textbox is txtDivision and combobox is cboResponsible
I have a series of 2 combo boxes. I have a macro called Generate which will change the options in the second combo box based on the number the first combo box returns. However this requires the user to press a button to execute this macro. I would like this macro to execute automatically when the number in the first combo box's linked cell changes.
This is the code I have previously tried, however the change in the link cell which is B2 doesn't seem to trigger the event.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2")) Is Nothing Then Generate
End Sub
As mentioned before, a Worksheet_Change event will only be triggered by physically changing a cell, and thus your linked cell won't have any effect.
If it's a cell in another wb that needs to trigger your Generate Sub, then I'd advise using a Worksheet_Change event for said wb.
In my own project, I have a sub in a regular module:
Dim AddNew As Workbook
Set AddNew = Workbooks("") 'change this
Set oWb.Workbook = AddNew
Then, in a class module:
Public WithEvents m_wb As Workbook
Public Property Set Workbook(wb As Workbook)
Set m_wb = wb
End Property
Public Property Get Workbook() As Workbook
Set Workbook = m_wb
End Property
Public Sub m_wb_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'In here, you could trigger the Generate Sub if a specific cell changes
End Sub
The simplest solution was to give the first combo box an input range and also assign it the Generate macro
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'm working on Excel and making use of a userform. The idea is to highlight a row, then click a button which opens my useform. Column 10 of the active row consists of comments. I have coded the button so that when it is clicked, the textbox is automatically populated with the contents from column 10. The user can then add to these current comments and then press submit to make the changes.
The problem is that when the button is pressed the first time, the useform will not come populated with the comments. If I exit and do the procuedre again, then it works. What have I done wrong? The code for the button is below (textbox is called 'comment'):
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Set ws1 = Worksheets("Now")
UserForm1.Show
'Add current comments to box
UserForm1.Comment.Value = ws1.Cells(ActiveCell.row, 10).Value
End Sub
Try rearranging your code to be like this:
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Set ws1 = Worksheets("Now")
'Add current comments to box
UserForm1.comment.Value = ws1.Cells(ActiveCell.Row, 10).Value
UserForm1.Show
End Sub
This should load the data into the text box before it is shown to you.