简体   繁体   English

在Excel 2007中指定VBA宏使用的范围

[英]Specifying the range used by VBA macro in excel 2007

I have a sheet with a grid of about 10 columns. 我有一张约有10列网格的工作表。 11th column in the sheet does some calculations using the data in those 10 columns and saves the result. 工作表的第11列使用这10列中的数据进行了一些计算,并保存了结果。 After adding a new column in between the initial 10 columns, my range is changed. 在最初的10列之间添加新列之后,更改了我的范围。 So, now I have to post the result in 12th column. 因此,现在我必须将结果发布到第12列。

Private Sub Worksheet_Change(ByVal Target As Range)
   ' Check if change is made to the correct range of cells.
   Dim Results As Variant

   'Defaults while sheet only used in Rotterdam
   mGravityUnit = UnitDensity
   mVolumeUnit = UnitCubicMetres
   mTempUnit = UnitCelcius


With Application
    If Not (.Intersect(Target, Range("BargeOpenTemp")) Is Nothing And .Intersect(Target, Range("BargeOpenDens")) Is Nothing And .Intersect(Target, Range("BargeCloseTemp")) Is Nothing And .Intersect(Target, Range("BargeCloseDens")) Is Nothing And .Intersect(Target, Range("BargeGrossVolOpen")) Is Nothing And .Intersect(Target, Range("BargeGrossVolClose")) Is Nothing) Then
        ' Change in Volume Temp or Density
        Dim ThisRow As Integer, VCF As Double
        ThisRow = Target.Row
        If Not (Cells(ThisRow, 3) = "" Or Cells(ThisRow, 11) = "") Then
            Results = VCF_Calculation(Cells(ThisRow, 3), mTempUnit, Cells(ThisRow, 11), mGravityUnit, mVolumeUnit)
            '         Results = VCF_Calc(Cells(ThisRow, 3), Cells(ThisRow, 10), mGravityUnit, 15, 0)
            Cells(ThisRow, 12) = Results
            'Range("BargeVCFOpen").Value = Results(1)
            'Range("BargeVCFClose").Value = Results(1)
        Else
            Cells(ThisRow, 12) = ""
        End If
        'If closing reading has already been entered then re-calculate the clocing VCF as theoretical Density may have changed.
        If ThisRow < 25 Then
            ThisRow = Target.Row + 20
            If Not (Cells(ThisRow, 3) = "" Or Cells(ThisRow, 11) = "") Then
                Results = VCF_Calculation(Cells(ThisRow, 3), mTempUnit, Cells(ThisRow, 11), mGravityUnit, mVolumeUnit)
                '             Results = VCF_Calc(Cells(ThisRow, 3), Cells(ThisRow, 10), mGravityUnit, 15, 0)
                Cells(ThisRow, 12) = Results
                'Range("BargeVCFOpen").Value = Results(1)
                'Range("BargeVCFClose").Value = Results(1)
            Else
                Cells(ThisRow, 12) = ""
            End If
        End If
    End If
End With
End Sub

This is the existing code, that I changed, and incremented the column indexes. 这是我更改的现有代码,并增加了列索引。

Sadly, this does not work, as the line below throws an out of range exception. 遗憾的是,这行不通,因为下面的行引发了超出范围的异常。

If Not (.Intersect(Target, Range("BargeOpenTemp")) Is Nothing

Clearly, I have to update the target somewhere in the excel. 显然,我必须在excel中的某个地方更新目标。 But I can't seem to find the same anywhere. 但我似乎在任何地方都找不到。

Never mind. 没关系。 Found how to do that change. 找到了如何做的改变。 Just required selecting the range in the top left corner, and updating the cell numbers. 只需选择左上角的范围,并更新单元格编号即可。

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

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