Excel 迭代列中的单元格,如果包含条件,则将行复制到新工作表

Excel 迭代列中的单元格,如果包含条件,则将行复制到新工作表,excel,vbscript,vba,Excel,Vbscript,Vba,我有一个类似于以下内容的电子表格: Name Title ID Person 1 Title 1 1 Person 2 Title 2 11 Person 3 Title 3 111 Person 4 Title 4 1111 Person 5 Title 5 12 Person 6 Title 6 121 Person 7 Title 7 1211 Person 8

我有一个类似于以下内容的电子表格:

Name Title ID Person 1 Title 1 1 Person 2 Title 2 11 Person 3 Title 3 111 Person 4 Title 4 1111 Person 5 Title 5 12 Person 6 Title 6 121 Person 7 Title 7 1211 Person 8 Title 8 1212 Person 9 Title 9 122 Person 10 Title 10 13 Person 11 Title 11 131 Person 12 Title 12 1311 Person 13 Title 13 1312 Person 14 Title 14 13121 Person 15 Title 15 1313 Person 16 Title 16 132 Person 17 Title 17 1321 Person 18 Title 18 14 Person 19 Title 19 15 Person 20 Title 20 151 Person 21 Title 21 1512 名称标题ID 第1人标题1 第2人标题2 11 第3人标题3 111 第4人标题4 1111 第5人标题5 12 第6人标题6 121 第7人标题7 1211 第8人标题8 1212 第9人标题9 122 人员10职务10 13 人11标题11 131 第12人标题12 1311 第13人标题13 1312 人员14职务14 13121 第15人标题15 1313 第16人标题16 132 人17标题17 1321 个人18职务18 14 人19标题19 15 第20人标题20 151 第21人标题21 1512 我想遍历“ID”列中的每个单元格。 如果列包含值,则将整行复制到新图纸

这里是棘手的部分:

- The first number will always start with 1. - I want the script to search "ID" for 1 + another single digit (10,11,12,13...). For all cells that match the criteria copy that row to a new sheet. - When that is done, now search for 11 + another single digit (111,112,113,114...). - When that is done, now search for 12 + another digit (121,122,123,124...) -第一个数字总是以1开头。 -我希望脚本在“ID”中搜索1+另一个单个数字(10,11,12,13…)。 对于符合条件的所有单元格,将该行复制到新图纸。 -完成后,现在搜索11+另一个单个数字(11111 211314…)。 -完成后,现在搜索12+另一个数字(121122123124…) 这应该继续下去:

Search "ID" for: Finds all: 19 191, 192, 193, 194... 124 1241, 1242, 1243, 1244... 1111 11111, 11112, 11113, 11114, 11115... 1127 11271, 11272, 11273, 11274... 12345 123451, 123452, 123453, 123454, 123455... 在“ID”中搜索:查找所有: 19 191, 192, 193, 194... 124 1241, 1242, 1243, 1244... 1111 11111, 11112, 11113, 11114, 11115... 1127 11271, 11272, 11273, 11274... 12345 123451, 123452, 123453, 123454, 123455... “+另一个单个数字”前面的数字指示它应该进入哪张纸

- If search for 1 + another single digit - these should all go on a single sheet. - If searching for 11 + another single digit - these should go on a single sheet. - If search for 1234 + another single digit - these should go on a single sheet. - So on and so forth. -如果搜索1+另一个单个数字-这些数字都应该放在一张纸上。 -如果搜索11+另一个单个数字-这些数字应放在一张纸上。 -如果搜索1234+另一个单个数字-这些数字应放在一张纸上。 -诸如此类。
我意识到我应该在发问之前公布我的尝试。不管怎样,我知道怎么做了。我是VBA新手,但这很有效:

Set mainSht = Worksheets("Sheet1")  ' Copy From this sheet

'Find the last row with data in specified column.
LR = mainSht.Cells(Rows.Count, "F").End(xlUp).Row

Set LRange = mainSht.Range("F2:F" & LR)

'Find max number in the range
max = Application.WorksheetFunction.max(LRange)

'Look at every cell in cpecified column
For Each Cell In LRange
 For i = 1 To max

    cIndex = CStr(i)

    If Cell.Value Like "1" + cIndex + "?" Then    'check to See if C equals value                                       
        sheetName = "1" + cIndex

        'Check if worksheet exists
        Set wsCheck = Nothing
        On Error Resume Next
        Set wsCheck = ActiveWorkbook.Worksheets(sheetName)
        On Error GoTo 0

        'If worksheet does not exist, create new
        If wsCheck Is Nothing Then
            Worksheets.Add(After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)).Name = sheetName
        End If

Set Pastesheet = Worksheets(sheetName)  ' Paste to this sheet
            Cell.EntireRow.Copy  ' Copy the row
            Pastesheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
    End If
    Next

Next Cell  ' Now we check the next cell in chosen column

@我在下面添加了我的代码。