[英]Excel VBA: Find a value in Worksheet, offset column to find same value
Apologies for my newbishness but I am experiencing a bit of an issue regarding some excel vba code I made. 抱歉,我对我制作的一些excel vba代码有点问题。 It is meant to clean an excel sheet that has hundreds of entries.
它旨在清除具有数百个条目的Excel工作表。
The format is always the same, and columns D, E, and F will sometimes have the same word in all three of them (the word purge). 格式始终相同,并且D,E和F列有时在所有三个列中都具有相同的单词(单词purge)。 When this is the case, I want that row deleted.
在这种情况下,我希望删除该行。
I have tried to use define a my range as D, and using the .Find feature search for my item in question. 我尝试使用将我的范围定义为D,并使用.Find功能搜索所涉及的我的商品。 Then from there offset Find offset Find.
然后从那里偏移量查找偏移量查找。
Alas, my program has converted from telling me I am coding wrong to simply running (freezing) forever with no results. las,我的程序已经从告诉我我编码错误转变为只是永久运行(冻结)而没有任何结果。 I will post my code so that maybe my situation is more clear.
我将发布我的代码,以便也许我的情况更加清楚。
PS If someone can help me fix my code, and also explain what I did wrong and why, it would be much appreciated! PS:如果有人可以帮助我修复我的代码,并解释我做错了什么以及原因,将不胜感激!
Thank you, all! 谢谢你们!
Sub DeleteRows()
Dim strPath As String
Dim strFile As String
Dim wbk As Workbook
Dim wsh As Worksheet
Dim strFind As String
Dim SrchRng
Dim x As Range
Dim y
Dim z
'searches directory for any and all excel files
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then
strPath = .SelectedItems(1)
Else
MsgBox "No folder selected!", vbExclamation
Exit Sub
End If
End With
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
Application.ScreenUpdating = False
strFile = Dir(strPath & "*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open(Filename:=strPath & strFile, AddToMRU:=False)
For Each wsh In wbk.Worksheets
'end directory search
'supposed to search column d, offset column +1 in same row,
'then do same for a third row.
'if all three cells contain "PURGE" then delete cell
Set SrchRng = ActiveSheet.Range("D1",
ActiveSheet.Range("D65536").End(xlUp))
Do
Set x = SrchRng.Find("PURGE", LookIn:=xlValues)
Set y = ActiveCell.Offset(0, 1).Find("PURGE", LookIn:=xlValues)
Set z = ActiveCell.Offset(0, 2).Find("PURGE", LookIn:=xlValues)
If Not x Is Nothing And Not y Is Nothing And Not z Is Nothing Then x.EntireRow.Delete
Loop While Not x Is Nothing
Next
For Each wsh In wbk.Worksheets
Next wsh
wbk.Close SaveChanges:=True
strFile = Dir
Application.ScreenUpdating = True
End Sub
I would personally write the formula in excel that would figure out if a row needs to be deleted or not. 我将亲自在excel中编写公式,以找出是否需要删除行。 (do any of the columns for the row contain the word you are looking for).
(在该行的任何列中都包含您要查找的单词)。 Once I know the formula work correctly I would add it to the file using vba in the last column.
一旦知道公式可以正常工作,就可以在最后一栏中使用vba将其添加到文件中。
Once the formula in the last column I would sort this row and deleted any of the rows I need. 将公式放在最后一列后,我将对该行进行排序并删除我需要的任何行。 Then delete the new column and save the file.
然后删除新列并保存文件。
Have a look at this. 看看这个。 This uses
FindNext
and Offset
(not entirely sure why you were trying to use Find
on one cell) This finds the value PURGE
and if the two cells next to it are also PURGE
it then deletes the row. 这将使用
FindNext
和Offset
(不完全确定为什么要在一个单元格上使用“ Find
”),这将找到值PURGE
,如果旁边的两个单元格也是PURGE
,则将其删除。 If not it carries on going. 如果没有,它将继续进行。
Also this line Do While strFile <> ""
needs a Loop
at some point although not sure where you wanted it. 另外,虽然不确定要在哪里,但此行在
Do While strFile <> ""
需要Loop
。
Option Explicit
Sub DeleteRows()
Dim wbk As Workbook
Dim wsh As Worksheet
Dim strPath As String, strFile As String, strFind As String, firstAddress As String
Dim x As Range, y As Range, z As Range
Dim DelRng As Range
'searches directory for any and all excel files
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then
strPath = .SelectedItems(1)
Else
MsgBox "No folder selected!", vbExclamation
Exit Sub
End If
End With
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
With Application
.ScreenUpdating = False
' Stops xlsm files on Open event
.EnableEvents = False
' uncomment to hide any deletion message boxes
' .DisplayAlerts = False
End With
strFile = Dir(strPath & "*.xls*")
Do While Len(strFile) > 0
Set wbk = Workbooks.Open(Filename:=strPath & strFile, AddToMRU:=False)
For Each wsh In wbk.Worksheets
'end directory search
'supposed to search column d, offset column +1 in same row,
'then do same for a third row.
'if all three cells contain "PURGE" then delete cell
With wsh.Columns(4)
Set x = .Find("PURGE", Lookat:=xlPart)
If Not x Is Nothing Then
firstAddress = x.Address
Do
If InStr(1, x.Offset(0, 1), "purge", vbTextCompare) > 0 And InStr(1, x.Offset(0, 2), "purge", vbTextCompare) > 0 Then
If DelRng Is Nothing Then
Set DelRng = x
Else
Set DelRng = Union(DelRng, x)
End If
End If
Set x = .FindNext(x)
Loop While Not x Is Nothing And x.Address <> firstAddress
End If
End With
Next wsh
If Not DelRng Is Nothing Then DelRng.EntireRow.Delete
wbk.Close SaveChanges:=True
strFile = Dir()
Loop
With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.