Get textbox value in user form from cell - excel

I have an user form that displays textbox with current path to shared Excel workbook.
It is used in many macros to get workbook's path (using line UserForm1.TextBox1.Value)
The textbox itself has Activate Event, that displays data from cell (user can change cell value if they move shared workbook somewhere)
Private Sub UserForm_Activate()
TextBox1.Value = Workbooks("personal.xlsb").Worksheets("sheet").range("A2").Value
End Sub
But even when textbox displays correct value, it's value in properties stays the same as it was when it was created. It doesn't update. How it looks in VBA editor:
So whenever user updates the path, textbox displays correct path, but all macros using it still point to the original value.
Is it possible to save textbox displayed value also in the properties?

it's value in properties stays the same
Is it possible to save> textbox displayed value also in the
properties?
I actually can't grasp the meaning of "properties"
so here's a blind shot
add:
Private Sub TextBox1_Change()
Workbooks("personal.xlsb").Worksheets("sheet").Range("A2").Value = Me.TextBox1.Value
End Sub
BTW, you should also make sure your "personal.xlsb" workbook actually has a worksheet named after "Sheet"

Related

refedit sum in textbox

I have a UserForm with refedit, textbox, and command button controls.
I would like the user to be able to select a range of cells from the active worksheet with the refEdit control, and as the range values are selected, the sum of its values are displayed in the text box.
Once the total of the ranges are selected, the user will click the command button, which should copy the value from textbox, close current UserForm, open another UserForm and paste the value into a textbox.
However, when I click the refEdit control, it only shows the refEdit textbox by minimizing the user form until the button is clicked. How can I prevent this from happening?
Also, the code I wrote for the textbox doesn't work, in fact it doesn't do anything:
Private Sub RefEdit1_Change()
txtbxSum.Value = Sum(RefEdit1.Value)
End Sub
Thanks!
The RefEditcontrol has its issues, so it may not be the best design choice. As Ashleedawg commented, see here
That said, the RefEdit.Value property is a string representing the selected range. So to Sum that range you need to use
Private Sub RefEdit1_Change()
txtbxSum.Value = Application.Sum(Range(RefEdit1.Value))
End If
Note that RefEdit1_Change fires every time the Selected Range changes (so if the user drags over a 3 cell range it will fire three times). If you also have a txtbxSum_Change event it will fire each time the refedit1_Change event updates the
Textbox with new value, ie as the selected range Sum changes.

Rowsource not working via VBA

I can't get the RowSource property of a list box to update via VBA. From another thread, I found the syntax, so I think this is correct. But, despite not failing, it doesn't do anything to the RowSource property (it remains blank). Below:
frmAddIngredient is the user form.
lbxIngredient is a listbox control in that form.
UniqueIngredients is one of the sheets in the workbook.
NumberOfItems is 1 (in this case).
It doesn't give an error, but it doesn't change anything, either. The form itself is not active at this time. This code is supposed to set up the form for later showing.
frmAddIngredient.lbxIngredient.RowSource = Sheets("UniqueIngredients").Range("A1:A" & CStr(NumberOfItems)).Address
The most recent code is
frmAddIngredient.lbxIngredient.RowSource = "=UniqueIngredients!A1:A1"
but, it still doesn't change anything in the actual form.
Also, can I add a new post, or do I have to continue editing this one and adding stuff?
What you want (as discussed in the comments on your question) is not possible. Setting something by code does not change it's property in the properties window and is only so until your project resets.
Consider a Userform with 2 buttons, with their original name and caption and then in a module paste these 2 subs.
Sub demo1()
UserForm1.CommandButton1.Caption = "Demo 1"
UserForm1.Show
End Sub
Sub demo2()
UserForm1.CommandButton2.Caption = "Demo 2"
UserForm1.Show
End Sub
When you run the first Sub demo1 Button 1's caption has changed but Button 2's caption has not.
Close the Userfom and now run demo2, you'll see that Button 1's caption is back to it's original hard set (properties window) name and that now Button 2 has a different name.

Link ActiveX to Excel

Hope somebody can help!
I am programming in VB6 and am trying to write an activeX control for an indicator. The indicator should change color relative to an excel open workbook cell being true or false. The indicator should be auto updating i.e. the indicator needs to link live to the excel cell.
I can then place several of the indicators c/w links to different cells on a userform. The workbook is opened and tested in the userform and object references set up ok.
I can't figure out how to link the indicator to the excel cell.
This is part of a larger project I am trying. Other control such as bargraphs, Switches etc. to be added if I can get the first one working.
Thanks in advance
You need to first add a reference to the Microsoft Excel x.x library in your control's references.
Next, you should add a private module level variable of type Excel.Worksheet, and declare it WithEvents, e.g.
Private WithEvents m_oWorksheet As Excel.Worksheet
You should also create a Property Set procedure called Worksheet which sets this variable.
Then you should add code for its Change event, e.g.
Private Sub m_oWorksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row = 2 Then ' m_oWorksheet.Range("A2")
'Do some coding here
End If
End Sub
Obviously, .Column = 1 and .Row = 2 would be replaced by the cell coordinates you are interested in. I originally used coordinates like "A2", but found that the objects returned from m_oWorksheet.Range("A2") cannot be directly compared with the Target object, e.g.
If Target Is m_oWorksheet.Range("A2") Then
I tried to extract the cell reference "A2" from Target, but I can't seem to find a way to do it, unless you write a function to do the conversion of Column/Row to a string reference.
Note the previous answer I provided was very wrong, since I was testing
If Target = m_oWorksheet.Range("A2) Then
... which only worked because the default value properties were identical. This would fall over if any changed cell had the same value as a "watched" cell.

Excel combobox listfillrange property pointing at a formula-based named range has issues

ActiveX combobox objects in Excel do not behave well when their ListFillRange refers to a formula-based Named Range (Defined Name).
I think I have encountered other errors and possibly even Excel crashes thanks to this, but right now all that happens is the combobox_change() event is triggered anytime ANY cell in the workbook is changed.
I am not sure if this is really a bug, or if there is a fix, or a workaround. If it is a bug, how do I report it to the Excel people?
And finally, the real meat of my question is "How do I work around this issue best?" I would like to have some formula-based named ranges, but it seems like this won't be possible.
To reproduce this bug, do the following:
Create a new workbook. On Sheet3, create a small table 3 columns across, and several rows high.
Create a named range with this formula (or an equivalent): =OFFSET(Sheet3!$A$2:$C$36,0,0,COUNTA(Sheet3!$A:$A),COUNTA(Sheet3!$4:$4)) To do this use Input>Name>Define. Name the range something like "demoRange"
Go to Sheet1 and create a combobox, (it must be on a separate sheet). (Use the Control Toolbox menu, not the Forms menu).
Click on the Design Mode button (the blue triangle with pencil), then right click on the combo box and go to Properties.
In the properties window for the combobox, change the ListFillRange property so that it points at the named range you created in step 2 ("demoRange").
You may want to change the ColumnCount property to 3, and the ColumnWidths property to "50,50,50"
Set the linkedCell property to cell "A1" by typing A1 in the linkedCell property.
Close the properties window, and double click on the combobox to define its change() event.
Put a Debug.Assert(false) or Msgbox("demo") line in the subroutine for the new combobox's change event.
Exit design mode
important - Now select an item in the combobox. The event should trigger normally the first time. (The bug will not show if you don't do this step--something must be selected in the combobox)
Edit cells anywhere in the workbook [Edit] or any other open workbook [/edit], on any sheet and any location. Each time you edit any cell, (at least for me), the onchange event for the combo box is run.
Again, is this normal, and what is the best alternative for what I am doing? This combo box gets linked to various cells, and is supposed to be a replacement for the tiny font in the data validation dropdowns excel provides by default.
My advice is to never use ListFillRange and LinkedCell. They are just trouble. Fill your listbox with List and use the Change event to write to the cell. Somewhere, maybe the Workbook_Open event, fill the listbox
Private Sub Workbook_Open()
Sheet2.ListBox1.Clear
Sheet2.ListBox1.List = Sheet1.Range("demoRange").Value
End Sub
Then in the change event in the Sheet2 module, check that something was clicked and write it to the cell
Private Sub ListBox1_Change()
If Me.ListBox1.ListIndex >= 0 Then
Sheet2.Range("A1").Value = Me.ListBox1.Value
End If
End Sub
I have a few options available that I am aware of thus far. The best I can come up with is this:
Avoid directly using formula-based named ranges. Instead, define a subroutine that will check whether the defined range "demoRange" should be changed from what its current value is. Run this subroutine on the workbook_open and sheet3_deactivate events. If needed, prompt the user to ask if it's all right to update the named range. [edit] The macro that updates "demoRange" could probably just copy from a "demoRange_FormulaBased" named range into "demoRange" which would be static. [/edit]
This solution works well because you can keep using the linkedcell property, you don't have to use VBA to populate the comboboxes, and the named range can still be used for whatever other purposes it already had. Avoid using the onchange event to run this new subroutine, since it might end up being triggered thousands of times if a user opens the Find/Replace dialog and chooses "Replace All".

MS-Excel: How to show the value of a combo box inside a locked cell

Background Details
I have an excel spreadsheet with Activex dropdown (combobox) objects which help the user to know what options are available. I did this because the data validation list dropdowns are way too small in font size, and were gathering a lot of complaints.
So my solution was to add combobox objects which allow the user to select from a range of options. However, I have to link the comboboxes to a cell with the linkedcell property, so that both the user and various formulas can see what has been chosen. I also set up the combobox to disappear when it's not in use (much in the same way as the data validation dropdown button only appears when you select the relevant cell).
Here is the problem:
I don't want the users to edit the value in the linked cell, so I make sure the linked cell is locked whenever the combobox is not selected:
Private Sub comboBox1_GotFocus()
Call unlockComboBoxTargetCell(comboBox1)
End Sub
the procedure above does this:
If (targetComboBox.LinkedCell <> "") Then
Dim targetCell As Variant
Set targetCell = Range(targetComboBox.LinkedCell)
If Not targetCell Is Nothing And targetCell.Locked <> False Then
unlockSheet (activesheet.Name)
targetCell.MergeArea.Locked = False
lockSheet (activesheet.Name)
End If
End If
Equivalent procedures exist to lock the target cell.
However, whenever you do a "Save As" action on the workbook, it seems that the linked and locked cells create a problem: Excel gives this error out of the blue:
"The cell or chart you are trying to change is protected and therefore read-only..."
This error comes up about twice or three times for each cell that is locked and is the linkedcell for a combobox.
Is there a good way to overcome this problem? Right now my best solution is to leave the cells unlocked and place data validation on the cell, so that if the user edits the cell they will at least be refused when they type something invalid. I could make sure that the combobox covers up the linked cell whenever it is selected, but sometimes that means having a very large, annoying combo box with a very tiny dropdown button on its right side.
Perhaps I am being a bit too particular about the user interface?
Thanks in advance for reading this long and involved post.
In the "lockSheet" procedure you have created, the code to 'protect' the worksheet needs an additional parameter, UserInterfaceOnly, set to true.
I imagine the LockSheet sub is something like this;
sub lockSheet(strSheetName as string)
thisworkbook.sheets(strSheetName).Protect
end sub
Try this:
sub lockSheet(strSheetName as string)
thisworkbook.sheets(strSheetName).Protect, UserInterfaceOnly=True
end sub
UserInterfaceOnly allows programmatic changes to the protected sheet.
Bill

Resources