VBA-查找第一个空单元的行号或返回行号

发布于 2025-02-01 11:28:16 字数 1904 浏览 1 评论 0原文

我正在努力使下面的代码工作,并非常感谢任何支持。

我可以找到包含可变ID号的行号 - findrow可以正常工作。

如果Findrow = 0,我想返回下一个空排的行号。我正在使用FindNextrow尝试完成此操作,但显然我缺少一些东西。

Function findId(ids As String) As Integer
    Dim findRow As Range
    Dim findNextRow As Range
    
    Sheets("Data").Select
    Columns("A:A").Select
    
    Set findRow = Selection.Find(What:=ids, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        
    Set findNextRow = Selection.Data.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        
    
    If Not findRow Is Nothing Then
        findId = findRow.Row
        
    ElseIf Not findRow Is Nothing Then
        findId = findNextRow.Row
        
    Else
        findId = 0
        
            End If
    
End Function

编辑 此处正在调用该功能:

Sub Import()
    Dim Drop As Worksheet
    Dim Data As Worksheet

    Set Drop = Worksheets("Drop")
    Set Data = Worksheets("Data")

    Dim idRow As Integer
    idRow = findId(Drop.Range("A2"))

    If (idRow = 0) Then
        MsgBox ("ID not found")
        End
    End If

    Drop.Range("A2:DS2").Copy
    Data.Range("A" & idRow & ":DS" & idRow).PasteSpecial xlPasteValues

    Call CopyPaste(Drop.Range("A" & idRow & ":R" & idRow), _
        Data.Range("A" & Rows.Count).End(xlUp).Offset(1, 0))

End Sub

我尝试使用建议的更新功能:

Function findId(ids As String) As Long
    Dim m
    With Sheets("Data")
        m = Application.Match(ids, .Columns("A"), 0) 'any match on id?
        'got a match?  If not get next empty cell
        If IsError(m) Then m = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    findId = CLng(m)
End Function

但是无法找到该匹配项。 匹配(IDS,)是否位于不同的表格上.columns(“ a”)

I'm struggling to get the code below to work and would really appreciate any support.

I can find the row number that contains a variable ID number - findRow which works fine.

I want to return the row number of the next available empty row if findRow = 0. I am using findNextRow to try and accomplish this, but clearly I am missing something.

Function findId(ids As String) As Integer
    Dim findRow As Range
    Dim findNextRow As Range
    
    Sheets("Data").Select
    Columns("A:A").Select
    
    Set findRow = Selection.Find(What:=ids, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        
    Set findNextRow = Selection.Data.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        
    
    If Not findRow Is Nothing Then
        findId = findRow.Row
        
    ElseIf Not findRow Is Nothing Then
        findId = findNextRow.Row
        
    Else
        findId = 0
        
            End If
    
End Function

EDIT
The function is being called on here:

Sub Import()
    Dim Drop As Worksheet
    Dim Data As Worksheet

    Set Drop = Worksheets("Drop")
    Set Data = Worksheets("Data")

    Dim idRow As Integer
    idRow = findId(Drop.Range("A2"))

    If (idRow = 0) Then
        MsgBox ("ID not found")
        End
    End If

    Drop.Range("A2:DS2").Copy
    Data.Range("A" & idRow & ":DS" & idRow).PasteSpecial xlPasteValues

    Call CopyPaste(Drop.Range("A" & idRow & ":R" & idRow), _
        Data.Range("A" & Rows.Count).End(xlUp).Offset(1, 0))

End Sub

I've tried using the updated function as suggested:

Function findId(ids As String) As Long
    Dim m
    With Sheets("Data")
        m = Application.Match(ids, .Columns("A"), 0) 'any match on id?
        'got a match?  If not get next empty cell
        If IsError(m) Then m = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    findId = CLng(m)
End Function

But can't get this to find a match. Should it matter that Match(ids, is located on a different sheet to .Columns("A")?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2025-02-08 11:28:16

尝试这样的事情:

Function findId(id As String) As Long
    Dim m
    With Sheets("Data")
        m = Application.Match(id, .Columns("A"), 0) 'any match on id?
        'got a match?  If not get next empty cell
        If IsError(m) Then m = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    findId = CLng(m)
End Function

fyi vba中没有理由在上使用integer,并且在处理范围时使用longs更安全,因为整数会溢出大约上方。 32,000

Try something like this:

Function findId(id As String) As Long
    Dim m
    With Sheets("Data")
        m = Application.Match(id, .Columns("A"), 0) 'any match on id?
        'got a match?  If not get next empty cell
        If IsError(m) Then m = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    findId = CLng(m)
End Function

FYI there's no reason in VBA to use Integer over Long, and using longs is safer when dealing with ranges, since integer will overflow above approx. 32,000

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文