Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
Excel 如何使用变量引用范围?_Excel_Vba - Fatal编程技术网

Excel 如何使用变量引用范围?

Excel 如何使用变量引用范围?,excel,vba,Excel,Vba,我想引用跨列的一系列单元格:B:C,然后是E:M(跳过D)。我想复制单元格并将其粘贴到另一个工作表中 我有一个用于下一个循环,循环中有行数变量iT。如何使用变量选择它们 这将选择包括D在内的整个范围 Sheet4.Range("B" & iT & ":C" & iT, "E" & iT & ":M" & iT).Select 如果要使用Cells方法,我尝试了Cell

我想引用跨列的一系列单元格:B:C,然后是E:M(跳过D)。我想复制单元格并将其粘贴到另一个工作表中

我有一个
用于下一个
循环,循环中有行数变量
iT
。如何使用变量选择它们

这将选择包括D在内的整个范围

Sheet4.Range("B" & iT & ":C" & iT, "E" & iT & ":M" & iT).Select

如果要使用Cells方法,我尝试了
Cells()

使用cellsmethod()进行子复制
暗列数等于长列数
暗行数等于长行数
RowNumber=1'在此处输入所需的行号
使用此工作簿。工作表(“表4”)
对于ColumnNumber=2到5,步骤3'将范围(B1:C1)复制到范围(I1:J1)中,然后将范围(E1:F1)复制到范围(L1:M1)中
.Range(单元格(行编号,列编号),单元格(行编号,列编号+1))。复制工作表(“Sheet4”).Range(.Cells(行编号,列编号+7),.Cells(行编号,列编号+8))
下一栏号
以
端接头

如果要使用Cells方法

使用cellsmethod()进行子复制
暗列数等于长列数
暗行数等于长行数
RowNumber=1'在此处输入所需的行号
使用此工作簿。工作表(“表4”)
对于ColumnNumber=2到5,步骤3'将范围(B1:C1)复制到范围(I1:J1)中,然后将范围(E1:F1)复制到范围(L1:M1)中
.Range(单元格(行编号,列编号),单元格(行编号,列编号+1))。复制工作表(“Sheet4”).Range(.Cells(行编号,列编号+7),.Cells(行编号,列编号+8))
下一栏号
以
端接头
试试这个

   Sht.range("A:M").copy AnotherWorkbook.sheets("YourSheet").range("A1")
   AnotherWorkbook.sheets("YourSheet").range("D:D").delete
试试这个

   Sht.range("A:M").copy AnotherWorkbook.sheets("YourSheet").range("A1")
   AnotherWorkbook.sheets("YourSheet").range("D:D").delete
简论 复制值、格式、公式

Sub NonContiguousRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    ' Optionally:
    'Set cols = Union(Sheet1.Columns("B:C"), Sheet1.Columns("E:M"))
    Dim rRng As Range
    Set rRng = Intersect(Sheet1.Rows(iT), cols)
    rRng.Copy Sheet2.Cells(1, "A")
    ' This will also work:
    'Dim ColumnsCount As Long
    'ColumnsCount = getColumnsCount(cols)
    'rRng.Copy Sheet2.Cells(1, "A").Resize(, ColumnsCount)
    ' This will NOT work:
    'Sheet2.Cells(1, "A").Resize(, ColumnsCount).Value = rRng.Value
End Sub

Function getColumnsCount( _
    aRange As Range) _
As Long
    If Not aRange Is Nothing Then
        Dim rng As Range
        For Each rng In aRange.Areas
            getColumnsCount = getColumnsCount + rng.Columns.Count
        Next rng
    End If
End Function
Sub TESTgetRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    Dim Data As Variant
    Data = getRow(cols, iT)
    Sheet2.Cells(1, "A").Resize(, UBound(Data) - LBound(Data) + 1).Value = Data
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values at the intersection of a range
'               and one of its worsheet's rows, in an array.
' Remarks:      Supports non-contiguous ranges.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getRow( _
    aRange As Range, _
    Optional ByVal aRow As Long = 1) _
As Variant
    If Not aRange Is Nothing Then
        Dim rRng As Range
        Set rRng = Intersect(aRange, aRange.Worksheet.Rows(aRow))
        If Not rRng Is Nothing Then
            With CreateObject("Scripting.Dictionary")
                Dim rng As Range
                Dim cel As Range
                Dim n As Long
                For Each rng In rRng.Areas
                    For Each cel In rng.Cells
                        n = n + 1
                        .Item(n) = cel.Value
                    Next cel
                Next rng
                getRow = .Items
            End With
        Else
        ' Row range is empty ('Nothing').
        End If
    Else
        ' Range is empty ('Nothing').
    End If
End Function
复制值

Sub NonContiguousRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    ' Optionally:
    'Set cols = Union(Sheet1.Columns("B:C"), Sheet1.Columns("E:M"))
    Dim rRng As Range
    Set rRng = Intersect(Sheet1.Rows(iT), cols)
    rRng.Copy Sheet2.Cells(1, "A")
    ' This will also work:
    'Dim ColumnsCount As Long
    'ColumnsCount = getColumnsCount(cols)
    'rRng.Copy Sheet2.Cells(1, "A").Resize(, ColumnsCount)
    ' This will NOT work:
    'Sheet2.Cells(1, "A").Resize(, ColumnsCount).Value = rRng.Value
End Sub

Function getColumnsCount( _
    aRange As Range) _
As Long
    If Not aRange Is Nothing Then
        Dim rng As Range
        For Each rng In aRange.Areas
            getColumnsCount = getColumnsCount + rng.Columns.Count
        Next rng
    End If
End Function
Sub TESTgetRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    Dim Data As Variant
    Data = getRow(cols, iT)
    Sheet2.Cells(1, "A").Resize(, UBound(Data) - LBound(Data) + 1).Value = Data
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values at the intersection of a range
'               and one of its worsheet's rows, in an array.
' Remarks:      Supports non-contiguous ranges.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getRow( _
    aRange As Range, _
    Optional ByVal aRow As Long = 1) _
As Variant
    If Not aRange Is Nothing Then
        Dim rRng As Range
        Set rRng = Intersect(aRange, aRange.Worksheet.Rows(aRow))
        If Not rRng Is Nothing Then
            With CreateObject("Scripting.Dictionary")
                Dim rng As Range
                Dim cel As Range
                Dim n As Long
                For Each rng In rRng.Areas
                    For Each cel In rng.Cells
                        n = n + 1
                        .Item(n) = cel.Value
                    Next cel
                Next rng
                getRow = .Items
            End With
        Else
        ' Row range is empty ('Nothing').
        End If
    Else
        ' Range is empty ('Nothing').
    End If
End Function
简论 复制值、格式、公式

Sub NonContiguousRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    ' Optionally:
    'Set cols = Union(Sheet1.Columns("B:C"), Sheet1.Columns("E:M"))
    Dim rRng As Range
    Set rRng = Intersect(Sheet1.Rows(iT), cols)
    rRng.Copy Sheet2.Cells(1, "A")
    ' This will also work:
    'Dim ColumnsCount As Long
    'ColumnsCount = getColumnsCount(cols)
    'rRng.Copy Sheet2.Cells(1, "A").Resize(, ColumnsCount)
    ' This will NOT work:
    'Sheet2.Cells(1, "A").Resize(, ColumnsCount).Value = rRng.Value
End Sub

Function getColumnsCount( _
    aRange As Range) _
As Long
    If Not aRange Is Nothing Then
        Dim rng As Range
        For Each rng In aRange.Areas
            getColumnsCount = getColumnsCount + rng.Columns.Count
        Next rng
    End If
End Function
Sub TESTgetRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    Dim Data As Variant
    Data = getRow(cols, iT)
    Sheet2.Cells(1, "A").Resize(, UBound(Data) - LBound(Data) + 1).Value = Data
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values at the intersection of a range
'               and one of its worsheet's rows, in an array.
' Remarks:      Supports non-contiguous ranges.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getRow( _
    aRange As Range, _
    Optional ByVal aRow As Long = 1) _
As Variant
    If Not aRange Is Nothing Then
        Dim rRng As Range
        Set rRng = Intersect(aRange, aRange.Worksheet.Rows(aRow))
        If Not rRng Is Nothing Then
            With CreateObject("Scripting.Dictionary")
                Dim rng As Range
                Dim cel As Range
                Dim n As Long
                For Each rng In rRng.Areas
                    For Each cel In rng.Cells
                        n = n + 1
                        .Item(n) = cel.Value
                    Next cel
                Next rng
                getRow = .Items
            End With
        Else
        ' Row range is empty ('Nothing').
        End If
    Else
        ' Range is empty ('Nothing').
    End If
End Function
复制值

Sub NonContiguousRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    ' Optionally:
    'Set cols = Union(Sheet1.Columns("B:C"), Sheet1.Columns("E:M"))
    Dim rRng As Range
    Set rRng = Intersect(Sheet1.Rows(iT), cols)
    rRng.Copy Sheet2.Cells(1, "A")
    ' This will also work:
    'Dim ColumnsCount As Long
    'ColumnsCount = getColumnsCount(cols)
    'rRng.Copy Sheet2.Cells(1, "A").Resize(, ColumnsCount)
    ' This will NOT work:
    'Sheet2.Cells(1, "A").Resize(, ColumnsCount).Value = rRng.Value
End Sub

Function getColumnsCount( _
    aRange As Range) _
As Long
    If Not aRange Is Nothing Then
        Dim rng As Range
        For Each rng In aRange.Areas
            getColumnsCount = getColumnsCount + rng.Columns.Count
        Next rng
    End If
End Function
Sub TESTgetRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    Dim Data As Variant
    Data = getRow(cols, iT)
    Sheet2.Cells(1, "A").Resize(, UBound(Data) - LBound(Data) + 1).Value = Data
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values at the intersection of a range
'               and one of its worsheet's rows, in an array.
' Remarks:      Supports non-contiguous ranges.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getRow( _
    aRange As Range, _
    Optional ByVal aRow As Long = 1) _
As Variant
    If Not aRange Is Nothing Then
        Dim rRng As Range
        Set rRng = Intersect(aRange, aRange.Worksheet.Rows(aRow))
        If Not rRng Is Nothing Then
            With CreateObject("Scripting.Dictionary")
                Dim rng As Range
                Dim cel As Range
                Dim n As Long
                For Each rng In rRng.Areas
                    For Each cel In rng.Cells
                        n = n + 1
                        .Item(n) = cel.Value
                    Next cel
                Next rng
                getRow = .Items
            End With
        Else
        ' Row range is empty ('Nothing').
        End If
    Else
        ' Range is empty ('Nothing').
    End If
End Function

Sheet4.范围(“B”&iT&“:C”&iT,“E”&iT&“:M”&iT)。选择
逗号位置错误。尝试
Sheet4.范围(“B”&iT&“:C”&iT&“、E”&iT&“:M”&iT)。选择
BTW您不需要选择范围。您也可以直接使用该范围。你可能想读一读《谢谢》。工作得很好。。我将阅读您的建议。
Sheet4.范围(“B”&iT&“:C”&iT,“E”&iT&“:M”&iT)。选择
您的逗号位置错误。尝试
Sheet4.范围(“B”&iT&“:C”&iT&“、E”&iT&“:M”&iT)。选择
BTW您不需要选择范围。您也可以直接使用该范围。你可能想读一读《谢谢》。工作得很好。。我会读你的建议。