Copying the activecell to a cleared textbox and keeping the hyperlink - excel

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?

Related

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.

Copy specific cell contents into clipboard in Excel

I am a frequent user of Excel. Now I was looking to copy some of specific cells into clipboard for pasting them in another application.
I could see the copy single cell data into clipboard. Can someone suggest steps to make a configurable way to copy specific cell data into clipboard by the click of a button?
This will work for any sheet in the workbook and clear the clipboard before copying. Right click the Excel logo just to the left of File on the menu bar, select View Code and paste in
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.CutCopyMode = False
Target.Copy
End Sub
then press ALT + Q to return to your sheet.

Is it possible to change the Enter key function for just one cell?

I have a cell (=$C$4) on a worksheet that is referenced in multiple formulas. I am using it as an end-user input cell.
I am trying to avoid selecting next cell after Enter is pressed so users can input values, hit Enter, see the results, then if needed, replace the contents with new values in the cell and hit Enter again to see different results without having to arrow-up or click on the cell again.
Is there a way to change the function of Enter for just this cell or is anyone aware of another work-around for this?
You can use sheet protection to do that:
Unlock cell C4 in the Format Cell dialog (in the Protection tab)
Make sure all other cells are locked (which they are be default)
Right click the worksheet name, click Protect sheet, uncheck Select locked cells and press OK
Done!
If for some reason this is not applicable, you can use this macro:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C4")) Is Nothing Then
Me.Range("C4").Activate
End If
End Sub
You need to place this code in the worksheet module of your worksheet. Press Alt-F11 to activate the Visual Basic Editor, double click on the worksheet in the tree view on the top left and paste your code in the main window!
I did something similar once by implementing a VBA subroutine. I think that the signature of the sub is:
Private Sub Worksheet_Change(ByVal Target As Range)
All you need to do in the body is check if the current cell is the cell below C4 then select C4, otherwise do nothing.

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