Vba 第一个空单元

Vba 第一个空单元,vba,excel,Vba,Excel,我在网站上找到了不同的解决方案,但它们不能解决我的问题。以下部分是查找的结果,其中“FoundCell”地址返回到工作表。 我试图做的是将“FoundCell”地址返回到cell(1,1),其余地址直接返回到下面。我想在电子表格上打印调试行正在执行的操作 Value Found In Cell: $F$2 Value Found In Cell: $F$5 Value Found In Cell: $F$8 Value Found In Cell: $F$9 工作表“程序索引”包含A到F列。我

我在网站上找到了不同的解决方案,但它们不能解决我的问题。以下部分是查找的结果,其中“FoundCell”地址返回到工作表。 我试图做的是将“FoundCell”地址返回到cell(1,1),其余地址直接返回到下面。我想在电子表格上打印调试行正在执行的操作

Value Found In Cell: $F$2
Value Found In Cell: $F$5
Value Found In Cell: $F$8
Value Found In Cell: $F$9
工作表“程序索引”包含A到F列。我正在使用Find搜索F列中的逗号分隔字符串。当前,代码返回F列中找到字符串的单元格地址。我需要的是与F列中找到的地址相关联的A列和B列中的条目

Sub Find()

Dim SearchRange As Range
Dim FoundCells As Range
Dim FoundCell As Range
Dim Destination As Range
Dim c, d As Range
Dim Row As String


Dim FindWhat As Variant
Dim FindWhat2 As Variant


Set Destination = Sheets("Calculations").Cells(1, 1)
Set SearchRange = Sheets("Program Index").Range("F2:F1000")

Debug.Print Sheets("main").Range("F2")

Sheets("Calculations").Range("A2:A50").Clear

FindWhat = Sheets("Main").Range("F2")
FindWhat2 = "All"

Set FoundCells = FindAll(SearchRange:=SearchRange, _
                        FindWhat:=FindWhat, _
                        LookIn:=xlValues, _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByColumns, _
                        MatchCase:=False, _
                        BeginsWith:=vbNullString, _
                        EndsWith:=vbNullString, _
                        BeginEndCompare:=vbTextCompare)

If FoundCells Is Nothing Then
    Debug.Print "Value Not Found"
Else
    Set c = Sheets("Calculations").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    For Each FoundCell In FoundCells
         c.Value = FoundCell.Address   
         Set c = c.Offset(1, 0)
    Next FoundCell
End If

    Set FoundCells = FindAll(SearchRange:=SearchRange, _
                            FindWhat:=FindWhat2, _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByColumns, _
                            MatchCase:=False, _
                            BeginsWith:=vbNullString, _
                            EndsWith:=vbNullString, _
                            BeginEndCompare:=vbTextCompare)

If FoundCells Is Nothing Then
    Debug.Print "Value Not Found"
Else
    Set c = Sheets("Calculations").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    For Each FoundCell In FoundCells
         c.Value = FoundCell.Address
         Set c = c.Offset(1, 0)
    Next FoundCell
End If

End Sub
我相信我需要的改变应该发生在“FindAll”中,但是我不确定在哪里修改

If Not FoundCell Is Nothing Then
    Set FirstFound = FoundCell

    Do Until False ' Loop forever. We'll "Exit Do" when necessary.
        Include = False
        If BeginsWith = vbNullString And EndsWith = vbNullString Then
            Include = True
        Else
            If BeginsWith <> vbNullString Then
                If StrComp(Left(FoundCell.Text, Len(BeginsWith)), BeginsWith, BeginEndCompare) = 0 Then
                    Include = True
                End If
            End If
            If EndsWith <> vbNullString Then
                If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, BeginEndCompare) = 0 Then
                    Include = True
                End If
            End If
        End If
        If Include = True Then
            If ResultRange Is Nothing Then
                Set ResultRange = FoundCell
            Else
                Set ResultRange = Application.Union(ResultRange, FoundCell)
            End If
        End If
        Set FoundCell = SearchRange.FindNext(after:=FoundCell)
        If (FoundCell Is Nothing) Then
            Exit Do
        End If
        If (FoundCell.Address = FirstFound.Address) Then 'modify to find program number and description
            Exit Do
        End If

    Loop
End If

Set FindAll = ResultRange
如果不是FoundCell,则
Set FirstFound=FoundCell
直到永远“假”循环。必要时我们会“退出”。
Include=False
如果BeginsWith=vbNullString,EndsWith=vbNullString,则
Include=True
其他的
如果以vbNullString开头,则
如果StrComp(左(FoundCell.Text,Len(BeginsWith)),BeginsWith,beginedcompare)=0,则
Include=True
如果结束
如果结束
如果EndsWith vbNullString,则
如果StrComp(Right(FoundCell.Text,Len(EndsWith)),EndsWith,beginedcompare)=0,则
Include=True
如果结束
如果结束
如果结束
如果Include=True,则
如果ResultRange什么都不是,那么
Set ResultRange=FoundCell
其他的
Set ResultRange=Application.Union(ResultRange,FoundCell)
如果结束
如果结束
设置FoundCell=SearchRange.FindNext(后面:=FoundCell)
如果(FoundCell为Nothing),则
退出Do
如果结束
如果(FoundCell.Address=FirstFound.Address),则“修改以查找程序编号和说明”
退出Do
如果结束
环
如果结束
Set FindAll=ResultRange
编辑:也许这就是你要找的:

Dim c as range

If FoundCells Is Nothing Then
    Debug.Print "Value Not Found"
Else
    Set c = Sheets("Calculations").Cells(Rows.Count, 1).End(xlUp).Offset(1,0)
    For Each FoundCell In FoundCells
         Debug.Print "Value Found In Cell: " & FoundCell.Address
         c.value = FoundCell.Address()
         'add values from the same row as FoundCell
         c.offset(0, 1).value = FoundCell.EntireRow.Cells(1).value 'from colA
         c.offset(0, 2).value = FoundCell.EntireRow.Cells(2).value 'from colB
         Set c = c.offset(1,0)
    Next FoundCell
End If

要使debug.print行显示在A列第1到n行中,可以执行以下操作:

Dim FoundCells As Range, FoundCell As Range
Dim rDest As Range
Set rDest = Worksheets("Calculations").Range("A1")

'For testing
Set FoundCells = Union(Range("f2"), Range("f5"), Range("f8"), Range("f9"))

If FoundCells Is Nothing Then
        rDest.Value = "Value Not Found"
    Else
        For Each FoundCell In FoundCells
             rDest.Value = "Value Found In Cell: " & FoundCell.Address
             Set rDest = rDest(2, 1)
        Next FoundCell
    End If

好的一面这到底解决了什么?好问题,我想-我跳过了这个问题,根据标题假设OP在定位最后一个单元格时遇到了问题,并且没有真正破译出问题中的曲折线。重新阅读问题,不太清楚OP想要什么。谢谢!这就是为什么我问这个问题,我也不知道这个问题是什么,以为你抓住了我错过的东西。我知道这个问题问得不好。我需要的是来自“foundcell”的手机地址。这将返回F列中的值,我需要“FoundCell”关联行中A列和B列中的值。我可以更改“c.value”偏移量以检索所需的列数据吗?谢谢你的帮助。你可以做任何你需要的改变,但是很难从问题中准确地说出你的最终目标是什么。
Dim FoundCells As Range, FoundCell As Range
Dim rDest As Range
Set rDest = Worksheets("Calculations").Range("A1")

'For testing
Set FoundCells = Union(Range("f2"), Range("f5"), Range("f8"), Range("f9"))

If FoundCells Is Nothing Then
        rDest.Value = "Value Not Found"
    Else
        For Each FoundCell In FoundCells
             rDest.Value = "Value Found In Cell: " & FoundCell.Address
             Set rDest = rDest(2, 1)
        Next FoundCell
    End If