使用find方法查找下一个可用空白行

列A的标签号码根据该标签号码的前缀而不同。 例如:

  • 35C-1234
  • 空白行在这里
  • 35C-1236
  • 35C-1237
  • 35TC-1234
  • 35TC-1235
  • 这里空白的行
  • 35TC-1237

我期待的macros有什么作用是在用户input标签号前缀后find第一个空白行。 在这种情况下,如果用户想要为标签号码前缀“35TC”find空白行,则将select标签号码“35TC-1235”之后的空白行而不是标签号码“35C-1234”之后的空白行”。 我得到的代码find一个空白的行,但是,我有麻烦执行.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”。 现在当我去search“35C”时,“35CA”的前缀就包含在里面了。 我如何保持它只是给我关于我search的结果?

在这里输入图像说明

在这种情况下,如果我search“35C”,代码将跳到“35CA-1600-K02”之后的空白处。 这是上面解释的两个问题的一个例子。

另一种方式,这…

 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 

首先,你正在寻找c ,你永远不会定义/声明。 此外,你可能想要使用一个Rangevariables来存储下一个非空单元格。

尝试这个:

 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 

注意:出于某种原因,如果您search35C ,则不会使用$A$2作为第一个空白单元格,但是让我来研究一下。 这可能会有所帮助,如果你可以发布一些更多的数据和例子,添加一些“错误”处理。