How to repeat command button in each row in excel - excel

I'm creating a table in excel where the user can use a command button to create a clickable link to a document.
The table has various formulas to calculate dates etc. and it is an excel table, not a range of cells in excel.
So far I have copied the button for about 100 rows.
How do I get the file link button to repeat for each row?

Without the need of a CommandButton, the Code below combines Worksheet_SelectionChange and Intersect to create the link in the selected cell inside a specified Range as you click on it.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(ActiveSheet.Range("X3:X400"), Target) Is Nothing Then
ActiveSheet.Hyperlinks.Add Anchor:=Target, _
Address:="hhtp:\\YourLinkAddress.org", _
TextToDisplay:="Link"
End If
End Sub
Modify it to your Needs. You will get the values of the Row by using Target.Offset

Related

Excel VBA Run Macro when Cell is deselected

I have a table tbl1 with a list of names. When I select any row in the column "name", I would like to run a macro to filter another table tbl2. When I deselect that same row, I would like to run another macro to unfilter tbl2.
So far, I have gotten the filter on select part working, but I am stuck on how to get it to unfilter on deselect. FilterTableByName and UnfilterTableByName are both macros that will filter/unfilter tbl2.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(ActiveSheet.ListObjects("tbl1").ListColumns("name").Range, Target) Is Nothing Then
Call FilterTableByName(ActiveCell.Row)
End If
End Sub
An alternate solution that I thought of was to call unfilter and then filter macros on select, and then call unfilter when I leave the worksheet, but I was wondering if there was a cleaner way to unfilter instead of running it every time I click a different worksheet. Thanks

Need to run a macro based on unique ID in worksheet cell

Users will enter new information into a sheet using an userform. This information is tied to a unique ID given at time of entry (i.e. 2019-7 for the 7th item in this year). Currently, each piece of information is placed into separate cells in a row. I want to hide some of these cells but be able to allow users to click on the unique ID at the start of that row, which will run a macro that will create a new sheet to display all information in a user friendly way.
I have tried creating a hyperlink to a run a macro but the code for identifying the hyperlink can't be cell specific. I need a more dynamic way to have excel recognize which ID was clicked and then use that to gather the rest of the information in that row (i.e. the hidden columns).
My best option had been to put a private sub in the worksheet to recognize when a cell was changed/clicked but couldn't get past it just identifying the cell. I need it to then identify the contents of that cell, which would be the unique ID.
I have no issues with creating a hyperlink to nothing, I just need excel to run a macro when a hyperlink is clicked and then within that macro, identify the unique ID and/or row that was clicked. Once I have the ID or Row identified, I then can go from there to grab the rest of the info in the other columns.
You can use the BeforeDoubleClick event of the Worksheet:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address <> "$A$7" Then Exit Sub
' below, call the functions/subs that produce your output worksheet
MsgBox "You clicked on " & Target.Address
End Sub
Above only works on cell A7. If you need this to apply to entire column A, modify slightly:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
' below, call the functions/subs that produce your output worksheet
MsgBox "You clicked on " & Target.Address
End Sub

Data Validation with filter

I am aware of how to create a combobox to emulate a data validation cell based off of a range list, featuring the ability to filter the dropdown list based on what you type in. This is similar in functionality to how column filtering in an Excel table works, where if I start typing "tor" it will immediately filter the preview window for items only containing the string "tor".
However, I am trying to create a report where every row requires the same dropdown menu, and ideally what I hope to do is have each row have this cell where you can click on the dropdown, start typing, and the list will narrow down. However, I realize a combobox is a unique object and it cannot be duplicated as simply as copying and inserting a row with a data validation field.
Is there a way I can easily duplicate this sort of drop down where a list preview filters based on what is typed into the cell, either via vba or plain data validation? In short, can a dropdown with the filtering preview capability as my code below be copied and pasted as easily as a simple data validated cell within a row?
For reference here is my combobox code, listing range A2:A791 on sheet DVTest:
Sub ComboBox1_Populate(Optional fltr As String)
ComboBox1.List =
Filter(Application.Transpose(Worksheets("DVTest").Range("A2:A791" &
SymbolCount).Value), fltr)
End Sub
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
Call ComboBox1_Populate(ComboBox1.Text)
End Sub
Private Sub UserForm_Initialize()
Call ComboBox1_Populate
End Sub
Thanks kindly!

Change width of data validation dropdown

In my workbook, cells AR8:AS8 are merged and in the cell is a data validation dropdown list. The source of the list uses the formula =indirect(GG8) and this refers to lists in a different tab.
My problem is that when I click on the dropdown, the box isn't wide enough to show the full item.
Is there any way of changing this? I would prefer to NOT use VBA if possible..
I look forward to your responses :)
There is no possible way to achieve this W/O VBA. if you wish to use VBA solution, then please find code below. You have to paste this code to your Worksheet module, not Regular module, and adjust based on comments.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Address = "$H$1" Then 'adjust this range to your drop down list
Target.Columns.ColumnWidth = 30 'adjust to your needs
Else
Columns(8).ColumnWidth = 8 'adjust column number to column with drop down values
End If
End Sub
When dropdown is not selected:
Dropdown selected:

Using VB to alter row colour in Excel if checkbox on same row is ticked

I have an Excel spreadsheet listing various courses. I have checkboxes in column F for each course (not for every row - there are some gaps). When a checkbox is ticked I want to alter the color of cells B to E on the same row.
Would there be a way of doing this in one method rather than copying the code for each checkbox?
Any help appreciated as always - thanks!
The checkbox idea will be hard to implement. But if you want to have a clickable way to set the background of the cells in that row you can insert a hyperlink on your course title. Have the hyperlink point to the adjacent cells in that row. So if the title is on B3, have the hyperlink point to C3:E3, for example.
Implement the Worksheet_FollowHyperlink event on your sheet's code-behind. Here is a sample sub to get you started:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim myRange As Range
Set myRange = Range(Application.Selection.Address)
myRange.Interior.Color = RGB(127, 256, 256)
End Sub
This works on Excel 2007. Untested on other versions.
Assign the same macro to all the checkboxes. The issue you may have is trying to figure out what row the checkbox sits on. The controls don't actually sit in the grid. They sit on top of the worksheet and have no reference to the rows.

Resources