[英]VBA Collect information from found column through loop in Excel
我提前表示歉意,我从未真正使用过VBA,以后可能也永远不会使用。
一位朋友要求我根据提供的ID创建一个编辑文档按钮。 我有一个循环,筛选数据库工作表以匹配ID。
我的目标是,一旦找到ID,就从该找到的行中收集所有数据。 我在想是否可以使用Range(i +“ 1”)。Value ...到Range(i +“ 10”)。Value来收集数据。 明显的问题似乎是这些列是字母而不是数字的预设。
我想知道是否可以帮助进入VBA / Excel,以及在If语句中可以做什么以收集希望从找到的行中获取的数据/值。
这就是我现在所拥有的。
Sub Load_Edit()
Sheets("SearchEstimation").Select
Dim i As Integer, intValueToFind As Integer
intValueToFind = Range("B5").Value
Sheets("EstimationDB").Select
For i = 1 To 500 ' VBA column.length?
If Cells(i, 1).Value = intValueToFind Then
Sheets("EstimationDB").Select
Range(i + "1").Value
Exit Sub
End If
Next i
' This MsgBox will only show if the loop completes with no success
MsgBox ("Value not found in the range!")
End Sub
我不得不猜测一下,因为您没有指定“收集数据/值”,所以我假定了一个简单的.copy。 我还假设在数据库之间没有空单元格。 那这个呢:
Sub Load_Edit()
Dim wssearch as worksheet, wsDB as worksheet
Set wssearch = Sheets("SearchEstimation")
Set wsDB = Sheets("EstimationDB")
Dim i As Integer, intValueToFind As Integer
intValueToFind = wssearch.Range("B5").Value
Do
i = i+1
If wsDB.Cells(i, 1).Value = intValueToFind Then
wsDB.Rows(i).copy 'Copy the whole row
Exit Sub
End If
'Break the loop if the cell is empty
Loop until wsDB.Cells(i,1).Value = ""
' This MsgBox will only show if the loop completes with no success
MsgBox ("Value not found in the database!")
End Sub
您可以用Foreach代替我的Do ... Loop。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.