VBA Code Help

buyers

Board Regular
Joined
Jan 7, 2016
Messages
54
Hello, I'm trying to combine two pieces of code and have been getting error messages. Below is the code I am trying to use. I have also attached a picture of the worksheet. Appreciate any help you might have!

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(ActiveCell, Range("H3")) Is Nothing Then
' Change "E3" to the cell the user completes the year in
Const tlbName = "Table3" 'update to name of table name on relevant sheet

Dim DateCol
Dim MinYear
Dim MaxYear

DateCol = "H:H" 'change to match your date column

MinYear = Year(WorksheetFunction.Min(ActiveSheet.Range(DateCol)))
MaxYear = Year(WorksheetFunction.Max(ActiveSheet.Range(DateCol)))

With ActiveSheet
If Target.Value >= MinYear And Target.Value <= MaxYear Then
.ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1, Criteria1:="TRUE"

Else
.ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1


End If
End With
End If

End Sub

Sub GetListObjectNames()

Dim ws As Worksheet
Dim lo As ListObject
Dim rng As Range

Set ws = ActiveSheet

For Each lo In ws.ListObjects
Debug.Print lo.Name
MsgBox lo.Name
Next lo

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180830
Dim xCellColumn As Integer
Dim xTimeColumn As Integer
Dim xRow, xCol As Integer
Dim xDPRg, xRg As Range
xCellColumn = 20
xTimeColumn = 19
xRow = Target.Row
xCol = Target.Column
If Target.Text <> "" Then
If xCol = xCellColumn Then
Cells(xRow, xTimeColumn) = Now()
Else
On Error Resume Next
Set xDPRg = Target.Dependents
For Each xRg In xDPRg
If xRg.Column = xCellColumn Then
Cells(xRg.Row, xTimeColumn) = Now()
End If
Next
End If
End If
End Sub
Screen Shot 2019-12-07 at 10.29.10 PM.png
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Here the united codes.
The second part I don't understand very well what you want to do, if you can explain what you need, I can help you change it.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("H3")) Is Nothing Then
    If Target.Count > 1 Then Exit Sub
    ' Change "E3" to the cell the user completes the year in
    Const tlbName = "Table3" 'update to name of table name on relevant sheet
    Dim DateCol, MinYear, MaxYear
    
    DateCol = "H:H" 'change to match your date column
    MinYear = Year(WorksheetFunction.Min(ActiveSheet.Range(DateCol)))
    MaxYear = Year(WorksheetFunction.Max(ActiveSheet.Range(DateCol)))
    
    With ActiveSheet
      If Target.Value >= MinYear And Target.Value <= MaxYear Then
        .ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1, Criteria1:="TRUE"
      Else
        .ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1
      End If
    End With
  End If
  '
  Dim xCellColumn As Integer
  Dim xTimeColumn As Integer
  Dim xRow, xCol As Integer
  Dim xDPRg, xRg As Range
  xCellColumn = 20
  xTimeColumn = 19
  xRow = Target.Row
  xCol = Target.Column
  If Target.Text <> "" Then
    If xCol = xCellColumn Then
      Cells(xRow, xTimeColumn) = Now()
    Else
      'On Error Resume Next
      Set xDPRg = Target '.Dependents
      For Each xRg In xDPRg
        If xRg.Column = xCellColumn Then
          Cells(xRg.Row, xTimeColumn) = Now()
        End If
      Next
    End If
  End If
End Sub

Sub GetListObjectNames()
  Dim ws As Worksheet
  Dim lo As ListObject
  Dim rng As Range
  Set ws = ActiveSheet
  For Each lo In ws.ListObjects
    Debug.Print lo.Name
    MsgBox lo.Name
  Next lo
End Sub
 
Upvote 0
Thank you so much for above! The Timestamp now works, but the date sort only seems to work for 2018, 2019,2020. For some reason when I put in 2021 it shows all the "False" rows as shown in the below image. Also, I have "All" in the drop down list for "Year", but that is intended to show all for "Rep", instead it is showing all regardless of who the rep is. Is there a way for my Rep drop down list and Year drop down list will each have "All" as an option so that if "Rep 1" is selected and the year is "All" it will only show all rows for Rep 1. Really appreciate your help.

image001.png
 
Upvote 0
Ok, I messed around with it and if I change both of below to "TRUE" is seems to work good for Year: "H3", but need it to do the same for Rep: "E3" and run the auto filter for "TRUE". Can I just copy the first 3 lines of code and paste them somewhere with the "E3" cell instead of "H3" or can I do a formula for "And" within the current formulas? Thanks again

VBA Code:
    With ActiveSheet
      If Target.Value >= MinYear And Target.Value <= MaxYear Then
        .ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1, Criteria1:="TRUE"
      Else
        .ListObjects(tlbName).Range.Columns(1).AutoFilter Field:=1, Criteria1:="TRUE"
 
Upvote 0

Forum statistics

Threads
1,214,840
Messages
6,121,895
Members
449,058
Latest member
Guy Boot

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top