我是Excel Vba的新手,我编写了一个搜索输入文本的程序,如果它出现在从A列到L列的任何地方,它应该在该特定行的第V列中写入用户输入。我在Do While Cells(i,1)<>""
行遇到了错误。任何解决这个问题的建议都将不胜感激。
Sub testing()
Application.ScreenUpdating = False
Dim oRange As Range, aCell As Range, bCell As Range, sAddress As Range
Dim store As Variant
Dim ws As Worksheet
Dim SearchString As String, FoundAt As String, Device As String, FailureUnitValue As String
Dim Occurence1ms As Integer, Occurence10ms As Integer, Occurence As Integer
Dim DeviceCellAdd As Variant, DeviceCellValue As Variant, FailureUnitAddress As Variant, Failure As Variant
Dim errorcode As Variant
Dim rowNum As Double
Dim i As Long
Dim lastrow As Double
errorcode = InputBox("Enter Error Code")
Worksheets("DUT1").Activate
Range("V:V").ClearContents
Set oRange = Range("G:G")
Do While Cells(i, 1) <> ""
If InStr(ActiveCell.Value, errorcode, vbTextCompare) <> 0 Then
rowNum = ActiveCell.Row
Range("V:" & rowNum).Value = errorcode
ActiveCell.Offset(0, -1).Select
End If
i = i + 1
Loop
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
您尚未将i
设置为等于有效的列行号。
目前正在尝试评估显然没有意义的Do While Cells(0, 1) <> ""
。
将i = 1
(或您希望开始的任何行)放在上面一行。
答案 1 :(得分:0)
这是您原始问题中宏的目标描述:
&#34;搜索输入文本以及它是否存在于A列的任何位置 通过列L,它应该在第五列中写入用户输入 特定行&#34;
此代码将实现该目标,评论以帮助提供清晰度:
Sub tgr()
'Declare variables
Dim ws As Worksheet 'Used to store the worksheet that contains the data
Dim rngData As Range 'Used to store the range that contains the data
Dim rngFound As Range 'Used to find all matches (if any)
Dim rngOutput As Range 'Used to store cells where the output will be displayed (in column V)
Dim strErrorCode As String 'Used to store the user input
Dim strFirst As String 'Used to store the first found cell to prevent infinite loop
'Get input from user
strErrorCode = InputBox("Enter Error Code")
If Len(strErrorCode) = 0 Then Exit Sub 'Pressed cancel
Set ws = ActiveWorkbook.Sheets("DUT1") 'Set the ws variable to the worksheet that contains the data
Set rngData = ws.Range("A:L") 'Set the rngData variable to the range that contains the data
ws.Columns("V").ClearContents 'Clear column V of previous results
'Check columns A:L for the provided input, checking for partial matches
Set rngFound = rngData.Find(strErrorCode, rngData.Cells(rngData.Cells.Count), xlValues, xlPart)
'Was anything found?
If rngFound Is Nothing Then
'Nothing found, return error
MsgBox "No matches found for [" & strErrorCode & "]"
Else
'Found a match, record first found cell and begin building the output range
strFirst = rngFound.Address
Set rngOutput = ws.Cells(rngFound.Row, "V")
'Initiate a loop to find all matches
Do
'Build output range from matching cells using the Union method
Set rngOutput = Union(rngOutput, ws.Cells(rngFound.Row, "V"))
Set rngFound = rngData.FindNext(rngFound) 'Go to next matching cell
Loop While rngFound.Address <> strFirst 'End loop when back to first found cell
'Output the data to the output range
rngOutput.Value = strErrorCode
End If
End Sub