在 Excel VBA 中删除一行
我有这段代码,它从列表中查找项目的 Excel 行并从列表中删除该项目。我想要的是...也删除 Excel 行。
代码在这里
Private Sub imperecheaza_Click()
Dim ws As Worksheet
Dim Rand As Long
Set ws = Worksheets("BD_IR")
Rand = 3
Do While ws.Cells(Rand, 4).Value <> "" And Rand < 65000
If ws.Cells(Rand, 4).Value = gksluri.Value * 1 And ws.Cells(Rand, 5).Value = gksluri.List(gksluri.ListIndex, 1) * 1 Then
ws.Range(Rand, 1).EntireRow.Delete '(here I want to delete the entire row that meets the criteria from the If statement)
gksluri.RemoveItem gksluri.ListIndex
Exit Do
End If
Rand = Rand + 1
Loop
End Sub
我添加了 ws.Range(Rand,1).EntireRow.Delete 是我想删除整行但我不知道该怎么做的地方。我想要什么...如果它在单元格中找到相同的值(例如我的列表中的某些选定项目),则能够删除 Excel 中的整行和列表框中的项目。它可以从列表框中删除该项目,但我也不知道如何删除该行
I have this piece of code which finds the excel row of an item from a list and deletes the items from a list. What I want... is to delete the Excel row as well.
The code is here
Private Sub imperecheaza_Click()
Dim ws As Worksheet
Dim Rand As Long
Set ws = Worksheets("BD_IR")
Rand = 3
Do While ws.Cells(Rand, 4).Value <> "" And Rand < 65000
If ws.Cells(Rand, 4).Value = gksluri.Value * 1 And ws.Cells(Rand, 5).Value = gksluri.List(gksluri.ListIndex, 1) * 1 Then
ws.Range(Rand, 1).EntireRow.Delete '(here I want to delete the entire row that meets the criteria from the If statement)
gksluri.RemoveItem gksluri.ListIndex
Exit Do
End If
Rand = Rand + 1
Loop
End Sub
Where I added ws.Range(Rand,1).EntireRow.Delete is where I want to delete the entire row but I don't know how to do it. What I want... if it finds the same value in a cell like in some selected item of my list to be able to remove both the entire row in excel and the item from the listbox. It works to remove the item from the listbox but I don't know how to remove the row as well
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
克里斯·尼尔森的解决方案很简单,而且效果很好。稍微短一点的选项是...
...注意删除行时不需要指定 Shift,因为根据定义,不可能向左移动
顺便说一句,我删除行的首选方法是使用...
...在初始循环中。然后,我使用排序函数将这些行推到数据的底部。这样做的主要原因是删除单行可能是一个非常慢的过程(如果您删除> 100)。它还确保按照 Robert Ilbrink 的评论不会遗漏任何内容。
您可以通过录制宏并减少代码来学习排序代码,如 Excel 专家视频。我怀疑最简洁的方法 (Range("A1:Z10").Sort Key1:=Range("A1"), Order1:=xlSortAscending/Descending, Header:=xlYes/No) 只能在预发现2007 版本的 Excel...但您始终可以减少 2007/2010 等效代码
更多的要点...如果您的列表尚未按列排序并且您希望要保留顺序,您可以在循环时将行号“Rand”粘贴到每行右侧的备用列中。然后,您可以按该注释进行排序并将其删除。
如果您的数据行包含格式,您可能希望找到新数据范围的末尾并删除之前清除的行。这是为了减小文件大小。请注意,过程末尾的单个大删除不会像删除单行那样损害代码的性能
Chris Nielsen's solution is simple and will work well. A slightly shorter option would be...
...note there is no need to specify a Shift when deleting a row as, by definition, it's not possible to shift left
Incidentally, my preferred method for deleting rows is to use...
...in the initial loop. I then use a Sort function to push these rows to the bottom of the data. The main reason for this is because deleting single rows can be a very slow procedure (if you are deleting >100). It also ensures nothing gets missed as per Robert Ilbrink's comment
You can learn the code for sorting by recording a macro and reducing the code as demonstrated in this expert Excel video. I have a suspicion that the neatest method (Range("A1:Z10").Sort Key1:=Range("A1"), Order1:=xlSortAscending/Descending, Header:=xlYes/No) can only be discovered on pre-2007 versions of Excel...but you can always reduce the 2007/2010 equivalent code
Couple more points...if your list is not already sorted by a column and you wish to retain the order, you can stick the row number 'Rand' in a spare column to the right of each row as you loop through. You would then sort by that comment and eliminate it
If your data rows contain formatting, you may wish to find the end of the new data range and delete the rows that you cleared earlier. That's to keep the file size down. Note that a single large delete at the end of the procedure will not impair your code's performance in the same way that deleting single rows does
将您的线路更改
为
Change your line
to
更好的是,使用 union 获取所有要删除的行,然后一次性删除它们。行不必是连续的。
这样你就不必担心排序或东西在底部。
Better yet, use union to grab all the rows you want to delete, then delete them all at once. The rows need not be continuous.
That way you don't have to worry about sorting or things being at the bottom.
像这样的事情就会做到这一点:
所以在你的代码中它看起来像这样:
Something like this will do it:
So in your code it would look like something like this: