VBA code to copy data matching criteria to new sheet and copy specific rows in between

vbanewbie2011

New Member
Joined
Sep 6, 2011
Messages
5
Hello! I am relatively new to VBA and have been searching for a few weeks for a code that will do the following:

I need to copy all data in a row from a "Master" sheet to individual sheets (in the same workbook) where the ID on the Master sheet match the individual sheet names while also copying the row titles/headings. My problem is on the Master sheet, the first 6 rows are "section titles", as are rows 17-19 and rows 96-98. Rows 7, 20, & 99 are "headers".

For example: There is one instance of ID "11" under Section 1 (cell A8), three instances under Section 2 (cells A21, A22, & A23), and one instance under Section 3 (cell A100). On the sheet named "11", I need each Section heading with the corresponding data shown below each.

For instances where an ID may not appear under all 3 sections, I would like the text "Section Not Applicable" to appear below the section title.

I have an example file that may provide a better visual- let me know how to send if you'd like the file. Any help would be GREATLY appreciated!!! Thank you!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
vbanewbie2011,

Welcome to the MrExcel forum.

What version of Excel are you using?

You will generally get much more help (and faster) in this forum if you can post your small samples (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:

If you are not able to give us screenshots, see below in my Signature block: You can upload your workbook to Box Net, and provide us with a link to your workbook.
 
Upvote 0
I apologize for not including which version I'm using... I can't believe I did that! :) I'm using Excel 2003. I have loaded the example file to Box Net; here is the link: http://www.box.net/shared/egkkbt42274a1jh2fna8

In the example file, the Master page is what I currently have; the other 3 pages are what I would like to be able to do with a VBA code. The example has 3 sheets for separate ID's. However, there are actually 38 unique ID's. I already have a code that will create a separate sheet for each unique ID number.

What I'm hoping someone here can help me with is a code that will copy the section titles and headers from the master to each sheet while also copying the data in each row where the ID number matches the sheet name and keeps it under the correct section title. An added bonus would be if an ID number is not present in one of the sections, the text "Section Not Applicable" would be listed below the section title.

I hope this helps clarify what I'm looking for, but if not, please let me know. Thank you so much!
 
Upvote 0
vbanewbie2011,

Thanks for the workbook.

Very interesting problem.

Working on it.

Will have some questions for you later today.
 
Upvote 0
vbanewbie2011,


The macro will work correctly as long as the spacing between the 3 groups is constant.

The macro will create the worksheets for the ID's, as they are processed from Section 1 To Section 3.


Sample raw data in worksheet Master:


Excel Workbook
ABCDEFGHIJKL
12011 Data
2Report
31st Qtr vs. 2nd Qtr
4
5Section 1: Major Change
6
7IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
811R 24191SMITH J1234ABC78927000045000201011Test 1
946R 1212DOE J1234ABC7892700004400020108Test 2
1054N 59385BLACK M1234ABC78927000064500020105Test 3
1162R 90381TAYLOR R1234ABC78927000052000201012Test 4
1263N 4198BROWN D1234ABC789270000350020106Test 5
1373N 17415WILLIAMS M1234ABC78927000028500201012Test 6
1474R 57112HARRIS G1234ABC78927000047500201012Test 7
15
16
17
18Section 2: Minor Change
19
20IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
2111R 26130SMITH J987ZYXM44589500002500020102Test 8
2211R 27140SMITH J987ZYXM44589500006900020108Test 9
2311N 11991SMITH J987ZYXM445895000035000020109Test 10
2416N 2130SMITH J987ZYXM4458950000110000020103Test 11
2518R 22137SMITH J987ZYXM4458950000680020101Test 12
Master





Excel Workbook
ABCDEFGHIJKL
8974R 19237BROWN D987ZYXM44589500004500020102Test 76
9074R 57112BROWN D987ZYXM445895000047500201012Test 77
9174R 79116BROWN D987ZYXM44589500009499820109Test 78
9274R 96212BROWN D987ZYXM44589500007240020108Test 79
9374R 97112BROWN D987ZYXM44589500003200020102Test 80
94
95
96
97Section 3: Supposed to Change but Didn't
98
99IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
10011R 313DOE J632589WFTG554530001500020102Test 81Reason 1
10116R 255DOE J632589WFTG56453001450000020107Test 82Reason 2
10218R 2133DOE J632589WFTG574530027700020104Test 83Reason 3
10321N 1522DOE J632589WFTG584530037750020105Test 84Reason 4
Master





After the macro:


Excel Workbook
ABCDEFGHIJKL
12011 Data
2Report
31st Qtr vs. 2nd Qtr
4
5Section 1: Major Change
6
7IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
811R 24191SMITH J1234ABC78927000045000201011Test 1
9
10Section 2: Minor Change
11
12IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
1311R 26130SMITH J987ZYXM44589500002500020102Test 8
1411R 27140SMITH J987ZYXM44589500006900020108Test 9
1511N 11991SMITH J987ZYXM445895000035000020109Test 10
16
17Section 3: Supposed to Change but Didn't
18
19IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
2011R 313DOE J632589WFTG554530001500020102Test 81Reason 1
21
11





Excel Workbook
ABCDEFGHIJKL
12011 Data
2Report
31st Qtr vs. 2nd Qtr
4
5Section 1: Major Change
6
7IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
8Section Not Applicable
9
10Section 2: Minor Change
11
12IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
1318R 22137SMITH J987ZYXM4458950000680020101Test 12
1418R 43137SMITH J987ZYXM4458950000209300201010Test 13
1518R 49137SMITH J987ZYXM4458950000345000201012Test 14
1618R 64137SMITH J987ZYXM4458950000750020103Test 15
1718R 78115SMITH J987ZYXM445895000024500020109Test 16
1818N 8137SMITH J987ZYXM445895000014500020104Test 17
19
20Section 3: Supposed to Change but Didn't
21
22IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
2318R 2133DOE J632589WFTG574530027700020104Test 83Reason 3
24
18






Excel Workbook
ABCDEFGHIJKL
12011 Data
2Report
31st Qtr vs. 2nd Qtr
4
5Section 1: Major Change
6
7IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
874R 57112HARRIS G1234ABC78927000047500201012Test 7
9
10Section 2: Minor Change
11
12IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
1374R 19237BROWN D987ZYXM44589500004500020102Test 76
1474R 57112BROWN D987ZYXM445895000047500201012Test 77
1574R 79116BROWN D987ZYXM44589500009499820109Test 78
1674R 96212BROWN D987ZYXM44589500007240020108Test 79
1774R 97112BROWN D987ZYXM44589500003200020102Test 80
18
19
20
21
22
23
24
25
74





Ooops.... Be back in a little while to add some code to check each worksheet for the three Sections.
 
Upvote 0
vbanewbie2011,


After the macro update:


Excel Workbook
ABCDEFGHIJKL
12011 Data
2Report
31st Qtr vs. 2nd Qtr
4
5Section 1: Major Change
6
7IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
874R 57112HARRIS G1234ABC78927000047500201012Test 7
9
10Section 2: Minor Change
11
12IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
1374R 19237BROWN D987ZYXM44589500004500020102Test 76
1474R 57112BROWN D987ZYXM445895000047500201012Test 77
1574R 79116BROWN D987ZYXM44589500009499820109Test 78
1674R 96212BROWN D987ZYXM44589500007240020108Test 79
1774R 97112BROWN D987ZYXM44589500003200020102Test 80
18
19Section 3: Supposed to Change but Didn't
20
21IDRefOriginal CodeChange CodeNameFull #Original AmtChange AmtYearMonthMajor Change Reason
22Section Not Applicable
23
24
25
74





With your sample raw data, there were only 37 unique ID's, and there were 37 worksheets created.


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 DistributeData()
' hiker95, 09/08/2011
' http://www.mrexcel.com/forum/showthread.php?t=577018
Dim wM As Worksheet, ws As Worksheet
Dim MyS1 As Long, MyS2 As Long, MyS3 As Long, LR As Long, LUR As Long
Dim MyC1 As Long, MyC2 As Long, MyC3 As Long
Dim TR As Long, FR As Long, ER As Long, a As Long, NR As Long, N As String
Application.ScreenUpdating = False
Set wM = Worksheets("Master")
MyS1 = Application.Match("Section 1: Major Change", wM.Columns(1), 0)
MyS2 = Application.Match("Section 2: Minor Change", wM.Columns(1), 0)
MyS3 = Application.Match("Section 3: Supposed to Change but Didn't", wM.Columns(1), 0)
LR = wM.Cells(Rows.Count, 1).End(xlUp).Row
'********** Section 1: Major Change **********
TR = MyS1 + 2
FR = MyS1 + 3
ER = MyS2 - 4
For a = FR To ER Step 1
  If Not Evaluate("ISREF(" & wM.Cells(a, 1) & "!A1)") Then Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = wM.Cells(a, 1)
  N = wM.Cells(a, 1)
  Set ws = Worksheets(N)
  MyC1 = 0
  On Error Resume Next
  MyC1 = Application.Match("Section 1: Major Change", ws.Columns(1), 0)
  On Error GoTo 0
  If MyC1 = 0 Then
    ws.Cells.Interior.ColorIndex = 2
    wM.Range("A1:L7").Copy ws.Range("A1:L7")
    wM.Range("A1:L1").Copy
    With ws.Range("A1:L1")
      .PasteSpecial Paste:=xlPasteColumnWidths
    End With
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wM.Range("A" & a & ":M" & a).Copy ws.Range("A" & NR)
  Else
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wM.Range("A" & a & ":M" & a).Copy ws.Range("A" & NR)
  End If
Next a
'********** Section 2: Minor Change **********
TR = MyS2 + 2
FR = MyS2 + 3
ER = MyS3 - 4
For a = FR To ER Step 1
  If Not Evaluate("ISREF(" & wM.Cells(a, 1) & "!A1)") Then Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = wM.Cells(a, 1)
  N = wM.Cells(a, 1)
  Set ws = Worksheets(N)
  MyC1 = 0
  On Error Resume Next
  MyC1 = Application.Match("Section 1: Major Change", ws.Columns(1), 0)
  On Error GoTo 0
  If MyC1 = 0 Then
    ws.Cells.Interior.ColorIndex = 2
    wM.Range("A1:L7").Copy ws.Range("A1:L7")
    wM.Range("A1:L1").Copy
    With ws.Range("A1:L1")
      .PasteSpecial Paste:=xlPasteColumnWidths
    End With
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    ws.Range("A" & NR) = "Section Not Applicable"
    With ws.Range("A" & NR & ":L" & NR)
      .HorizontalAlignment = xlCenter
      .MergeCells = True
      .Interior.ColorIndex = 2
    End With
  End If
  MyC2 = 0
  On Error Resume Next
  MyC2 = Application.Match("Section 2: Minor Change", ws.Columns(1), 0)
  On Error GoTo 0
  If MyC2 = 0 Then
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(2).Row
    wM.Range("A" & MyS2 & ":L" & MyS2 + 2).Copy ws.Range("A" & NR)
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wM.Range("A" & a & ":M" & a).Copy ws.Range("A" & NR)
  Else
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wM.Range("A" & a & ":M" & a).Copy ws.Range("A" & NR)
  End If
Next a
'********** Section 3: Supposed to Change but Didn't *********
TR = MyS3 + 2
FR = MyS3 + 3
ER = LR
For a = FR To ER Step 1
  If Not Evaluate("ISREF(" & wM.Cells(a, 1) & "!A1)") Then Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = wM.Cells(a, 1)
  N = wM.Cells(a, 1)
  Set ws = Worksheets(N)
  MyC1 = 0
  On Error Resume Next
  MyC1 = Application.Match("Section 1: Major Change", ws.Columns(1), 0)
  On Error GoTo 0
  If MyC1 = 0 Then
    ws.Cells.Interior.ColorIndex = 2
    wM.Range("A1:L7").Copy ws.Range("A1:L7")
    wM.Range("A1:L1").Copy
    With ws.Range("A1:L1")
      .PasteSpecial Paste:=xlPasteColumnWidths
    End With
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    ws.Range("A" & NR) = "Section Not Applicable"
    With ws.Range("A" & NR & ":L" & NR)
      .HorizontalAlignment = xlCenter
      .MergeCells = True
      .Interior.ColorIndex = 2
    End With
  End If
  MyC2 = 0
  On Error Resume Next
  MyC2 = Application.Match("Section 2: Minor Change", ws.Columns(1), 0)
  On Error GoTo 0
  If MyC2 = 0 Then
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(2).Row
    wM.Range("A" & MyS2 & ":L" & MyS2 + 2).Copy ws.Range("A" & NR)
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    ws.Range("A" & NR) = "Section Not Applicable"
    With ws.Range("A" & NR & ":L" & NR)
      .HorizontalAlignment = xlCenter
      .MergeCells = True
      .Interior.ColorIndex = 2
    End With
  End If
  MyC3 = 0
  On Error Resume Next
  MyC3 = Application.Match("Section 3: Supposed to Change but Didn't", ws.Columns(1), 0)
  On Error GoTo 0
  If MyC3 = 0 Then
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(2).Row
    wM.Range("A" & MyS3 & ":L" & MyS3 + 2).Copy ws.Range("A" & NR)
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wM.Range("A" & a & ":M" & a).Copy ws.Range("A" & NR)
  Else
    NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    wM.Range("A" & a & ":M" & a).Copy ws.Range("A" & NR)
  End If
Next a
'********** Check each worksheet for "Section 3: Supposed to Change but Didn't" **********
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Instructions" And ws.Name <> "Master" Then
    MyC3 = 0
    On Error Resume Next
    MyC3 = Application.Match("Section 3: Supposed to Change but Didn't", ws.Columns(1), 0)
    On Error GoTo 0
    If MyC3 = 0 Then
      NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(2).Row
      wM.Range("A" & MyS3 & ":L" & MyS3 + 2).Copy ws.Range("A" & NR)
      NR = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      ws.Range("A" & NR) = "Section Not Applicable"
      With ws.Range("A" & NR & ":L" & NR)
        .HorizontalAlignment = xlCenter
        .MergeCells = True
        .Interior.ColorIndex = 2
      End With
    End If
  End If
Next ws
wM.Activate
Application.ScreenUpdating = True
End Sub


Then run the DistributeData macro.
 
Upvote 0
hiker95~

Thank you so much for your time and effort on this code! I'm going to test out now; I'll let you know how it works.
 
Upvote 0
hiker95,

You are a GENIUS!!! The code works exactly the way I'd hoped!!!

I've played around with the Master sheet, adding rows in each section with different ID numbers to see if the code would still work... it did! The only thing I noticed when adding rows was this:

3 new rows added in Section 1 with ID 13. The code created a sheet named 13 and copied everything like it was supposed to with the exception of the Section 2 title and header.

I went back and added 1 row in Section 2 with ID 13, and it worked just like the original- everything showed up. I also tried adding a single row in Section 2 (ID 14) only and a single row in Section 3 (ID 10) only, each with a different ID; the code worked perfectly on these as well. I went back and tried Section 1 again, adding a single row with ID 85 further down the list. Same problem as with ID 13 when I added the rows in Section 1 only.

Any idea what might cause the Section 2 title & header to not show when a row is added in Section 1? I've uploaded the file to Box Net: http://www.box.net/shared/qlj9z2otog4kqdsv0dt2

Again, I can't thank you enough for all your help on this!
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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