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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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 VBA中使用带移位列的范围?_Vba_Excel - Fatal编程技术网

如何在Excel VBA中使用带移位列的范围?

如何在Excel VBA中使用带移位列的范围?,vba,excel,Vba,Excel,我在Excel VBA中工作。我正在运行一个回归。“O”列中的数据是因变量。使用do while循环将此回归运行8次 我遇到的问题如下。每次通过时,我都会删除一列。这当然会在第一次传递时将列O移动到列N,在第二次传递时将列M移动到列N,以此类推 我需要找到一种方法,在不使用if-then语句的情况下使用range来实现这一点。有人能解释一下吗 您可以通过以下方式访问单元格: .Cells(RowNumber,ColumnNumber) 你每次删除一个列,你可以将列号减去一个以达到你的目标。很难

我在Excel VBA中工作。我正在运行一个回归。“O”列中的数据是因变量。使用do while循环将此回归运行8次

我遇到的问题如下。每次通过时,我都会删除一列。这当然会在第一次传递时将列O移动到列N,在第二次传递时将列M移动到列N,以此类推


我需要找到一种方法,在不使用if-then语句的情况下使用range来实现这一点。有人能解释一下吗

您可以通过以下方式访问单元格:

.Cells(RowNumber,ColumnNumber)

<>你每次删除一个列,你可以将列号减去一个以达到你的目标。

很难想象你在没有看到代码的情况下做了什么,但是你可以考虑使用范围偏移函数。在删除此范围之前、之后和之上的列时,查看范围值的变化。然后设计一个能够处理这些情况的小语句

例如:

Option Explicit

Sub x()

    Dim rngCurrent As Range
    Dim wksCurrent As Worksheet

    Set wksCurrent = ActiveSheet

    ' Example 1: The range in question is in a column after the column to delete

    Set rngCurrent = wksCurrent.Cells(20, 31)

    Debug.Print rngCurrent.Address '$AE$20

    wksCurrent.Range("O:O").Delete

    Debug.Print rngCurrent.Address '$AD$20  (it decreased automatically)

    Set rngCurrent = rngCurrent.Offset(0, -1) ' Reset column to previous column, in this case 30

    Debug.Print rngCurrent.Address '$AC$20


    ' Example 2: The range in question is a column before the deleted column
    Set rngCurrent = wksCurrent.Cells(20, 3)

    Debug.Print rngCurrent.Address '$C$20

    wksCurrent.Range("O:O").Delete

    Debug.Print rngCurrent.Address '$C20  (no change since the deleted column was after this range)

    Set rngCurrent = rngCurrent.Offset(0, -1) ' Reset column to previous column, in this case 30

    Debug.Print rngCurrent.Address '$B20


    ' Example 3: The range in question is the same as the deleted column
    Set rngCurrent = wksCurrent.Cells(20, 15)

    Debug.Print rngCurrent.Address '$O$20

    wksCurrent.Range("O:O").Delete

    'Debug.Print rngCurrent.Address 'Error: Object Required, the cell pointed no longer exists

    'Set rngCurrent = rngCurrent.Offset(0, -1) ' Error: Object Required

    'Debug.Print rngCurrent.Address '$O19  ' Error: Object Required

    ' Example 4: The range in question is the same as the deleted column. Avoid the error.
    Set rngCurrent = wksCurrent.Cells(20, 15)

    Debug.Print rngCurrent.Address '$O$20

    wksCurrent.Range("O:O").Delete

    Set rngCurrent = wksCurrent.Range("O20").Offset(0, -1) ' Refer to the worksheet to reset the range, instead of to the range itself

    Debug.Print rngCurrent.Address '$N20

End Sub

命名范围将随选择移动。然后在代码中使用此名称。e、 g.
MyData
定义为
Sheet1!O:O
。如果删除列,Excel将自动更改命名区域的地址

例如:

ActiveWorkbook.Names.Add Name:="TempRange", RefersTo:="=Sheet1!O1"
Range("TempRange").Value = Range("TempRange").Address
Range("A:A").Delete
Range("TempRange").Value = Range("TempRange").Address

另外,请注意,地址没有改变,但它引用的单元格会改变,因为您将看到O1和N1中的值
$O$1
是表中的数据吗?或者至少有一个标题?如果有标题但没有表,可以使用match,
=match(“列标题”,1:1,0)
将返回包含该标题的列的编号

否则,如果您有标题和一个表,您可以简单地用
TableName[HeaderName]
调用它,它将从表中指定标题下的列中提取所有数据。请注意下面指定标题下的所有数据是如何高亮显示的


无论哪种方式,只要标题保留在您指定的行或表中,数据将始终保持不变。

您是否可以共享一些代码,以便我们能够了解您到底想要实现什么?
,而无需使用if语句
???这是杂志任务吗?数据在表格中吗?或者至少有一个标题?如果有标题但没有表,可以使用match,
=match(“列标题”,1:1,0)
将返回包含该标题的列的编号。否则,如果您有标题和一个表,您可以简单地用
TableName[HeaderName]
调用它,它将从表中指定标题下的列中提取所有数据。只要标题保留在指定的行或表中,这将始终保持信息不变。