Filter with auto refresh and column A filter interfering with B

SunValley

New Member
Joined
Jul 12, 2023
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Pasta1.xlsm
AB
1Group 1Group 2
245
337
436
525
6
7
82
sheet 1


When I sort column A from largest to smallest, it automatically interferes with column B. I tried in different ways, but I couldn't make the filter of column A not interfere with B and vice versa as well. My goal is to sort the names in columns A and B in order from highest to lowest, without interfering with the other columns.
I'm also trying to make the filter automatically update every time a change is made, so I'm using the following VBA:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Sheets("sheet 1").AutoFilter.ApplyFilter
End Sub

However, the filter sometimes fails as demonstrated in the xl2bb example, leaving empty cell ranges instead of sorting.
I tried to use tables with a filter instead of the simple filter as an alternative, I used the same VBA but in addition to failing a few times when sorting the cells, sometimes when adding data to the cells I got the error "Runtime Error 9 is a Visual BASIC error o which stands for Object Variable Not Defined".
 
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I managed to solve it, I had to create a table for each column separately (A and B) and use this VBA

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("A:A", "B:B")) Is Nothing Then
        Range("A1").Sort Key1:=Range("A2"), _
          Order1:=xlDescending, Header:=xlYes, _
          OrderCustom:=1, MatchCase:=False, _
          Orientation:=xlTopToBottom
        Range("B1").Sort Key1:=Range("B2"), _
          Order1:=xlDescending, Header:=xlYes, _
          OrderCustom:=1, MatchCase:=False, _
          Orientation:=xlTopToBottom
    End If
End Sub

This vba sorts column A and B separately in descending order, in addition to not interfering with other columns that are not part of VBA, without making it impossible to control + Z them.
 
Upvote 0
Solution
When using this VBA in cells that have results coming from a formula, the VBA starts to have inconsistent results and sometimes stops working randomly. And now it doesn't even work with cells that contain only numbers, it is necessary to create a new sheet for VBA to work again. can anybody help me?

I already pressed the control + break button several times as suggested in some topics and click on save VBA, but it didn't work.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Range("B1").Sort Key1:=Range("B2"), _
Order1:=xlDescending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

'Esse vba classifica de ordem decrescente a coluna A e B separadamente
'além de não interferir em outras colunas que não fazem parte do VBA,
'sem impossibiltar o control + Z delas
 
Upvote 0
When removing "On error Resume Next" from VBA, the error displayed is "to do this all the merged cells need to be the same size". But even using unmerged in the cells, the error persists.
 
Upvote 0
I was able to work around the problem by using filters to sort, instead of using VBA for that (:
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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