Help In Optimizing My Code Please

pure vito

Board Regular
Joined
Oct 7, 2021
Messages
183
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have this code for each of my buttons with only one difference highlighted in red 10-100 , 10-300 , 10-350
and I have this code repeated for over 30 times with the same differences, my question is it possible for this code to be compressed into one code and thanks in advance


VBA Code:
Sub Macro20()

'
' Macro15 Macro
'
' Keyboard Shortcut: Ctrl+q
'
    Application.ScreenUpdating = False
    
         Sheets("Sheet2").Select
    Sheets("Sheet1 (2)").Visible = True
    Sheets("Sheet1 (2)").Select
    ActiveSheet.Range("$a$1:$G$463").AutoFilter Field:=3, Criteria1:=[COLOR=rgb(226, 80, 65)]"10-100"[/COLOR]
    ActiveWindow.SmallScroll Down:=-12
    Range("A2:G659").Select
    Selection.Copy
    Sheets("Pick Form").Select
    Range("A:A").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("a8").Select
            ActiveSheet.Shapes.Range(Array("Rectangle: Rounded Corners 24")).Select
   With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 255)
        .Transparency = 0
        .Solid
         Range("a8").Select
                                    Sheets("Sheet1 (2)").Select
    ActiveWindow.SelectedSheets.Visible = False
    
    Sheets("Pick Form").Select
         
    Application.ScreenUpdating = True
    End With
End Sub

Sub Macro21()

'
' Macro15 Macro
'
' Keyboard Shortcut: Ctrl+q
'
    Application.ScreenUpdating = False
             Sheets("Pick Form").Select
    Sheets("Sheet1 (2)").Visible = True
    
    Sheets("Sheet1 (2)").Select
    ActiveSheet.Range("$a$1:$G$463").AutoFilter Field:=3, Criteria1:=[COLOR=rgb(226, 80, 65)]"10-300"[/COLOR]
    ActiveWindow.SmallScroll Down:=-12
    Range("A2:G659").Select
    Selection.Copy
    Sheets("Pick Form").Select
    Range("A:A").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("a8").Select
            ActiveSheet.Shapes.Range(Array("Rectangle: Rounded Corners 33")).Select
   With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 255)
        .Transparency = 0
        .Solid
         Range("a8").Select
    Sheets("Sheet1 (2)").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Pick Form").Select
         
    Application.ScreenUpdating = True
    End With
End Sub
Sub Macro22()

'
' Macro15 Macro
'
' Keyboard Shortcut: Ctrl+q
'
    Application.ScreenUpdating = False
    Sheets("Pick Form").Select
    Sheets("Sheet1 (2)").Visible = True
    
    Sheets("Sheet1 (2)").Select
    ActiveSheet.Range("$a$1:$G$463").AutoFilter Field:=3, Criteria1:=[COLOR=rgb(226, 80, 65)]"10-350"[/COLOR]
    ActiveWindow.SmallScroll Down:=-12
    Range("A2:G659").Select
    Selection.Copy
    Sheets("Pick Form").Select
    Range("A:A").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("a8").Select
            ActiveSheet.Shapes.Range(Array("Rectangle: Rounded Corners 31")).Select
   With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 255)
        .Transparency = 0
        .Solid
         Range("a8").Select
    
    Sheets("Sheet1 (2)").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Pick Form").Select
         
    Application.ScreenUpdating = True
    End With
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You can do something like this:

VBA Code:
Sub Macro20()

'
' Macro15 Macro
'
' Keyboard Shortcut: Ctrl+q
'
    FilterAndCopy "10-100", "Rectangle: Rounded Corners 24"
End Sub

Sub Macro21()

'
' Macro15 Macro
'
' Keyboard Shortcut: Ctrl+q
'
    FilterAndCopy "10-300", "Rectangle: Rounded Corners 33"
End Sub
Sub Macro22()

'
' Macro15 Macro
'
' Keyboard Shortcut: Ctrl+q
'
        FilterAndCopy "10-350", "Rectangle: Rounded Corners 31"
End Sub

Sub FilterAndCopy(FilterCriteria As String, shapeName As String)
    Dim sourceSheet As Worksheet
    Set sourceSheet = Sheets("Sheet1 (2)")
    Dim pickSheet As Worksheet
    Set picsheet = Sheets("Pick Form")
    
    Application.ScreenUpdating = False
    
    With sourceSheet
      .Visible = True
      .Range("$a$1:$G$463").AutoFilter Field:=3, Criteria1:=FilterCriteria
      .Range("A2:G659").Copy
    With pickSheet
      .Cells(.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                                                               SkipBlanks:=False, Transpose:=False
      With .Shapes(shapeName).Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 255)
        .Transparency = 0
        .Solid
      End With
    End With
    sourceSheet.Visible = xlSheetHidden
   pickSheet.Select
         
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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