在 Excel VBA 中删除一行

发布于 2024-12-11 03:37:06 字数 804 浏览 0 评论 0原文

我有这段代码,它从列表中查找项目的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落日海湾 2024-12-18 03:37:06

克里斯·尼尔森的解决方案很简单,而且效果很好。稍微短一点的选项是...

ws.Rows(Rand).Delete

...注意删除行时不需要指定 Shift,因为根据定义,不可能向左移动

顺便说一句,我删除行的首选方法是使用...

ws.Rows(Rand) = ""

...在初始循环中。然后,我使用排序函数将这些行推到数据的底部。这样做的主要原因是删除单行可能是一个非常慢的过程(如果您删除> 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...

ws.Rows(Rand).Delete

...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...

ws.Rows(Rand) = ""

...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

末骤雨初歇 2024-12-18 03:37:06

将您的线路更改

ws.Range(Rand, 1).EntireRow.Delete

ws.Cells(Rand, 1).EntireRow.Delete 

Change your line

ws.Range(Rand, 1).EntireRow.Delete

to

ws.Cells(Rand, 1).EntireRow.Delete 
千年*琉璃梦 2024-12-18 03:37:06

更好的是,使用 union 获取所有要删除的行,然后一次性删除它们。行不必是连续的。

dim rng as range
dim rDel as range

for each rng in {the range you're searching}
   if {Conditions to be met} = true then
      if not rDel is nothing then
         set rDel = union(rng,rDel)
      else
         set rDel = rng
      end if
   end if
 next
 
 rDel.entirerow.delete

这样你就不必担心排序或东西在底部。

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.

dim rng as range
dim rDel as range

for each rng in {the range you're searching}
   if {Conditions to be met} = true then
      if not rDel is nothing then
         set rDel = union(rng,rDel)
      else
         set rDel = rng
      end if
   end if
 next
 
 rDel.entirerow.delete

That way you don't have to worry about sorting or things being at the bottom.

三生路 2024-12-18 03:37:06

像这样的事情就会做到这一点:

Rows("12:12").Select
Selection.Delete

所以在你的代码中它看起来像这样:

Rows(CStr(rand) & ":" & CStr(rand)).Select
Selection.Delete

Something like this will do it:

Rows("12:12").Select
Selection.Delete

So in your code it would look like something like this:

Rows(CStr(rand) & ":" & CStr(rand)).Select
Selection.Delete
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文