我还是VBA的新手,到目前为止还无法使用搜索功能找到任何解决方案。 问题: 我有(在这种情况下十张)一行,超过500个单元格包含5-7个字符串。 现在我需要删除搜索到的单词不是最后一个单词的所有列,但是单词位于字符串中不同位置的所有单元格中(在行中)。 我试过这个:
auto_increment_increment
我可以找到并识别单词和"如果"到目前为止工作正常,所选单元格没有任何反应,列也没有被删除。 通过一些更改,我只能删除整行,但它不是我需要的。 任何帮助appriciated。 Thx提前。
答案 0 :(得分:0)
我认为这会对你有帮助
Sub findtext()
Dim wsAkt As Worksheet
Dim i As Integer
Dim k As Integer
Dim x As Integer
Dim strWord As String
Dim intWord As Integer
'get the word
strWord = Application.InputBox("Looking for?")
'length of word
intWord = Len(strWord)
'loop through all worksheets
For i = 1 To ThisWorkbook.Worksheets.Count
'variable for selected worksheet
Set wsAkt = ThisWorkbook.Worksheets(i)
'get how many columns are in row 3
x = wsAkt.Cells(3, wsAkt.Columns.Count).End(xlToLeft).Column
'loop through row 3 columns
For k = 1 To x
'if last Word in cell = the word then it has to have the same length
If Right(wsAkt.Cells(3, k), intWord) <> strWord Then
'delete selected column
wsAkt.Columns(k).Delete
End If
Next k
Next i
End Sub
(未经测试)
答案 1 :(得分:0)
这应该可行,但我建议清理语法。我已经删除了您选择范围的代码(网上有很多关于您不应该这样做的信息)。
创建一个数组来查找最后一个单词并根据搜索值进行测试。
Sub Test()
Dim examinee As Variant
Dim cell As Range
Dim word As String
Dim myCell As String
Dim arr() As String
Dim strLastWord As String
'How Many Sheets Should We Loop?
examinee = InputBox("How many sheets?")
'What Word Are We Searching For?
word = InputBox("Looking for?")
'Loop Sheets
For A = 1 To examinee
'Loop Cells In Row 3
For Each cell In Range("A3", Range("A3").End(xlToRight))
'Get The Value Of The Current Cell
myCell = Right(cell, Len(cell) - (InStrRev(cell, " ") - 1))
'Is It A Single Word?
If InStr(1, myCell, " ") Then
'Several Words. Create An Array Of Individual Words In The Cell
arr() = Split(myCell, " ")
'Get The Number Of The Last Word
strLastWord = arr(UBound(arr))
Else
'Single Word. Get The Word
strLastWord = myCell
End If
'Is The Last Word = The Search Word?
If strLastWord = word Then
'Yes. Make It Bold
cell.Font.Bold = True
Else
'No. Delete The Column
Columns(cell.Column).Delete
End If
Next cell
Next
End Sub