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.
Related
I created a userform in Excel 2016 with two ListBoxes, using the Tools menu. I double clicked them to create subs and inserted code to check whenever one is selected.
Here is the code:
Private sub SaleType_Click ()
If SaleType.Value ="Core" then
'make sale label visible
QTDV.visible =true
' show core option btn
Core.Visible = true
End if
End sub
When I have a ListBox created from the toolbox this works, but every other time the form is run the saletype ListBox will be value null and this is a problem because I have a check to make sure the ListBox is not empty. Code follows:
If saletype = "" then
Dim msg as string Msg = " please select sale type"
Msgbox msg, and vbcritical
End if
If the ListBox presents value null it will not see it as empty and skip the check if I try saletype = null it still skips it.
I searched and it seems creating ListBoxes on the tool box is weird because Excel does not know what kind of control it is. I opted for creating the ListBoxes in VBA.
Private sub userform_initialize()
Dim saletype as msforms.Listbox
Set saletype = me.Controls.Add("Forms.ListBox.1", "SaleType")
But when running the form and selecting any option on the ListBox the SaleType_Click sub does not trigger.
If you want to implement event handling (like SaleType_Click) you need to declare the object with the WithEvents keyword:
Dim WithEvents saletype as msforms.Listbox
And if a variable/property is not set then its value doesn't exist, so instead of empty string ("") you need to validate for NULL - the IsNull function can do that (= NULL doesn't work as = only works with values):
If IsNull (saletype.Value) then
I just had a problem with my listbox_Click not being fired "every other time". Maybe it was when the same selection was desired twice in a row. Anyway, put this in the sheet code for the sheet that is "Show"ing the userform:
userformXXX.listboxYYY.ListIndex = -1
userformXXX.Show
This doesn't work if it is in the userform code.
I want to use VBA code for the following: based on a cell output (either "TRUE" or "FALSE" in cell W20, which changes based on whether a user checks a box or not), I want rows to be hidden/unhidden on another worksheet called "Analysis" worksheet based on the code below. I right clicked the "Summary" tab where cell W20 and the checkbox are located -> view code -> wrote the code below, but it's not working. I am very new to VBA, please help, it would be very appreciated, thanks in advance
Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("$W$20")
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = False
End If
End If
End Sub
Excel doesn't recognize the modification of a cell value caused by the manipulation of form controls. I assume this is because Excel links a form control to a value and applies the changes itself, therefore not considering it a user change in the spreadsheet.
In order to make your code work, you will have to move your code into a module as a click event of your form control. Click the Developer tab and then Visual Basic. Once Visual Basic is open, you will notice on the left pane that you have a list of your worksheets within a VBA Project bearing the name of your workbook and your macro is currently within one of them. Once you found it, delete the code of your macro. Then, right-click on the name of your workbook and then select Insert > Module...
On the coding pane, insert this modified code:
Sub NameOfYourFormControlCheckBox_Click()
Dim MyRange As Range
Set MyRange = Range("$W$20")
If MyRange = False Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
ElseIf MyRange = True Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
End Sub
Make sure that the name of the sub is the name of your form control (checkbox) followed by an underscore and then "click".
Hope this helps!
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?
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.