列A的标签号根据该标签号的前缀而变化。例如:
我希望宏做的是在用户输入标签号前缀后找到第一次出现空行。在这种情况下,如果用户想要为标签号前缀“35TC”找到空行,则选择标签号“35TC-1235”之后的空白行,而不是标签号“35C-1234之后的空行” ”。我得到了一个空白行的代码但是,我在将这个代码中的.Find()函数实现时遇到了麻烦,非常感谢您对此有任何帮助!
正在使用的代码:
Private Sub Worksheet_Activate()
Dim msg As String
Dim result As Integer
Dim x As String
msg = "Would you like to find the next available tag number?"
result = MsgBox(msg, vbYesNo)
If result = vbYes Then
x = Application.InputBox("Enter the part reference ")
'need to work on the find function here
Cells.Find(What:=c, Lookin:=range("A"))
NextFree = Range("A:A").Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
Else
Cancel = True
End If
End Sub
编辑问题:
通过以下两个答案的帮助,我已经能够在用户声明前缀后得到空格。我现在遇到两个问题,其中1)如果声明的前缀没有空行,代码将跳转到下一个可用的空行,即使它具有不同的前缀。而不是发生这种情况,我希望它可能会转到包含前缀的最后一个标签,并输出一条消息,声明前缀没有任何空白行。 2)我还添加了一个新的前缀“35CA”。现在,当我去搜索“35C”时,其中包含“35CA”的前缀。我如何保持它只是给我一些与我搜索到的内容有关的结果?
在这种情况下,如果我搜索“35C”,则代码将在“35CA-1600-K02”之后跳到空白处。这是上面解释的两个问题都发生的例子。
答案 0 :(得分:2)
另一种方式......
Sub FindBlankRow()
Dim Rng As Range, eRng As Range
Dim lr As Long
Dim str As String
str = InputBox("Enter the part reference ")
lr = Cells(Rows.Count, 1).End(xlUp).Row + 1
On Error Resume Next
Set Rng = Range("A2:A" & lr).SpecialCells(xlCellTypeBlanks)
If Not Rng Is Nothing Then
For Each eRng In Rng.Areas
If InStr(LCase(eRng.Cells(1).Offset(-1, 0)), LCase(str)) > 0 Then
eRng.Cells(1).Select
Exit For
End If
Next eRng
End If
End Sub
编辑答案:
Sub FindBlankRow()
Dim Cell As Range
Dim str As String, firstcell As String
str = InputBox("Enter the part reference ")
If str = "" Then Exit Sub
If Right(str, 1) <> "-" Then str = str & "-"
With Range("A:A")
Set Cell = .Find(str, lookat:=xlPart, MatchCase:=False)
If Not Cell Is Nothing Then
firstcell = Cell.Address
Do
If Cell.Offset(1, 0) = "" Then
Cell.Select
Exit Sub
ElseIf InStr(LCase(Cell.Offset(1, 0)), LCase(str)) = 0 Then
Cell.Select
MsgBox "No blank cell was found below a code with prefix " & str & ".", vbExclamation
Exit Sub
End If
Set Cell = .FindNext(Cell)
Loop While Not Cell Is Nothing And firstcell <> Cell.Address
End If
End With
End Sub
答案 1 :(得分:1)
首先,您正在寻找您从未定义/声明的c
。此外,您可能希望使用Range
变量来存储下一个非空单元格。
试试这个:
Sub find_value()
Dim msg As String, lookForValue As String
Dim result As Integer
Dim foundCell As Range, emptyCell As Range
msg = "Would you like to find the next available tag number?"
result = MsgBox(msg, vbYesNo)
If result = vbYes Then
lookForValue = Application.InputBox("Enter the part reference ")
'need to work on the find function here
Set foundCell = Range("A:A").Find(What:=lookForValue & "*")
Debug.Print foundCell.Address
Set emptyCell = foundCell.End(xlDown).Offset(1, 0)
' Now you have your cell. Do whatever with it...
MsgBox ("The next empty cell is " & emptyCell.Address)
Else
Cancel = True
End If
End Sub
注意:出于某种原因,如果您搜索$A$2
,则不会将35C
用于第一个空白单元格,但让我继续处理。如果您可以发布更多数据和示例,添加一些“错误”处理可能会有所帮助。