VBA-查找第一个空单元的行号或返回行号
我正在努力使下面的代码工作,并非常感谢任何支持。
我可以找到包含可变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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样的事情:
fyi vba中没有理由在
长
上使用integer
,并且在处理范围时使用longs更安全,因为整数会溢出大约上方。 32,000Try something like this:
FYI there's no reason in VBA to use
Integer
overLong
, and using longs is safer when dealing with ranges, since integer will overflow above approx. 32,000