gleamng
Board Regular
- Joined
- Oct 8, 2016
- Messages
- 98
- Office Version
- 365
- 2021
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
- 2003 or older
- Platform
- Windows
- MacOS
- Mobile
- Web
Good day all, i need some help with the macro below which autofilter column C to sheets from sheet1 that has data in column A, B & C.
in my case i want the vba to only copy column A&B only to sheets after filtering and also should not copy the heading. attached is screen shot of sheet1 (raw data) for clarification.
in my case i want the vba to only copy column A&B only to sheets after filtering and also should not copy the heading. attached is screen shot of sheet1 (raw data) for clarification.
VBA Code:
Option Explicit
Function GetWorksheet(shtName As String) As Worksheet
On Error Resume Next
Set GetWorksheet = Worksheets(shtName)
End Function
Sub Filter()
Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String
Dim LR As String
LR = Columns("C").Cells(Rows.Count, 1).End(xlUp).Offset(0, 0)
'specify sheet name in which the data is stored
sht = "Sheet1"
'change filter column in the following code
last = Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
Set rng = Sheets(sht).Range("A1:C" & last)
Sheets(sht).Range("C1:C" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AT1"), Unique:=True
For Each x In Range([AT2], Cells(Rows.Count, "AT").End(xlUp))
If Not GetWorksheet(x.Text) Is Nothing Then
Application.DisplayAlerts = False
Sheets(x.Text).Delete
Application.DisplayAlerts = True
End If
With rng
.AutoFilter
.AutoFilter Field:=3, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy
Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.Range("A1").Value = UCase(x.Value)
ActiveSheet.Range("A2").PasteSpecial (xlPasteAll)
Columns("A:B").EntireColumn.AutoFit
Range("A1:B1").Select
Range("A1:B1").HorizontalAlignment = xlCenter
Selection.Merge
End With
Next x
'Turn off filter
Sheets(sht).AutoFilterMode = False
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
Sheet1.Activate
End Sub