hi.,
I have column with multiple unique values (which will be dynamic, not standard, as the user might enter different values) like following.
Offwhite - 21091 HGL
Off White - Off White
Offwhite - 10858 SF
10858 SF - 10858 SF
WPC.
ex: finish code1 (off white) - finish code2 (21091 HGL)
finish code2 (21091 HGL) - finish code2 (21091 HGL)
finish code1 (off white) - finish code3 (10858 SF)
finish code3 (10858 SF) - finish code3 (10858 SF)
I would like to filter out as separate ( finishcode1 & finish code2, finish code2 & finish code)
Offwhite - 21091 HGL
Off White - Off White
and the following as separately.
Offwhite - 10858 SF
10858 SF - 10858 SF
and WPC as separately.
The current code filters, copy the filtered value, creates new sheet and paste the values for the each value in the array of values.
How to find solution for the above scenario.
' Segregating the values from rows in seperate sheet with lookup in column with more than 30 Characters
Sub Splitdatabycol()
' declaring the variable for the last row
Dim lr As Long
' declaring the variable for the Source worksheet
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
' declaring the variable for the Top Header
Dim xTRg As Range
' declaring the variable for the Vertical Column
Dim xVRg As Range
Dim xWSTRg As Worksheet
On Error Resume Next
Set xTRg = Application.InputBox("Please select the header rows:", "Excel", "", Type:=8)
If TypeName(xTRg) = "Nothing" Then Exit Sub
' Type:= 8, A cell reference, as a Range object
Set xVRg = Application.InputBox("Please select the column you want to split data based on:", "Excel", "", Type:=8)
If TypeName(xVRg) = "Nothing" Then Exit Sub
'Returns the column no of the selected column
vcol = xVRg.Column
Set ws = xTRg.Worksheet
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = xTRg.AddressLocal
titlerow = xTRg.Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
Application.DisplayAlerts = False
If Not Evaluate("=ISREF('xTRgWs_Sheet!A1')") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = "xTRgWs_Sheet"
Else
Sheets("xTRgWs_Sheet").Delete
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = "xTRgWs_Sheet"
End If
Set xWSTRg = Sheets("xTRgWs_Sheet")
xTRg.Copy
xWSTRg.Paste Destination:=xWSTRg.Range("A1")
ws.Activate
For i = (titlerow + xTRg.Rows.Count) To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
Dim sheetName As String
For i = 2 To UBound(myarr)
'"=*" & myarr(i) & "*", Operator:=xlAnd
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
' Replace the below assignment to sheetName as you wish
sheetName = Left(CStr(i - 1) & "_" & myarr(i), 30)
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = sheetName
Else
Sheets(sheetName).Move after:=Worksheets(Worksheets.Count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(sheetName).Range("A1")
Sheets(sheetName).Columns.AutoFit
Next
xWSTRg.Delete
ws.AutoFilterMode = False
ws.Activate
Application.DisplayAlerts = True
On Error Resume Next
ActiveSheet.ShowAllData
End Sub
Display More