Excel VBA Run Macro when Cell is deselected - excel

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

Related

Trapping Excel VBA Slicer click events

A similar unanswered question lies here: Excel Macro slicer onclick event
I have a ListObject and a Slicer on that ListObject. The slicer is called "Year". I'm able to trap the on click event with
Private Sub Year_Click()
doCalcsOnFilteredListObject
End Sub
Just right click on the slicer heading > Add Macro
Problem is that I want the trapped event to happen, after the filter selection has been applied. Right now, I trap the event, the filter is never applied, but the calc function runs fine on the (unaltered) listobject.
There are no events for ListObjects and Slicers. The Year_Click you have is an event of container object not of SlicerItem.
The workaround is creating a DUMMY worksheet that has SUBTOTAL Formula on A1 Cell and trap a SheetCalculate event on it. So that when you click on Slicer, it recalculates the formula on DUMMY!A1 after filtering.
Procedures:
Create a new sheet named "DUMMY" & put a formula in A1 cell: "=SUBTOTAL(109, Table1[YEAR])". (Adjust appropriately to your ListObject & Column names.)
Put this VBA procedure.
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
If Sh.Name = "DUMMY" Then
doCalcsOnFilteredListObject
End If
End Sub
Select criteria in Slicer.
This should trigger the desired event and call your custom procedure.
Be aware that Automatic Calculation must be turned on. If you want Manual Calculation, there is a technique using Workbook_Open that sets all sheets into Manual Calculation except the DUMMY sheet.

How to change a cell value based on active/selected cell

I am having a list of names in a Range A2:A77, in the worksheet name called Manual. whenever i choose a name, that is when a cell gets selected from the range, that active cell value should get reflected in the cell C1. Also, the macro should not work incase if i selected else where, other than the given worksheet or range.
I have googled alot but nothing seem to be matching my criteria, so i'm here, hoping for a better solution. You may ask me to achieve this by using data validation, but for that i will have to do multiple clicks and scrolling work to be done everytime. so to avoid that i'm looking for vba code to minimize the work and time.
Thank You.
I am only just learning VBA at the moment so this could be some very horible code but here goes.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells As Range
Set cells = ActiveSheet.Range("A1:A27")
If Not (Intersect(Target, cells) Is Nothing) Then
ActiveSheet.Range("C1").Value = Target.Value
End If
End Sub
Worksheet_SelectionChange is called if the selected cell in the sheet changes then using the test from InRange that I found here: VBA test if cell is in a range test if the cell is within the defined range then set the values.
Edited as sugested by #Vitaliy Prushak in comments.

How to repeat command button in each row in 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

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:

Moving Rows to another sheet in a workbook

I need Help!
I am not well versed in VBA or Macros but i cannot find any other way to accomplish what i need to do without using it.
I have a sheet which i will be using to track Purchase orders, and what i need to do is; when i have a row in sheet 1 (Purchase Orders) which has been recieved i.e. the date of receipt has been recorded in column H i need for the entire row to be cut and pasted into sheet 2 (Received orders).
The header takes up the first 7 rows the rows, so i need the macro to look at rows 8-54. Once the received items are removed from sheet 1, i need the row to also be deleted or preferably for the list to be sorted by column A moving the now empty row which has been cut from open for a future entry.
Any help would be greatly appreciated.
The "Record Macro" feature should be enough to do the task you describe.. In Excel 2007, go to the Developer tab in the Ribbon, and select "Record Macro", and perform exactly the steps you are describing. It will record the equivalent VBA code, which you can then execute - or tweak/modify.
I tested this out, here's one way to do it:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim receivedDate As Range, nextOpen As Range, isect As Range
Set receivedDate = Sheet1.Range("H8:H54")
Set isect = Application.Intersect(Target, receivedDate)
If Not (isect Is Nothing) And IsDate(Target) = True Then
Set nextOpen = Sheet2.Range("A1").End(xlDown).Offset(1, 0)
Target.EntireRow.Copy Destination:=nextOpen.EntireRow
Target.EntireRow.Delete
End If
Application.EnableEvents = True
End Sub
This would be pasted into the Sheet1 code. Any time a cell is changed on sheet1, the code checks to see if it's in the critical range that you specified. (H8:H54) If it is, it then checks to see if it's a date. If it is, it then copies the entire row, puts it in the next open row on Sheet2, and deletes the original row. The cells below it will get shifted up so there are no gaps.
Since the code functions on a cell changing event, it disables "Application.EnableEvents" in order to avoid a loop of changing a cell to call an event which changes a cell to call an event... etc.

Resources