gpt4 book ai didi

excel - 根据列值移动行

转载 作者:行者123 更新时间:2023-12-04 21:04:41 33 4
gpt4 key购买 nike

我有一个电子表格,如果 B 列中的数据与某个值匹配,我想剪切并将整行插入到电子表格的顶部。

这是我到目前为止所拥有的:

Cells.Find("TRCH").Select
ActiveCell.EntireRow.Select
Selection.Cut
Range("7:7").Select
Selection.Insert
Application.CutCopyMode = False

此代码有效,但它只找到 TRCH 的第一个实例。我需要更改什么以便从第 7 行开始插入所有包含 TRCH 的行?

提前致谢!

我已经更新了截图。第一个例子是原始数据集。第二个示例是我希望它在运行宏后的样子。上面的代码仅将带有 TRCH 的第一行移到顶部。希望澄清。

original data

updated data

最佳答案

这还没有经过测试,如果代码有任何问题或疑问,请告诉我:

Dim wks As Worksheet
Dim lRow As Long

Set wks = ThisWorkbook.ActiveSheet

'loop through each row from 1 to the last row in used range
For lRow = 1 To wks.UsedRange.Rows.Count

If UCase(wks.Cells(lRow, 2)) = "TRDM" Then 'check if the this cell contains TRDM

'if there is a match then cut current row and insert into row 7
wks.Rows(lRow).Cut wks.Range("7:7")

End If

Next

关于excel - 根据列值移动行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26491820/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com