Shweta
Well-known Member
- Joined
- Jun 5, 2011
- Messages
- 514
Hi All,
Here is my query:
Table 1(DATA)
[TABLE="width: 313"]
<colgroup><col><col span="2"></colgroup><tbody>[TR]
[TD]Departments[/TD]
[TD][/TD]
[TD]Sub Cat.[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Oxygen[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Carbon[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Hydrozen[/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD]Labour Law[/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD]Civil Law[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Co2[/TD]
[/TR]
</tbody>[/TABLE]
Table2(OUTPUT)
[TABLE="width: 422"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Departments[/TD]
[TD][/TD]
[TD]Sub Cat.[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Oxygen,Carbon,Hydrogen,Co2[/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD]Labour Law, Civil Law[/TD]
[/TR]
</tbody>[/TABLE]
I have written the following code for it in module
Sub unique()
Dim arr, distinct() As String
Dim clctn As New Collection
Dim i As Integer
Dim sht As Worksheet
Set sht = Worksheets("Sh-6")
Range("I1") = "Departments"
arr = Application.Transpose(sht.Cells(2, 6).CurrentRegion.Resize(, 1).Value)
On Error Resume Next
For i = LBound(arr) To UBound(arr)
clctn.Add arr(i), arr(i)
Next
On Error GoTo 0
ReDim distinct(1 To clctn.Count)
For i = 1 To clctn.Count
distinct(i) = clctn(i)
Next
sht.Cells(1, 9).Resize(clctn.Count).Value = Application.Transpose(distinct)
Range("J1") = "Sub Category"
End Sub
It is giving me the following output
[TABLE="width: 446"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Departments[/TD]
[TD][/TD]
[TD]Sub Category[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
For another part of the query I have written a function and it is working fine
Function consol_data(x As String, y As Range, z As Integer)
Dim pan As String
Dim acell As Range
For Each acell In y
If acell.Value = x Then
pan = pan & "," & acell.Offset(0, z).Value
End If
Next
consol_data = Right(pan, Len(pan) - 1)
End Function
But the problem is I want to use a single macro to do this.
For this purpose I need to call this function in the above code but I dont know how to do this.
Please help me out on this.
Thanks!
Regards,
Shweta
Here is my query:
Table 1(DATA)
[TABLE="width: 313"]
<colgroup><col><col span="2"></colgroup><tbody>[TR]
[TD]Departments[/TD]
[TD][/TD]
[TD]Sub Cat.[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Oxygen[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Carbon[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Hydrozen[/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD]Labour Law[/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD]Civil Law[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Co2[/TD]
[/TR]
</tbody>[/TABLE]
Table2(OUTPUT)
[TABLE="width: 422"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Departments[/TD]
[TD][/TD]
[TD]Sub Cat.[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD]Oxygen,Carbon,Hydrogen,Co2[/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD]Labour Law, Civil Law[/TD]
[/TR]
</tbody>[/TABLE]
I have written the following code for it in module
Sub unique()
Dim arr, distinct() As String
Dim clctn As New Collection
Dim i As Integer
Dim sht As Worksheet
Set sht = Worksheets("Sh-6")
Range("I1") = "Departments"
arr = Application.Transpose(sht.Cells(2, 6).CurrentRegion.Resize(, 1).Value)
On Error Resume Next
For i = LBound(arr) To UBound(arr)
clctn.Add arr(i), arr(i)
Next
On Error GoTo 0
ReDim distinct(1 To clctn.Count)
For i = 1 To clctn.Count
distinct(i) = clctn(i)
Next
sht.Cells(1, 9).Resize(clctn.Count).Value = Application.Transpose(distinct)
Range("J1") = "Sub Category"
End Sub
It is giving me the following output
[TABLE="width: 446"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Departments[/TD]
[TD][/TD]
[TD]Sub Category[/TD]
[/TR]
[TR]
[TD]Department of Chemical[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Department of Law[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
For another part of the query I have written a function and it is working fine
Function consol_data(x As String, y As Range, z As Integer)
Dim pan As String
Dim acell As Range
For Each acell In y
If acell.Value = x Then
pan = pan & "," & acell.Offset(0, z).Value
End If
Next
consol_data = Right(pan, Len(pan) - 1)
End Function
But the problem is I want to use a single macro to do this.
For this purpose I need to call this function in the above code but I dont know how to do this.
Please help me out on this.
Thanks!
Regards,
Shweta