Challenging Macro help

Natalia

Board Regular
Joined
Feb 20, 2009
Messages
72
Hi, i have a work task which is very manual, i was hoping it could be automated.

I need a macro that will look at Col C in sheet "Raw" and look for single string codes before the first "-" if code found then create a tab for that code and import all data for that code to its own worksheet. If no code found then create a worksheet named "INVESTIGATION" and dump all unidentified items to that worksheet.

Note the single code will always be in Upper case, so its easy for the code to scan. Also once the data has imported to its own worksheet, i need the code to insert a row between each unique Comment, but not for sheet "INVESTIGATION". Please see below example.

This is a monthly task, so if new codes are found then the macro needs to create new worksheet for that code.

Excel Workbook
ABC
5MessageFeedDescription
6Deleted1ANXXEAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
7Deleted1GCSXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
8Deleted1GSXXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
9Deleted1OGSXOAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
10DeletedWSTBFAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
11DeletedWTINCAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
12DeletedWTINCADISTAMPBDMP1-Email Me
13DeletedWCTFUNFEAEXPDMP1-Email You
14DeletedWCTFUNPEAEXPDMP1-Email You
15DeletedWCTFUNPECEXPDMP1-Email You
16DeletedWCTFUNPEZEXPDMP1-Email You
17DeletedWEMFUNFE3EXPCITI-Email Me
18DeletedWEMFUNFEAEXPCITI-Email You
19DeletedWEMFUNFEBEXPCITI-Email You
Raw


Excel Workbook
ABC
1MessageFeedDescription
2DeletedWTINCADISTAMPBDMP1-Email Me
3
4DeletedWCTFUNFEAEXPDMP1-Email You
5DeletedWCTFUNPEAEXPDMP1-Email You
6DeletedWCTFUNPECEXPDMP1-Email You
7DeletedWCTFUNPEZEXPDMP1-Email You
DMP1


Excel Workbook
ABC
1MessageFeedDescription
2DeletedWEMFUNFE3EXPCITI-Email Me
3
4DeletedWEMFUNFEAEXPCITI-Email You
5DeletedWEMFUNFEBEXPCITI-Email You
6
CITI


Excel Workbook
ABC
1MessageFeedDescription
2Deleted1ANXXEAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
3Deleted1GCSXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
4Deleted1GSXXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
5Deleted1OGSXOAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
6DeletedWSTBFAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
7DeletedWTINCAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
INVESTIGATION
 
Natalia,

Here you go. I missed "insert a row between each unique Comment".


Sample raw data in worksheet Raw (grouped per your screenshot):


Excel Workbook
ABC
5MessageFeedDescription
6Deleted1ANXXEAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
7Deleted1GCSXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
8Deleted1GSXXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
9Deleted1OGSXOAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
10DeletedWSTBFAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
11DeletedWTINCAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
12DeletedWTINCADISTAMPBDMP1-Email Me
13DeletedWCTFUNFEAEXPDMP1-Email You
14DeletedWCTFUNPEAEXPDMP1-Email You
15DeletedWCTFUNPECEXPDMP1-Email You
16DeletedWCTFUNPEZEXPDMP1-Email You
17DeletedWEMFUNFE3EXPCITI-Email Me
18DeletedWEMFUNFEAEXPCITI-Email You
19DeletedWEMFUNFEBEXPCITI-Email You
20
Raw





After the macro in their respective worksheets:


Excel Workbook
ABC
1MessageFeedDescription
2Deleted1ANXXEAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
3Deleted1GCSXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
4Deleted1GSXXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
5Deleted1OGSXOAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
6DeletedWSTBFAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
7DeletedWTINCAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
8
INVESTIGATION





Excel Workbook
ABC
1MessageFeedDescription
2DeletedWTINCADISTAMPBDMP1-Email Me
3
4DeletedWCTFUNFEAEXPDMP1-Email You
5DeletedWCTFUNPEAEXPDMP1-Email You
6DeletedWCTFUNPECEXPDMP1-Email You
7DeletedWCTFUNPEZEXPDMP1-Email You
8
DMP1





Excel Workbook
ABC
1MessageFeedDescription
2DeletedWEMFUNFE3EXPCITI-Email Me
3
4DeletedWEMFUNFEAEXPCITI-Email You
5DeletedWEMFUNFEBEXPCITI-Email You
6
CITI





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub NataliaV2()
' hiker95, 04/22/2011
' http://www.mrexcel.com/forum/showthread.php?t=545411
Dim wR As Worksheet, wI As Worksheet, ws As Worksheet
Dim c As Range, NR As Long
Dim Sp, H As String, a As Long, b As Long, LR As Long
Application.ScreenUpdating = False
Set wR = Worksheets("Raw")
For Each c In wR.Range("C6", wR.Range("C" & Rows.Count).End(xlUp))
  Sp = Split(c, "-")
  H = Trim(Sp(0))
  If InStr(H, " ") > 0 Then
    If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
      Set wI = Worksheets("INVESTIGATION")
      wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    Else
      Set wI = Worksheets("INVESTIGATION")
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    End If
  Else
    b = 0
    For a = 1 To Len(H) Step 1
      If IsNumeric(Mid(H, a, 1)) = True Then
        'do nothing
      ElseIf Asc(Mid(H, a, 1)) >= 97 And Asc(Mid(H, a, 1)) <= 122 Then
        b = b + 1
      End If
    Next a
    If b = 0 Then
      If Not Evaluate("ISREF(" & H & "!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = H
        Set ws = Worksheets(H)
        ws.Range("A1:C1") = [{"Message","Feed","Description"}]
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      Else
        Set ws = Worksheets(H)
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      End If
    Else
      If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
        Set wI = Worksheets("INVESTIGATION")
        wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      Else
        Set wI = Worksheets("INVESTIGATION")
        NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        wI.UsedRange.Columns.AutoFit
      End If
    End If
  End If
Next c
Set ws = Nothing
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Raw" And ws.Name <> "INVESTIGATION" Then
    LR = ws.Cells(Rows.Count, 1).End(xlUp).Row
    For a = LR To 3 Step -1
      If ws.Cells(a, 3).Value <> ws.Cells(a - 1, 3).Value Then
        ws.Rows(a).Insert
      End If
    Next a
  End If
Next ws
wR.Activate
Application.ScreenUpdating = True
End Sub


Then run the NataliaV2 macro.
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hiker when i run the macro multiple times, i noticed the code keeps adding the same data to the existing sheets which was created by the macro. So would it be better for the code to delete the sheets and re-create again?
 
Upvote 0
Natalia,

Hiker when i run the macro multiple times, i noticed the code keeps adding the same data to the existing sheets which was created by the macro. So would it be better for the code to delete the sheets and re-create again?

Each time you run the macro, you want all the worksheets, if they exist (except Raw) to be deleted first?
 
Upvote 0
Natalia,



Each time you run the macro, you want all the worksheets, if they exist (except Raw) to be deleted first?

Yes that would be the best option. I will be adding extra worksheets which will not be deleted so like "Task" Errors"
 
Upvote 0
Natalia,


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.



Code:
Option Explicit
Sub NataliaV3()
' hiker95, 04/23/2011
' http://www.mrexcel.com/forum/showthread.php?t=545411
Dim wR As Worksheet, wI As Worksheet, ws As Worksheet
Dim c As Range, Sp, H As String
Dim NR As Long, a As Long, b As Long, LR As Long
Application.ScreenUpdating = False
Set wR = Worksheets("Raw")
If Worksheets.Count > 1 Then
  Application.DisplayAlerts = False
  For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Raw" And ws.Name <> "Task" And ws.Name <> "Errors" Then ws.Delete
  Next ws
  Application.DisplayAlerts = True
End If
For Each c In wR.Range("C6", wR.Range("C" & Rows.Count).End(xlUp))
  Sp = Split(c, "-")
  H = Trim(Sp(0))
  If InStr(H, " ") > 0 Then
    If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
      Set wI = Worksheets("INVESTIGATION")
      wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    Else
      Set wI = Worksheets("INVESTIGATION")
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    End If
  Else
    b = 0
    For a = 1 To Len(H) Step 1
      If IsNumeric(Mid(H, a, 1)) = True Then
        'do nothing
      ElseIf Asc(Mid(H, a, 1)) >= 97 And Asc(Mid(H, a, 1)) <= 122 Then
        b = b + 1
      End If
    Next a
    If b = 0 Then
      If Not Evaluate("ISREF(" & H & "!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = H
        Set ws = Worksheets(H)
        ws.Range("A1:C1") = [{"Message","Feed","Description"}]
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      Else
        Set ws = Worksheets(H)
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      End If
    Else
      If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
        Set wI = Worksheets("INVESTIGATION")
        wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      Else
        Set wI = Worksheets("INVESTIGATION")
        NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        wI.UsedRange.Columns.AutoFit
      End If
    End If
  End If
Next c
Set ws = Nothing
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Raw" And ws.Name <> "INVESTIGATION" Then
    LR = ws.Cells(Rows.Count, 1).End(xlUp).Row
    For a = LR To 3 Step -1
      If ws.Cells(a, 3).Value <> ws.Cells(a - 1, 3).Value Then
        ws.Rows(a).Insert
      End If
    Next a
  End If
Next ws
wR.Activate
Application.ScreenUpdating = True
End Sub


Then run the NataliaV3 macro.
 
Upvote 0
Hiker this is great. One more thing, not sure how difficult this is but would it be possible to reflect stats for each team in sheet "Tasks" so i know that it ties back to the raw data and nothing has been missed, i'm just being cautious, something like below

Excel Workbook
ABC
5TeamRawActual
6AMP1331331
7Etc
8Etc
9Etc
10Etc
11Etc
12Etc
13Etc
14Etc
15Etc
16Etc
17Etc
18Etc
19Etc
20Etc
21Etc
22Etc
23Etc
24Total??????
Tasks
 
Upvote 0
Natalia,

One more thing, not sure how difficult this is but would it be possible to reflect stats for each team in sheet "Tasks" so i know that it ties back to the raw data and nothing has been missed, i'm just being cautious, something like below


I think I know where you are going with this last, and final request.


I will need a workbook, with worksheet Raw containing raw data. And, worksheet Tasks manually completed by you, per your last, and final request.

You can upload your workbook to www.box.net and provide us with a link to your workbook.
 
Upvote 0
Hiker, wouldn't the below sample data be sufficient as the data will be changing everymonth, so i assumed if it works on sample data then it should work for all data?

Excel Workbook
ABC
5TeamRawActual
6DMP155
7CITI33
8INVESTIGATIONS66
9
10
Tasks


Excel Workbook
ABC
5MessageFeedDescription
6Deleted1ANXXEAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
7Deleted1GCSXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
8Deleted1GSXXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
9Deleted1OGSXOAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
10DeletedWSTBFAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
11DeletedWTINCAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
12DeletedWTINCADISTAMPBDMP1-Email Me
13DeletedWCTFUNFEAEXPDMP1-Email You
14DeletedWCTFUNPEAEXPDMP1-Email You
15DeletedWCTFUNPECEXPDMP1-Email You
16DeletedWCTFUNPEZEXPDMP1-Email You
17DeletedWEMFUNFE3EXPCITI-Email Me
18DeletedWEMFUNFEAEXPCITI-Email You
19DeletedWEMFUNFEBEXPCITI-Email You
20
Raw
 
Upvote 0
Natalia,

In order for the formulae to work correctly on worksheet Tasks, worksheet INVESTIGATION will have to be created all the time.


Sample raw data in worksheet Raw:


Excel Workbook
ABC
5MessageFeedDescription
6Deleted1ANXXEAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
7Deleted1GCSXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
8Deleted1GSXXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
9Deleted1OGSXOAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
10DeletedWSTBFAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
11DeletedWTINCAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
12DeletedWTINCADISTAMPBDMP1-Email Me
13DeletedWCTFUNFEAEXPDMP1-Email You
14DeletedWCTFUNPEAEXPDMP1-Email You
15DeletedWCTFUNPECEXPDMP1-Email You
16DeletedWCTFUNPEZEXPDMP1-Email You
17DeletedWEMFUNFE3EXPCITI-Email Me
18DeletedWEMFUNFEAEXPCITI-Email You
19DeletedWEMFUNFEBEXPCITI-Email You
20
Raw





After the macro:


Excel Workbook
ABC
1MessageFeedDescription
2Deleted1ANXXEAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
3Deleted1GCSXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
4Deleted1GSXXSAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
5Deleted1OGSXOAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
6DeletedWSTBFAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
7DeletedWTINCAAUDREP150311 Joe Bloggs - there is a separate holdings in these bank accounts
8
INVESTIGATION





Excel Workbook
ABC
1MessageFeedDescription
2DeletedWTINCADISTAMPBDMP1-Email Me
3
4DeletedWCTFUNFEAEXPDMP1-Email You
5DeletedWCTFUNPEAEXPDMP1-Email You
6DeletedWCTFUNPECEXPDMP1-Email You
7DeletedWCTFUNPEZEXPDMP1-Email You
8
DMP1





Excel Workbook
ABC
1MessageFeedDescription
2DeletedWEMFUNFE3EXPCITI-Email Me
3
4DeletedWEMFUNFEAEXPCITI-Email You
5DeletedWEMFUNFEBEXPCITI-Email You
6
CITI





Excel Workbook
ABC
5TeamRawActual
6DMP155
7CITI33
8INVESTIGATION66
9
Tasks





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub NataliaV4()
' hiker95, 04/24/2011
' http://www.mrexcel.com/forum/showthread.php?t=545411
Dim wR As Worksheet, wI As Worksheet, ws As Worksheet, wT As Worksheet
Dim c As Range, Sp, H As String
Dim NR As Long, a As Long, b As Long, LR As Long, LR2 As Long
Application.ScreenUpdating = False
Set wR = Worksheets("Raw")
If Worksheets.Count > 1 Then
  Application.DisplayAlerts = False
  For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Raw" And ws.Name <> "Tasks" And ws.Name <> "Errors" Then ws.Delete
  Next ws
  Application.DisplayAlerts = True
End If
For Each c In wR.Range("C6", wR.Range("C" & Rows.Count).End(xlUp))
  Sp = Split(c, "-")
  H = Trim(Sp(0))
  If InStr(H, " ") > 0 Then
    If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
      Set wI = Worksheets("INVESTIGATION")
      wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    Else
      Set wI = Worksheets("INVESTIGATION")
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    End If
  Else
    b = 0
    For a = 1 To Len(H) Step 1
      If IsNumeric(Mid(H, a, 1)) = True Then
        'do nothing
      ElseIf Asc(Mid(H, a, 1)) >= 97 And Asc(Mid(H, a, 1)) <= 122 Then
        b = b + 1
      End If
    Next a
    If b = 0 Then
      If Not Evaluate("ISREF(" & H & "!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = H
        Set ws = Worksheets(H)
        ws.Range("A1:C1") = [{"Message","Feed","Description"}]
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      Else
        Set ws = Worksheets(H)
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      End If
    Else
      If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
        Set wI = Worksheets("INVESTIGATION")
        wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      Else
        Set wI = Worksheets("INVESTIGATION")
        NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        wI.UsedRange.Columns.AutoFit
      End If
    End If
  End If
Next c
Set ws = Nothing
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Raw" And ws.Name <> "INVESTIGATION" Then
    LR = ws.Cells(Rows.Count, 1).End(xlUp).Row
    For a = LR To 3 Step -1
      If ws.Cells(a, 3).Value <> ws.Cells(a - 1, 3).Value Then
        ws.Rows(a).Insert
      End If
    Next a
  End If
Next ws
If Not Evaluate("ISREF(Tasks!A1)") Then
  Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Tasks"
End If
Set wT = Worksheets("Tasks")
If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
  Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
  Set wI = Worksheets("INVESTIGATION")
  wI.Range("A1:C1") = [{"Message","Feed","Description"}]
  wI.UsedRange.Columns.AutoFit
End If
LR = wT.Cells(Rows.Count, 1).End(xlUp).Row
If LR > 5 Then wT.Range("A6:C" & LR).ClearContents
wT.Range("A5:C5") = [{"Team","Raw","Actual"}]
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Raw" And ws.Name <> "Tasks" And ws.Name <> "INVESTIGATION" Then
    NR = wT.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wT.Range("A" & NR) = ws.Name
  End If
Next ws
wT.Range("A" & NR + 1) = "INVESTIGATION"
LR = wT.Cells(Rows.Count, 1).End(xlUp).Row
For a = 6 To LR Step 1
  wT.Range("C" & a).Formula = "=CountA(" & wT.Range("A" & a).Value & "!A:A)-1"
Next a
For a = 6 To LR - 1 Step 1
  wT.Range("B" & a).FormulaR1C1 = "=COUNTIF(Raw!C[1],RC[-1]&""*"")"
Next a
LR2 = wR.Cells(Rows.Count, 1).End(xlUp).Row
wT.Range("B" & LR).Formula = "=CountA(Raw!A6:A" & LR2 & ")-Sum(B6:B" & LR - 1 & ")"
wT.UsedRange.Columns.AutoFit
wT.Activate
Application.ScreenUpdating = True
End Sub


Then run the NataliaV4 macro.
 
Upvote 0
Natalia,


One last change and we are finished.


After the macro on worksheet Tasks:


Excel Workbook
ABC
5TeamRawActual
6DMP155
7CITI33
8INVESTIGATION66
9Total1414
10
Tasks





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub NataliaV5()
' hiker95, 04/24/2011
' http://www.mrexcel.com/forum/showthread.php?t=545411
Dim wR As Worksheet, wI As Worksheet, ws As Worksheet, wT As Worksheet
Dim c As Range, Sp, H As String
Dim NR As Long, a As Long, b As Long, LR As Long, LR2 As Long
Application.ScreenUpdating = False
Set wR = Worksheets("Raw")
If Worksheets.Count > 1 Then
  Application.DisplayAlerts = False
  For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Raw" And ws.Name <> "Tasks" And ws.Name <> "Errors" Then ws.Delete
  Next ws
  Application.DisplayAlerts = True
End If
For Each c In wR.Range("C6", wR.Range("C" & Rows.Count).End(xlUp))
  Sp = Split(c, "-")
  H = Trim(Sp(0))
  If InStr(H, " ") > 0 Then
    If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
      Set wI = Worksheets("INVESTIGATION")
      wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    Else
      Set wI = Worksheets("INVESTIGATION")
      NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
      wI.UsedRange.Columns.AutoFit
    End If
  Else
    b = 0
    For a = 1 To Len(H) Step 1
      If IsNumeric(Mid(H, a, 1)) = True Then
        'do nothing
      ElseIf Asc(Mid(H, a, 1)) >= 97 And Asc(Mid(H, a, 1)) <= 122 Then
        b = b + 1
      End If
    Next a
    If b = 0 Then
      If Not Evaluate("ISREF(" & H & "!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = H
        Set ws = Worksheets(H)
        ws.Range("A1:C1") = [{"Message","Feed","Description"}]
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      Else
        Set ws = Worksheets(H)
        NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        ws.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        ws.UsedRange.Columns.AutoFit
      End If
    Else
      If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
        Set wI = Worksheets("INVESTIGATION")
        wI.Range("A1:C1") = [{"Message","Feed","Description"}]
      Else
        Set wI = Worksheets("INVESTIGATION")
        NR = wI.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wI.Range("A" & NR).Resize(, 3).Value = c.Offset(, -2).Resize(, 3).Value
        wI.UsedRange.Columns.AutoFit
      End If
    End If
  End If
Next c
Set ws = Nothing
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Raw" And ws.Name <> "INVESTIGATION" Then
    LR = ws.Cells(Rows.Count, 1).End(xlUp).Row
    For a = LR To 3 Step -1
      If ws.Cells(a, 3).Value <> ws.Cells(a - 1, 3).Value Then
        ws.Rows(a).Insert
      End If
    Next a
  End If
Next ws
If Not Evaluate("ISREF(Tasks!A1)") Then
  Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Tasks"
End If
Set wT = Worksheets("Tasks")
If Not Evaluate("ISREF(INVESTIGATION!A1)") Then
  Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "INVESTIGATION"
  Set wI = Worksheets("INVESTIGATION")
  wI.Range("A1:C1") = [{"Message","Feed","Description"}]
  wI.UsedRange.Columns.AutoFit
End If
LR = wT.Cells(Rows.Count, 1).End(xlUp).Row
If LR > 5 Then wT.Range("A6:C" & LR).ClearContents
wT.Range("A5:C5") = [{"Team","Raw","Actual"}]
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Raw" And ws.Name <> "Tasks" And ws.Name <> "INVESTIGATION" Then
    NR = wT.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wT.Range("A" & NR) = ws.Name
  End If
Next ws
wT.Range("A" & NR + 1) = "INVESTIGATION"
LR = wT.Cells(Rows.Count, 1).End(xlUp).Row
For a = 6 To LR Step 1
  wT.Range("C" & a).Formula = "=CountA(" & wT.Range("A" & a).Value & "!C)-1"
Next a
For a = 6 To LR - 1 Step 1
  wT.Range("B" & a).FormulaR1C1 = "=COUNTIF(Raw!C[1],RC[-1]&""*"")"
Next a
LR2 = wR.Cells(Rows.Count, 3).End(xlUp).Row
wT.Range("B" & LR).Formula = "=CountA(Raw!C6:C" & LR2 & ")-Sum(B6:B" & LR - 1 & ")"
LR = wT.Cells(Rows.Count, 1).End(xlUp).Row
wT.Range("A" & LR + 1) = "Total"
wT.Range("B" & LR + 1).Formula = "=Sum(B6:B" & LR & ")"
wT.Range("C" & LR + 1).Formula = "=Sum(C6:C" & LR & ")"
wT.Range("A" & LR + 1 & ":C" & LR + 1).Font.Bold = True
wT.UsedRange.Columns.AutoFit
wT.Activate
Application.ScreenUpdating = True
End Sub


Then run the NataliaV5 macro.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top