refedit sum in textbox - excel

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.

Related

Get textbox value in user form from cell

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"

Code for automatic updating Userform

I'd like to know on how to code a userform (VBA Excel) with automatically updating itself when the cell values has changed.
I have produced a button that will show the userform with labels and text boxes. But whenever i click it yes it shows up but i need to click the userform in order for me to see the values.
Need help.
Thank you in advance,
Tramyer
The Worksheet.Change event is fired everytime you change the contents of a cell on a given worksheet.
You can create an event handler in the worksheet module (usually labeled Sheet1, Sheet2 etc. in the VBA editor) that is called every time the event is fired:
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address
End Sub
This example just outputs the address of the cell that was changed, but you can adapt this to update your userform with the changed values instead.

Reopen a UserForm

I have been stuggling for same time now but I don't now how to do that...
When I select a cell in column Q, I get a userform (which is ok).
In that userform I have the content of the selected cell (e.g. Q6) (which is ok)
In userform I have two buttons:
- quit: which closes the userform without saving the text in the cell;
- save: which save the text in the cell from userform.
Both at saving and quitting the selections (focus) remain on that cell (Q6).
I want when I click again on the Q6 (witch is already selected), the userform reopen again.
The only solution I found so far, is to change the selection (for exemple on P6).
Cam anyone, please, help me? Thx.
In addition to how you are currently showing the form, you could add a double-click event handler. In the sheet's code module do something like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = Range("Q1").Column Then
UserForm1.Show
Cancel = True
End If
End Sub
Instruct the users that if they want to reshow the form, they should double click on the currently selected 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