Reopen a UserForm - excel

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.

Related

Copying the activecell to a cleared textbox and keeping the hyperlink

Goals
I am trying to make a macro that will copy the active cell and paste the contents of that cell to a text box.
I also need it to clear the text box before adding the new value, as well as keeping the original hyperlink source.
Context
I have a large list of items that have hyperlinks to various web pages. What I ultimately want is for a user to search a specific item, using the Ctrl + F function, and the item be copied with its hyperlink into a textbox at the top of the sheet.
Then once a new Item is searched it will clear the textbox and copy the new item into it.
Code
All it does is copy the active cell into the text box. I will not clear the box beforehand or copy the hyperlink.
Option Explicit
Private Sub TextBox1_Change()
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Copy
Sheet1.TextBox1.Paste
End Sub
If you're after the hyperlink address, in the text box then just use:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet1.TextBox1 = Target.Hyperlinks(1).Address
End Sub
If you're after the text, but want the hyperlink to fire when the button (or text) is pressed - I'm not sure if either are possible. I guess perhaps you could use the OnAction to cause a hyperlink to fire.. but we'd need more info. Is this an ActiveX Textbox on a sheet?

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.

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.

Is there a Macro to hide all sheets in a workbook when a certain cell is equal to 100%?

I understand the logic behind this but I'm unsure how to right the macro. I import up to 63 sheets of data into excel.
ALL of the sheets have a status in Column B Row 9. I would like to make a macro to hide all sheets in the workbook when B9 = 100%
If Worksheet.Column.B, Row.9= 100%
Worksheet.hide
Open the VB Editor ALT+F11. Under Microsoft Excel Objects right click Insert --> Module. Paste in the following code.
Option Explicit
Public Sub HideSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("B9").Value = 1 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
The Option Explicit setting forces you to declare variables. I include this because, on top of good coding practice, it has saved me hours of debugging only to find I spelled a variable name wrong (such errors are captured before the code begins when adding this line).
The basic principle is that the code uses a For..Each loop to iterate through each worksheet in the workbook. IF cell B9 is 1 (corresponding to 100%) then the worksheet's Visible property is set to xlSheetHidden which hides the sheet. Sheets with this visible property can be unhidden if the user right-clicks along the worksheet tabs and selects Unhide.... If you don't want users to be able to unhide the sheets, you can set it to xlSheetVeryHidden which hides the sheet and disabled unhiding the sheet from the UI.
To run this macro you can click anywhere inside the code and click the button that looks like play (this is the Run Sub/Userform button) or you can press F5.
I would recommend setting the macro to a keyboard shortcut, or if you prefer to a button located somewhere on the worksheet.
To assign the macro a keyboard shortcut:
Under the Developer tab select Macros (or simply press ALT+F8) to display the Macro window
Under Macro name: select the name of your macro (HideSheets in this example)
Click Options...
Put the key in that you want to press to run the macro (in this case I chose CTRL+h for hide)
Select OK
Test by pressing the keyboard combination you specified
Additionally, you can assign a macro to run when a button on the worksheet is clicked, to do this:
Under Developer go to the Insert dropdown
Under ActiveX controls, select the command button
Draw the button anywhere on the page
Right click the button --> CommandButton Object --> Edit
Change the button text to whatever you want (like Hide Sheets for example)
Double click the button to open the code, you should see a Sub entitled CommandButton1_Click()
Type HideSheets into the subroutine like this (or whatever the name of your subroutine is)
Private Sub CommandButton1_Click()
HideSheets
End Sub
Exit design mode by clicking Design Mode under the Developer tab
Click the button to ensure the macro functions
Building on the answer from Soulfire, you can run the automatically anytime the value in the cell changes value. Just put the following code under the worksheet (not the module), which will run the macro 'HideSheets' whenever the value in cell C9 changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Call HideSheets
End If
End Sub

Excel: Assign a macro to a hyperlink?

How do I assign a macro to a hyperlink?
You can do it using the Worksheet_FollowHyperlink event.
For example I recorded a macro named Macro1 and the following code will run the macro whenever the hyperlink is clicked
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Run ("Macro1")
End Sub
But this is not a very effective solution. My hyperlinks points to the same sheet (an by default to the 1st cell) so when ever the hyperlink is clicked the first cell in this sheet will be selected automatically.
I didn't investigate more on this. you can simply cancel the navigation (don't know if possible) or set the hyperlink property to the current cell so that the selection stays in the same cell.

Resources