Combine vba code for use in one button

fishandtril

New Member
Joined
Nov 15, 2022
Messages
13
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Is there a way to join these two pieces of code so the one button executes the code depending on what is shown. currently I have two macros; one to filter the data and another to clear the filter and unhide columns.

VBA Code:
Sub Customer_Sales_Profile()
'
' Calculate Unique Range then Filter for all rows = "Show"
'
    ActiveSheet.Calculate
    Rows("1:1").Select
    Columns("A:G").Select
    Selection.EntireColumn.Hidden = False
     ActiveSheet.Range("$A$1:$F$844").AutoFilter Field:=1, Criteria1:="Show"
    Columns("A:A").Select
    Selection.EntireColumn.Hidden = True
    Range("A1").Select
End Sub

Sub Customer_Sales_Profile_Clear()
'
' Clear the filters and unide columns
'
    Columns("A:A").Select
    Selection.EntireColumn.Hidden = False
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
    Range("A1").Select
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You mean depending on some condition, either do the first one or else do the other?
Then yes, by testing for the condition
VBA Code:
If Condition = something Then
    one block of code goes here
Else
    other block of code goes here
End if
You would have to drop a Sub and End Sub so that there's only one of each.
 
Upvote 0

Forum statistics

Threads
1,215,614
Messages
6,125,848
Members
449,266
Latest member
davinroach

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