copying rows based on cell value

  • Thread starter Thread starter Ronnie
  • Start date Start date
R

Ronnie

I know this question has been asked before and I did search but I wa
not able to figure anything out so here it goes:

I need to copy a row in my "data" sheet to other worksheets based on
cell value that is located in column C. I have roughly 1000 rows and
different values located in column C. So basically it will look at eac
row and copy that row to its correct worksheet based on the value i
Coulmn C. Is this making sense?
 
Thanks Ron, this is exactly what I need but I still can't get it t
work. What it does is create sheets with numbers as its name (values i
A1) not C1. Also, it doesnt seem to copy entire row, just the
column.

I've tried this without the sheets existing but will eventually be on
with the sheets already existing and a simple clearing of the rang
will do
 
Hi

Try this

The example use Range("YourRange")
and filter on the first column

If you use Range("A1:Z1000") and filter on column 3

rng.Columns(1).AdvancedFilter
Change to
rng.Columns(3).AdvancedFilter
 
When I do this, I get the following error:
Run-time error '1004':

The extract range has a missing or illegal field name

Do I need to change the CopyToRange for it to accept a larger rang
now?

rng.Columns(3).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("IV1"), Unique:=Tru
 
Hi Ronnie

Do I need to change the CopyToRange for it to accept a larger range
now?

No

If you have change your range like this
Set rng = ws1.Range("A1:Z1000")

And have a header in the first row if your range
it must work
 
Yeah, I figured out my problem. There were still values in column IV s
after I deleted them, it worked perfect. thanks for all your help.

Now, how would I do it if the sheets already existed
 
Try this one

With headers in the first row of the range

Sub Copy_With_AdvancedFilter()
Dim ws1 As Worksheet
Dim WSNew As Worksheet
Dim rng As Range
Dim cell As Range
Dim Lrow As Long
Dim headerrow As Long
Dim str As String
Set ws1 = Sheets("Sheet1")
Set rng = ws1.Range("A1:Z1000")
'Use a Dynamic range name, http://www.contextures.com/xlNames01.html#Dynamic

With ws1
rng.Columns(3).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("IV1"), Unique:=True
'You see that the last two columns of the worksheet are used to make a Unique list
'and add the CriteriaRange.(you can't use this macro if you use this columns)
Lrow = .Cells(Rows.Count, "IV").End(xlUp).Row
.Range("IU1").Value = .Range("IV1").Value

For Each cell In .Range("IV2:IV" & Lrow)
.Range("IU2").Value = cell.Value
str = cell.Value
If SheetExists(str) = False Then
Set WSNew = Sheets.Add
On Error Resume Next
WSNew.Name = str
On Error GoTo 0

Else
Set WSNew = Sheets(str)
End If
headerrow = LastRow(WSNew) + 1
rng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=.Range("IU1:IU2"), _
CopyToRange:=WSNew.Cells(LastRow(WSNew) + 1, 1), _
Unique:=False
If headerrow > 1 Then WSNew.Rows(headerrow).Delete
Next
.Columns("IU:IV").Clear
End With
End Sub


You must copy this two functions in the module also

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function
 
Thanks Ron,
THe only thing I don't like about this new code is that it doesn
delete the existing information, it just adds on at the end. I di
replace:

CopyToRange:=WSNew.Cells(LastRow(WSNew) + 1, 1), _

with:

CopyToRange:=WSNew.Range("A1"),_

It works fine now but when the data begins to change (grow/shrink), i
won't work properly. It works flawlessly when the sheets do not exist
I'll have to find a way to either clear the cells before pasting o
delete the sheets before creating them
 
Hi

I thought you want to add the data to the existing data
Just delete the sheet if it exist then before you do the copy
in the first example you use
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top