1 excel search vba delete-row excel-2016
这是我第一次发帖.
我正在尝试在Excel 2016中找到一种方法来清理文件夹列表,以便我只有父文件夹.
我有一个电子表格,其中A列是文件夹列表,包括其子文件夹.像这样:[fyi - 每行中还有其他列,但它们与此示例无关]
\\server\share\root\subfolder1\
\\server\share\root\subfolder1\sub-subfolderA\
\\server\share\root\subfolder1\sub-subfolderB\
\\server\share\root\subfolder1\sub-subfolderC\
\\server\share\root\subfolder2\
\\server\share\root\subfolder2\other-subfolderA\
\\server\share\root\subfolder2\other-subfolderB\
\\server22\share\root\subfolder3\ham_sandwich\
\\server22\share\root\subfolder3\ham_sandwich\yet-another-subfolderA\
\\server22\share\root\subfolder3\and-another-subfolderA\
\\server22\share\root\subfolder3\and-another-subfolderB\
Run Code Online (Sandbox Code Playgroud)
大约有2500行包含不同长度的文件夹,我的最终目标是最终只有每个"集合"的顶级文件夹.例如:
\\server\share\root\subfolder1\
\\server\share\root\subfolder2\
\\server22\share\root\subfolder3\ham_sandwich\
\\server22\share\root\subfolder3\and-another-subfolderA\
\\server22\share\root\subfolder3\and-another-subfolderB\
Run Code Online (Sandbox Code Playgroud)
我对此的逻辑如下(如果我忽视任何事情,请纠正我):
See if the string in A1 is contained within the string in A2.
If A2 contains the string, delete row 2.
If it doesn't, move down to compare A2 with A3. [since we know A1 is now the only cell containing that top folder]
Rinse-and-repeat until the last populated row is reached.
Run Code Online (Sandbox Code Playgroud)
我的问题是弄清楚这个代码.我在网上看到各种关于搜索指定文本的代码片段,但没有使用变量的代码片段.我最初使用IsNumber和Search的公式组合,但它需要固定的文本进行搜索,这会随着宏的进展而改变.
有人能指出我正确的方向吗?
假设顶级文件夹始终列在子文件夹之前:
k = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = k - 1 To 1 Step -1
For j = k To i + 1 Step -1
If InStr(Range("A" & j), Range("A" & i)) > 0 Then
Rows(j).Delete
k = k - 1
End If
Next j
Next i
Run Code Online (Sandbox Code Playgroud)