-1

哈哈!我会难倒你的 Excel 大师。;-D

我想根据一些分隔文本设置选择范围边界。

如果我这样做 Cells.Find 会搜索整个工作表并找到多个实例。我想要找到的可能是分隔符的第三或第四个实例。实际上它在特定的列 B 中。但是它是一个不连续的范围,并且该列中的实际搜索开始是几百个单元格。

如何在该列中搜索并设置我的可重用范围开始变量,设置为分隔符单元格(不包括分隔符单元格)?我试过这个:

Dim selectionStart As Range, selectionEnd As Range
Dim currentCell As Range, dataRange As Range
Dim lastRow As Range, insertRows As Range, destinationCell As Range

Range("b1", Range("b65536").End(xlUp)).Select

Set selectionStart = Selection.Find(What:="<-RANGE START->", After:=ActiveCell, LookIn _
    :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
   xlNext, MatchCase:=False, SearchFormat:=False)

它选择范围但不设置变量。

我正在尝试所有这些令人讨厌的大东西,这样我就可以在没有太多眼睛疲劳的情况下看到它们的作用。不需要优雅。

TIA

4

1 回答 1

1

你是这个意思吗?

Option Explicit

Sub test()

Dim selectionStart As Range, selectionEnd As Range
Dim currentCell As Range, dataRange As Range
Dim lastRow As Range, insertRows As Range, destinationCell As Range
Dim rngtoSearch As Range
Dim foundValue As Variant
Dim foundAddress As String
Dim foundRow As Long

With sheetWhatever 'change to whatever sheet codename required
    Set rngtoSearch = .Range("b1", .Range("b65536").End(xlUp))

    Set selectionStart = rngtoSearch.Find(What:="<-RANGE START->", LookIn _
        :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
       xlNext, MatchCase:=False, SearchFormat:=False)

    'check it actually found a range
    If Not selectionStart Is Nothing Then
        'If found set the variable
        foundValue = selectionStart.Value 'set as value
        foundAddress = selectionStart.Address 'set as address string
        foundRow = selectionStart.Row ' set as row
    End If

End With


End Sub
于 2012-09-19T00:23:14.027 回答