Newbie;VBA Advanced Filter Help

parsec3

New Member
Joined
Sep 20, 2003
Messages
4
First off I am just learning VBA.
The code below is in a module for a workbook. What it does is send each individual row
of my database to it's own worksheet.(creates one if none exsist).
The problem is that it replaces the old data that is on the worksheets with the new data
every time it is run.(ie. I never have more than the header and ONE row of data)
What I need it to do is add a new row of data every time the macro is run.
From what I understand when I call AdvancedFilter(at least the way I have it now)
it is defaulted to replacing the existing data.
The last row code must only apply to the sheets I am sending the data to.
Thanks in advance for any help!

Code:
Option Explicit

Sub FilterStocks()
Dim c As Range
Dim ws As Worksheet
For Each c In Range("STOCKLIST")
  If WksExists(c.Value) = False Then
    Set ws = Sheets.Add
    ws.Name = c.Value
    ws.Move After:=Sheets(Sheets.Count)
    Sheets(Range("STOCKLIST").Cells(1, 1).Value) _
      .Rows("1:1").Copy Destination:=ws.Rows("1:1")
  End If
  Sheets("CRIT").Range("D2").Value = c.Value
  Sheets("DATA").Range("DATABASE").AdvancedFilter _
    Action:=xlFilterCopy, _
      CriteriaRange:=Sheets("CRIT").Range("D1:D2"), _
      CopyToRange:=Sheets(c.Value).Range("A1:H1"), _
      Unique:=False
Next
MsgBox "Data has been sent"
End Sub


Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi parsec3:

Welcome to MrExcel Board!

The problem occurs when you write the data after AdvanceFiltering to a sheet where some data already exists -- this is because your CopyToRange is always

A1:H1

so you are always writing starting from cell A1

so one of the ways we can fix this is

a) by defining our CopyToRange to begin one row under the existing number of rows in the sheet being written to
b) and this means we will be writng the Header Row as well as the AdvanceFiltered data -- which means every time we encounter a situation that we are writing to a sheet that has some existing data, we also have to delete the row with the header data

Alternately, you AdvanceFilter the data in the original sheet and then copy only the AdvanceFiltered data (sans Field Headers) to the sheet of interest

Does this help? If I have misunderstood your question -- my apologies!
 
Upvote 0
Let me take this one at a time,

>>>this is because your CopyToRange is always ....A1:H1
Yes I see this, in fact this is the first place I tried to change
the code to do what I want. I tried several different ways but
still no go.
This is from memory (not at computer with workbook)
Tried xlup? to find last empty row and enter data one row below.
I also tried to use some of the code on this page to make it work,
http://www.rondebruin.nl/copy1.htm
Could not get anything to work, remember I am a newbie.

>>>b) and this means we will be writng the Header Row as well as the AdvanceFiltered data -- which means every time we encounter a situation that we are writing to a sheet that has some existing data, we also have to delete the row with the header data.
Not sure what you mean...
>delete the header from the sheets that it is filtered to
every time the data is sent?

>>>Alternately, you AdvanceFilter the data in the original sheet and then copy only the AdvanceFiltered data (sans Field Headers) to the sheet of interest
This would work...doable even for me<g>.I am just trying to find
a way to do this without so many steps.
 
Upvote 0
parsec3 said:
....
>>>Alternately, you AdvanceFilter the data in the original sheet and then copy only the AdvanceFiltered data (sans Field Headers) to the sheet of interest
This would work...doable even for me<g>.I am just trying to find
a way to do this without so many steps.
Hi parsec3:

Looks like you like the Alternate method proposed in my earlier respons. Are you all set now? -- meaning do you have everything working now the way it should? otherwise post back and then let us take it from there.
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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