简体   繁体   中英

Excel Script - Split list into 3 sheets

I run a report of email addresses every month and split the list into 3 different sheets within the same workbook depending on the domain of each address. Each row contains about 5 columns of data, so the whole row must be moved to the next sheet.

It would look something like this:

If column B contains "@aaaaaa.com" keep row on sheet 1.

If column B contains "@bbbbbb.com" move whole row to sheet 2.

If column B contains "@cccccc.com" move whole row to sheet 3. Check next row

In the end I need 3 separate sheets that contain a list of each of the three domain email addresses. I do this by hand now and going through 400+ rows is too tedious when I now there is a better way. Any help on this would be much appreciated!

This should be enough to get you started.

Sub split()

Dim lastrow As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim domain As String
Dim a As String
Dim b As String
Dim c As String

Set ws1 = Worksheets(1)
Set ws2 = Worksheets(2)
Set ws3 = Worksheets(3)

a = "aaaaaa.com"
b = "bbbbbb.com"
c = "cccccc.com"

lastrow = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row

ws1.Select

For i = 1 To lastrow

    domain = Right(Cells(i, 2), 10)

    If domain = b Then

        Rows(i).Copy
        ws2.Cells(i, 1).PasteSpecial

    End If

Next i

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM