Question : Copy data from sheets according to sub header.

Hi Experts,

I need Experts help create a macro to copy data from sheet Ch1-Ch7 to “Complaints”, “Internal Issues”, “External Issues” and “Remark” sheets based on sub header in “CH-1 to CH7” sheets.

I’ve manually copied few data in the sheets for Experts perusal. Hope Experts can help me to create this macro. Attached the workbook for experts perusal.



Attachments:
 
 

Answer : Copy data from sheets according to sub header.

Hi Theva,

try this one.

Kris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
Option Explicit

Sub CompileData()
Dim rngToCompile    As Range
Dim rngArea         As Range
Dim i               As Long
Dim wksName         As String
Dim r               As Long
Dim MySheets
Dim wksCompile      As Worksheet
Dim rCell           As Range

Set wksCompile = Worksheets("Compilation")

MySheets = Array("CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7") '< adjust sheet names here

Application.ScreenUpdating = 0
For i = LBound(MySheets) To UBound(MySheets)
    With Sheets(CStr(MySheets(i)))
        On Error Resume Next
        Set rngToCompile = .Columns(2).SpecialCells(2, 1)
        On Error GoTo 0
        If Not rngToCompile Is Nothing Then
            For Each rngArea In rngToCompile.Areas
                If Len(rngArea.Cells(1).Offset(-1, 1)) Then
                    wksName = rngArea.Cells(1).Offset(-1, 1)
                Else
                    wksName = rngArea.Cells(1).Offset(-1, -1)
                End If
                For Each rCell In rngArea
                    If ISEXIST(Sheets(CStr(wksName)).UsedRange.Columns("a:b").Value, MySheets(i) & ";" & CLng(rCell.Value)) Then GoTo Nxt
                Next
                rngArea.Offset(, -1).Resize(, 3).Copy
                With Sheets(CStr(wksName))
                    r = .Range("a" & .Rows.Count).End(xlUp).Offset(1).Row
                    .Range("a" & r).PasteSpecial -4104
                    .Range("a" & r).Resize(rngArea.Rows.Count).Value = MySheets(i)
                End With
                With Sheets("Compilation")
                    r = .Range("a" & .Rows.Count).End(xlUp).Offset(1).Row
                    .Range("a" & r).Value = MySheets(i)
                    .Range("a" & r).Offset(1).PasteSpecial -4104
                End With
Nxt:
            Next
            Set rngToCompile = Nothing
        End If
    End With
Next
Xit:
Set wksCompile = Nothing
Set rngToCompile = Nothing
With Application
    .ScreenUpdating = 1
    .CutCopyMode = 0
End With
End Sub
Function ISEXIST(ByRef varData As Variant, ByVal strConcated As String) As Boolean
Dim strConcat As String, i As Long

ISEXIST = False
For i = 1 To UBound(varData, 1)
    On Error Resume Next
    strConcat = varData(i, 1) & ";" & CLng(varData(i, 2))
    On Error GoTo 0
    If LCase$(strConcat) = LCase$(strConcated) Then
        ISEXIST = True
        Exit Function
    End If
Next
End Function
Random Solutions  
 
programming4us programming4us