Sorting from a raw data base

running54

New Member
Joined
Oct 16, 2015
Messages
3
Hello -

I need help creating a vba code. I input data from a database to an excel sheet. I would like to sort this data into different spreadsheets. For instance: I want to sort "ele" and "ele-c" and have those criteria go to sheet one. Then have data "insta" go to sheet two, etc. The data I don't care about could remain on the input sheet. This is the code I have now. I want to be able to send two different criteria to one page.


Sub NotificationSort()
Dim SourceSheet As Worksheet
Dim TargetSheet As Worksheet
Dim SheetNames As Variant
Dim i As Long
Dim LR As Long
Set SourceSheet = Sheets("Data")
SheetNames = Array("ELE", "INSTA", "INSTF", "MW", "PFW")
Const FilterColumn = 6
With SourceSheet
LR = .Range("A" & .Rows.Count).End(xlUp).Row

For i = 0 To UBound(SheetNames)
Set TargetSheet = Worksheets(SheetNames(i))
TargetSheet.Cells.ClearContents

With .Range("A1:H" & LR)
.AutoFilter Field:=FilterColumn, Criteria1:=SheetNames(i)
.Offset(0, 0).Copy TargetSheet.Range("A1")
End With
Next i
End With
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the MrExcel board!

It depends a bit on just what might exist in column F.

The first option to consider might be to add the red text to your line as below
Rich (BB code):
.AutoFilter Field:=FilterColumn, Criteria1:=SheetNames(i) & "*"
This would add any rows where column F begins with "ELE" to the ELE sheet
Note though that it would also transfer rows beginning with "INSTA" to the INSTA sheet etc

If that does not suit your data then changing that line to the following would just make the "begins with" apply to the ELE sheet (first sheet in your sheet array)
Rich (BB code):
.AutoFilter Field:=FilterColumn, Criteria1:=SheetNames(i) & IIf(i = 0, "*", "")

But if column F might have values like "ELE-D" that you do not want moved to the ELE sheet then try
Rich (BB code):
.AutoFilter Field:=FilterColumn, Criteria1:=IIf(i = 0, Array(SheetNames(i), SheetNames(i) & "-c"), SheetNames(i)), Operator:=xlFilterValues
 
Upvote 0
OK, that's good.

It's not a big deal but Offset(0,0) of course doesn't offset at all, hence is not necessary.

Code:
<del>.Offset(0, 0)</del>.Copy TargetSheet.Range("A1")
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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