hi
I got this code from this forum. it works well . it split based on column B and update the data for each sheet has ever splited, but the problem is create new sheet when run the macro repeatedly . the sheet seems to be necessary . I no know if threre is way to get rid of this thing . any idea experts?
I got this code from this forum. it works well . it split based on column B and update the data for each sheet has ever splited, but the problem is create new sheet when run the macro repeatedly . the sheet seems to be necessary . I no know if threre is way to get rid of this thing . any idea experts?
VBA Code:
Option Explicit
Sub FilterColumn()
Dim wsData As Worksheet, wsNames As Worksheet, wsFilter As Worksheet
Dim Datarng As Range, FilterRange As Range
Dim rowcount As Long
Dim FilterCol As Variant
Dim SheetName As String
Set wsData = ThisWorkbook.Worksheets("Sheet1")
FilterCol = "B"
With Application
.ScreenUpdating = False: .DisplayAlerts = False
End With
Set wsFilter = ThisWorkbook.Worksheets.Add
With wsData
.Activate
.Unprotect Password:=""
Set Datarng = .Range("A2").CurrentRegion
.Cells(1, FilterCol).Resize(Datarng.Rows.Count).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=wsFilter.Range("A2"), Unique:=True
rowcount = wsFilter.Cells(wsFilter.Rows.Count, "A").End(xlUp).Row
wsFilter.Range("B2").Value = wsFilter.Range("A2").Value
For Each FilterRange In wsFilter.Range("A3:A" & rowcount)
SheetName = CStr(Left(FilterRange.Text, 31))
If SheetName <> "" Then
wsFilter.Range("B3").Formula = "=" & """=" & SheetName & """"
If Not Evaluate("ISREF('" & SheetName & "'!A2)") Then
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = SheetName
End If
Set wsNames = Worksheets(SheetName)
wsNames.UsedRange.Clear
Datarng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wsFilter.Range("B2:B3"), _
CopyToRange:=wsNames.Range("A1"), Unique:=False
End If
Datarng.Rows(1).Copy
wsNames.UsedRange.Rows(1).PasteSpecial xlPasteColumnWidths
Set wsNames = Nothing
Application.CutCopyMode = False
Next
.Select
End With
End Sub