copy paste specific content

excelnoobhere

Board Regular
Joined
Mar 11, 2019
Messages
61
I'm looking for a code that will copy certain columns and paste them into a new sheets that will be created based on cell value.
for example lets say I have a huge list of data in cell A contain
A
5555-01-120-121
5555-01-120-123
5555-02-120-255
5555-05-150-220

I would like to be able to search through all of an and if A contain -01- then that gets copied that row's columns (A,B,O) into a new sheet columns (A,B,C) titled -01-, and if it contains -02- then creates a new sheet and does the same and so on for -05-

basically I want to split these data based on the value I have in red

how can I go about this?

any help would be much appreciated
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
How about
Code:
Sub excelnoobhere()
   Dim Cl As Range
   Dim Uniq As String
   Dim Ky As Variant
   Dim Ws As Worksheet
   
   Set Ws = Sheets("[COLOR=#ff0000]All[/COLOR]")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
         Uniq = Mid(Cl, 5, 4)
         .Item(Uniq) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Range("A1:O1").AutoFilter 1, "*" & Ky & "*"
         Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
         Intersect(Ws.AutoFilter.Range.EntireRow, Ws.Range("A:B,O:O")).Copy Range("A1")
      Next Ky
      Ws.AutoFilterMode = False
   End With
End Sub
Change sheet name in red to suit
 
Upvote 0
How about
Code:
Sub excelnoobhere()
   Dim Cl As Range
   Dim Uniq As String
   Dim Ky As Variant
   Dim Ws As Worksheet
   
   Set Ws = Sheets("[COLOR=#ff0000]All[/COLOR]")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
         Uniq = Mid(Cl, 5, 4)
         .Item(Uniq) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Range("A1:O1").AutoFilter 1, "*" & Ky & "*"
         Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
         Intersect(Ws.AutoFilter.Range.EntireRow, Ws.Range("A:B,O:O")).Copy Range("A1")
      Next Ky
      Ws.AutoFilterMode = False
   End With
End Sub
Change sheet name in red to suit


i get an error when running

run-time error '9'

subscribe out of range

my code....
' Macro1 Macro
'


Sub CopyCode()


Dim Cl As Range
Dim Uniq As String
Dim Ky As Variant
Dim Ws As Worksheet

Set Ws = Sheets("MASTER")
With CreateObject("scripting.dictionary")
For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
Uniq = Mid(Cl, 5, 4)
.Item(Uniq) = Empty
Next Cl
For Each Ky In .Keys
Ws.Range("A1:O1").AutoFilter 1, "*" & Ky & "*"
Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
Intersect(Ws.AutoFilter.Range.EntireRow, Ws.Range("A:B,O:O")).Copy Range("A1")
Next Ky
Ws.AutoFilterMode = False
End With
End Sub


XZrGyTh
 
Upvote 0
If the error is on this line
Code:
Set Ws = Sheets("MASTER")
then you don't have a sheet called Master in the activeworkbook.
Check for leading/trailing spaces.
 
Upvote 0
If the error is on this line
Code:
Set Ws = Sheets("MASTER")
then you don't have a sheet called Master in the activeworkbook.
Check for leading/trailing spaces.

error is not there, it goes through the loops of reading all of A however it gets an error right when it creates the new sheet.
it creates an empty sheet and it just stops ther
 
Upvote 0
Add this line as shown
Code:
      For Each Ky In .Keys
         MsgBox "|" & Ky & "|"
         Ws.Range("A1:O1").AutoFilter 1, "*" & Ky & "*"
What does the msgbox say just before it fails?
 
Upvote 0
Please paste code between code tags. Click the # icon on reply toolbar to insert tags.

With cursor in the routine in the VBE, press F8 to execute each line to see which is causing the problem.

It could be that the activeworkbook does not contain a sheet named Master. It could be that your filter range did not start in row1. It could be that a sheet with name of Ky's value already exists, etc...
 
Upvote 0
Do you have blank cells in col A?
If so try
Code:
Sub excelnoobhere()
   Dim Cl As Range
   Dim Uniq As String
   Dim Ky As Variant
   Dim Ws As Worksheet
   
   Set Ws = Sheets("Master")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
         Uniq = Mid(Cl, 5, 4)
         If Uniq <> "" Then .Item(Uniq) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Range("A1:O1").AutoFilter 1, "*" & Ky & "*"
         Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
         Intersect(Ws.AutoFilter.Range.EntireRow, Ws.Range("A:B,O:O")).Copy Range("A1")
      Next Ky
      Ws.AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Code:
[CODE][CODE]
[/CODE][/CODE]
Do you have blank cells in col A?
If so try
Code:
Sub excelnoobhere()
   Dim Cl As Range
   Dim Uniq As String
   Dim Ky As Variant
   Dim Ws As Worksheet
   
   Set Ws = Sheets("Master")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
         Uniq = Mid(Cl, 5, 4)
         If Uniq <> "" Then .Item(Uniq) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Range("A1:O1").AutoFilter 1, "*" & Ky & "*"
         Sheets.Add(, Sheets(Sheets.Count)).Name = Ky
         Intersect(Ws.AutoFilter.Range.EntireRow, Ws.Range("A:B,O:O")).Copy Range("A1")
      Next Ky
      Ws.AutoFilterMode = False
   End With
End Sub

still not working

i have uploaded a sample of my excel sheet here if you'd like to give it a try

thanx in advance

https://www.dropbox.com/s/6bj17payiemet39/test.xlsx?dl=0
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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