aaumdamm
New Member
- Joined
- Mar 18, 2024
- Messages
- 1
- Office Version
- 365
- 2016
- 2013
- Platform
- Windows
- Mobile
- Web
Hello All,
I am studying Excel's newer versions, especially the VBA code on which I used to work a while back. A need has come up again to write a small VBA. I was able to find the references but those ones are giving me the solution that splits the data and copies it into multiple worksheets. My requirement is to split the data in a given worksheet by a filter and then paste all of them to another single worksheet only with the header for each section.
As an example, I have a worksheet (DataSource) with the table in cells A4:D100. A1:D2 have the other header information while A3:D3 has the table headers. I would like to filter by column D and paste the output of the filtered data along with the cell format/width from this DataSource to a new worksheet (DataTarget). Each section to be copied to DataTarget sheet will have the data filtered in column D along with the 3 header rows (A1:D3) above each section. I have this reference code from HippieHacker from quite a while back, which I was able to reference and it works well. But instead of pasting each section with the header to "Multiple worksheets", I need to paste it in the same DataTarget worksheet and I am struggling to get it up and running.
Appreciate any help and guidance.
I am studying Excel's newer versions, especially the VBA code on which I used to work a while back. A need has come up again to write a small VBA. I was able to find the references but those ones are giving me the solution that splits the data and copies it into multiple worksheets. My requirement is to split the data in a given worksheet by a filter and then paste all of them to another single worksheet only with the header for each section.
As an example, I have a worksheet (DataSource) with the table in cells A4:D100. A1:D2 have the other header information while A3:D3 has the table headers. I would like to filter by column D and paste the output of the filtered data along with the cell format/width from this DataSource to a new worksheet (DataTarget). Each section to be copied to DataTarget sheet will have the data filtered in column D along with the 3 header rows (A1:D3) above each section. I have this reference code from HippieHacker from quite a while back, which I was able to reference and it works well. But instead of pasting each section with the header to "Multiple worksheets", I need to paste it in the same DataTarget worksheet and I am struggling to get it up and running.
Appreciate any help and guidance.
VBA Code:
Sub DataSplitWithHeader()
Set asheet = ActiveSheet
lastrow = asheet.Range("D" & Rows.Count).End(xlUp).Row
myarray = uniqueValues(asheet.Range("D4:D" & lastrow))
For i = LBound(myarray) To UBound(myarray)
Sheets.Add.Name = myarray(i)
asheet.Range("A3:D" & lastrow).AutoFilter Field:=24, Criteria1:=myarray(i)
asheet.Range("A1:D" & lastrow).SpecialCells(xlCellTypeVisible).Copy _
Sheets(myarray(i)).Range("A1")
asheet.Range("A3:D" & lastrow).AutoFilter
Next i
End Sub
Private Function uniqueValues(InputRange As Range)
Dim cell As Range
Dim tempList As Variant: tempList = ""
For Each cell In InputRange
If cell.Value <> "" Then
If InStr(1, tempList, cell.Value) = 0 Then
If tempList = "" Then tempList = Trim(CStr(cell.Value)) Else tempList = tempList & "|" & Trim(CStr(cell.Value))
End If
End If
Next cell
uniqueValues = Split(tempList, "|")
End Function