简体   繁体   English

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

[英]How can I use a range in Excel VBA with shifting columns?

I am working in Excel VBA. 我在Excel VBA工作。 I am running a regression. 我正在回归。 The data in column 'O' is the dependent variable. 列“O”中的数据是因变量。 This regression is being run 8 times using a do while loop. 使用do while循环运行此回归8次。

The problem that I am having is as follows. 我遇到的问题如下。 On each pass through, I am deleting a column. 每次通过时,我都会删除一列。 This of course moves column O to column N on the first pass, column M on the second pass, etc. 这当然会在第一次通过时将列O移动到第N列,在第二次通过时将列移动到第M列等等。

I need to find a way using a range to do this WITHOUT using an if then statement. 我需要找到一种方法来使用范围来执行此操作而不使用if then语句。 Can anyone shed light on this? 任何人都可以阐明这一点吗?

You can access the cells as: 您可以访问单元格:

.Cells(RowNumber,ColumnNumber)

Everytime you delete a column, you can decrement the column number by one to achieve your objective. 每次删除列时,都可以将列号减1以实现目标。

named ranges will move with the selection. 命名范围将随选择一起移动。 you then use this name in your code. 然后在代码中使用此名称。 eg MyData is defined as Sheet1!O:O . 例如, MyData被定义为Sheet1!O:O If a column is deleted, then Excel will alter the address of the named range automatically. 如果删除了列,则Excel将自动更改指定范围的地址。

Example: 例:

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

also, note that the address doesn't change, BUT the cell it references does, as you will see the value $O$1 in both O1 and N1 另外,请注意,地址不会改变, 它引用的单元格会改变,因为你会在O1和N1中看到值$O$1

Hard to imagine exactly what you are doing without seeing the code, but you might consider using the Range Offset function. 如果没有看到代码,很难想象你正在做什么,但你可以考虑使用Range Offset函数。 See how the value of your range changes as you delete columns before, after and on this range. 在此范围之前,之后和之后删除列时,查看范围值的变化情况。 Then design a small statement that can handle these situations. 然后设计一个可以处理这些情况的小语句。

For instance: 例如:

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

IS the data in a table? 是表格中的数据吗? Or at least have a header? 或者至少有一个标题? You could use match if there is a header but no table, =MATCH("Column Header",1:1,0) will return the number for the column that contains that header. 如果有标题但没有表格,您可以使用匹配, =MATCH("Column Header",1:1,0)将返回包含该标题的列的编号。

在此输入图像描述

Otherwise if you have headers and a table you could simply call it with TableName[HeaderName] will pull all data from the column in the table under the header you specify. 否则,如果您有标题和表格,您只需使用TableName[HeaderName]调用它TableName[HeaderName]将从您指定的标题下的表格中的列中提取所有数据。 Notice below how all data below the specified Header is highlighted. 请注意下面如何突出显示指定标题下的所有数据。

在此输入图像描述

Either way the data will always remain constant to your information as long as the header remains in the row you specify OR the table. 无论哪种方式,只要标题保留在您指定的行或表中,数据将始终保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM