Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 选择散射区域旁边的单元格_Vba_Excel - Fatal编程技术网

Vba 选择散射区域旁边的单元格

Vba 选择散射区域旁边的单元格,vba,excel,Vba,Excel,我在VBA中选择一系列单元格时遇到困难。我已经有一个分散的单元格区域,但我正在尝试将该区域中每个单元格右侧的三个单元格添加到选择中 到目前为止,我已经尝试: SelectedRange(“Hi”)。调整大小(1,4)。选择,运行时会出现错误;我假设它不起作用,因为SelectedRange是一个范围而不是单元格 SelectedRange()搜索输入字符串并返回与输入匹配的每个单元格的范围,该范围是此代码的修改版本: 谢谢。您尚未透露所选范围引用的确切内容;希望这不是一个不连续细胞的结合 在中使

我在VBA中选择一系列单元格时遇到困难。我已经有一个分散的单元格区域,但我正在尝试将该区域中每个单元格右侧的三个单元格添加到选择中

到目前为止,我已经尝试:
SelectedRange(“Hi”)。调整大小(1,4)。选择
,运行时会出现错误;我假设它不起作用,因为SelectedRange是一个范围而不是单元格

SelectedRange()搜索输入字符串并返回与输入匹配的每个单元格的范围,该范围是此代码的修改版本:

谢谢。

您尚未透露所选范围引用的确切内容;希望这不是一个不连续细胞的结合

在中使用SelectedRange,您可以访问其尺寸

对于不连续的单元排列,需要循环并调整每个单元的大小,同时使用将其添加到集合中


有关摆脱依靠选择和激活来实现目标的更多方法,请参阅。

使用SelectedRange更新。不连续单元格的并集是否存在问题?不连续单元格的并集可能具有具有不同列数的单元格组(或区域)。每一项都必须单独处理。见上文。
Function SelectedRange(ByVal fnd As String) As Range

Dim FirstFound As String
Dim FoundCell As Range, rng As Range
Dim myRange As Range, LastCell As Range

Set myRange = ActiveSheet.UsedRange
Set LastCell = myRange.Cells(myRange.Cells.Count)
Set FoundCell = myRange.find(what:=fnd, after:=LastCell)

'Test to see if anything was found
  If Not FoundCell Is Nothing Then
    FirstFound = FoundCell.Address
  Else
    GoTo NothingFound
  End If

Set rng = FoundCell

'Loop until cycled through all unique finds
  Do Until FoundCell Is Nothing
    'Find next cell with fnd value
      Set FoundCell = myRange.FindNext(after:=FoundCell)

    'Add found cell to rng range variable
      Set rng = Union(rng, FoundCell)

    'Test to see if cycled through to first found cell
      If FoundCell.Address = FirstFound Then Exit Do

  Loop

  Set SelectedRange = rng

'Report Out Message
  MsgBox SelectedRange.Cells.Count & " cell(s) were found containing: " & fnd

Exit Function

'Error Handler
NothingFound:
  MsgBox "No cells containing: " & fnd & " were found in this worksheet"

End Function
dim SelectedRange as range
set SelectedRange = range("B2:D4")
with SelectedRange
    set SelectedRange = .resize(.rows.count, .columns.count + 3)
end with
SelectedRange.select    'might want to get away from this method
Dim a As Long, selectedRange As Range, tmpRange As Range
Set selectedRange = Selection
Set tmpRange = selectedRange.Range("A1")
With selectedRange
    For a = 1 To .Areas.Count
        With .Areas(a)
            Set tmpRange = Union(tmpRange, .Cells.Resize(.Rows.Count, .Columns.Count + 3))
        End With
    Next a
End With
Set selectedRange = tmpRange
selectedRange.Select   'might want to get away from this method