Copie

gnusmas

Board Regular
Joined
Mar 5, 2014
Messages
186
Hi friend i have Sheet1 with group of numbers i need vba code to copie just group if last cell have 15

Exemple:

101741
051119
011922
111215
010718
060822
121314
062526
192122
071415
030921
051022

<tbody>
</tbody>
Sheet2

111215
010718
060822
071415
030921
051022

<tbody>
</tbody>
Thank you for help
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi,

I assumed layout is the same as on your sample screen:

Code:
Sub CopyGroup15()


Dim Rng As Range
Dim i As Long, X As Long, Y As Long


Application.ScreenUpdating = False


Set Rng = ThisWorkbook.Sheets("Sheet1").UsedRange
Sheets("Sheet2").Cells.Clear 'delete cells on "Sheet2"


Y = 4
X = 1
For i = 1 To Rng.Rows.Count Step Y
    If Rng.Cells(i, 3) = 15 Then
        Sheets("Sheet1").Range(Rng.Cells(i, 1).Address, Rng.Cells(i + 2, 3).Address).Copy _
        Sheets("Sheet2").Cells(X, 1)
        X = X + Y
    End If
Next i


Application.ScreenUpdating = True


End Sub

I hope it'll work for you.

Kind Regards,
 
Upvote 0
thank you friend i have just problem when 15 is repeted like this!!

1112 15
01
07
15
06
08
22

<colgroup><col span="3"></colgroup><tbody>
</tbody>
as you see is just one group!! i need just copie each group when 15 on head and last cell!
 
Upvote 0
gnusmas,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?

If I understand you correctly, here is another macro for you to consider, that will search each group in Sheet1 column C for the value 15.

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:
Sub CopyGroups15()
' hiker95, 03/28/2015, ME844183
Dim w1 As Worksheet, w2 As Worksheet
Dim Area As Range, sr As Long, er As Long, nr As Long, c As Range
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
w2.Columns("A:C").ClearContents
nr = 1
With w1
  For Each Area In w1.Range("C1", w1.Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
    With Area
      sr = .Row
      er = sr + .Rows.Count - 1
      Set c = w1.Range("C" & sr & ":C" & er).Find(15, LookAt:=xlWhole)
      If Not c Is Nothing Then
        w2.Cells(nr, 1).Resize(3, 3).Value = w1.Range("A" & sr & ":C" & er).Value
        nr = nr + 4
      End If
    End With
  Next Area
End With
With w2
  .Columns("A:C").AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the CopyGroups15 macro.
 
Upvote 0
Thank you hicker95 great job i am using excel 2007
i have another Sheet 1 with the following groups separate with one row i need just extract each group with last cell 49
182126344149
030608293738
242530313343
080932344648
132430323346
172426303237

<tbody>
</tbody>

141921313245
132137444547
010509333540
010713142032
092227353946
020408303844

<tbody>
</tbody>

141827333842
030419344349
092933394345
091316343637
061527283446
020416232630

<tbody>
</tbody>

031116283949
091029414647
061322384145
042429323443
010307324146
101113151830

<tbody>
</tbody>

102324283542
030421344243
020918364449
232529313546
020913264046
143031343848

<tbody>
</tbody>

Sheet2

182126344149
030608293738
242530313343
080932344648
132430323346
172426303237

<tbody>
</tbody>

031116283949
091029414647
061322384145
042429323443
010307324146
101113151830

<tbody>
</tbody>

thank you for help!
 
Upvote 0
gnusmas,

Thank you hicker95 great job

Thanks for the feedback.

You are very welcome. Glad I could help.

i have another Sheet 1 with the following groups separate with one row i need just extract each group with last cell 49

Be back in a little while.
 
Upvote 0
gnusmas,

Here is a new macro solution, based on your new data structure, for you to consider.

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:
Sub CopyGroups49()
' hiker95, 03/28/2015, ME844183
Dim w1 As Worksheet, w2 As Worksheet
Dim Area As Range, sr As Long, er As Long, nr As Long, n As Range
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
w2.Columns("A:F").ClearContents
nr = 1
With w1
  For Each Area In w1.Range("F1", w1.Range("F" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
    With Area
      sr = .Row
      er = sr + .Rows.Count - 1
      Set n = w1.Range("F" & sr & ":F" & er).Find(49, LookAt:=xlWhole)
      If Not n Is Nothing Then
        w2.Cells(nr, 1).Resize(6, 6).Value = w1.Range("A" & sr & ":F" & er).Value
        nr = nr + 7
      End If
    End With
  Next Area
End With
With w2
  .Columns("A:F").AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the CopyGroups49 macro.
 
Upvote 0
Hi, hicker95 i have run your code i get all groups like this

182126344149
030608293738
242530313343
080932344648
132430323346
172426303237
102324283542
030421344243
020918364449
232529313546
020913264046
143031343848
141827333842
030419344349
092933394345
091316343637
061527283446
020416232630
031116283949
091029414647
061322384145
042429323443
010307324146
101113151830

<tbody>
</tbody>
not just 49 in first row!!
 
Upvote 0
gnusmas,

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:
Sub CopyGroups49infirstrow()
' hiker95, 03/28/2015, ME844183
Dim w1 As Worksheet, w2 As Worksheet
Dim Area As Range, sr As Long, er As Long, nr As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
w2.Columns("A:F").ClearContents
nr = 1
With w1
  For Each Area In w1.Range("F1", w1.Range("F" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
    With Area
      sr = .Row
      er = sr + .Rows.Count - 1
      If w1.Range("F" & sr) = 49 Then
        w2.Cells(nr, 1).Resize(.Rows.Count, 6).Value = w1.Range("A" & sr & ":F" & er).Value
        nr = nr + 7
      End If
    End With
  Next Area
End With
With w2
  .Columns("A:F").AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Ten run the CopyGroups49infirstrow macro.
 
Upvote 0
Hi,

I believe my code worked correctly (it handled array as one group if top right value was equal to 15). Now I changed my code in line with your new requirements. It'll work if you have two empty rows between groups. If you have other than two empty rows between groups please let me know and I'll update my code.

Code:
Sub CopyGroup15_revised()


Dim Rng As Range
Dim i As Long, X As Long, Y As Long


Application.ScreenUpdating = False


Set Rng = ThisWorkbook.Sheets("Sheet1").UsedRange
Sheets("Sheet2").Cells.Clear 'delete cells on "Sheet2"


Y = 8
X = 1
For i = 1 To Rng.Rows.Count Step Y
    If Rng.Cells(i, 6) = 49 Then
        Sheets("Sheet1").Range(Rng.Cells(i, 1).Address, Rng.Cells(i + 5, 6).Address).Copy _
        Sheets("Sheet2").Cells(X, 1)
        X = X + Y
    End If
Next i


Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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