在Excel中添加一个新列而不更改VB代码

我创build一个VB程序来自动更新组项目的甘特图。 但现在球队想要添加一个新的柱子。 问题是添加一个新的列将改变我的代码,使其不可用。 行可以在不更改代码的情况下添加,但是如果添加新的列,我将不得不更新所有的代码。 我怎样才能添加一个列没有编写我的VB代码?

Private Sub Worksheet_Change(ByVal Target As Range) Dim StartDate_Row As Integer Dim Total_Weeks As Integer Dim Date_Week_Column As Integer Dim Number_of_Weeks As Integer Dim Date_Week_Column_Color As Integer StartDate_Row = 10 Date_Week_Column = 8 Range("H9:AN25").Interior.Color = xlNone Do For Total_Weeks = 1 To 33 If Cells(StartDate_Row, 5).Value = Cells(8, Date_Week_Column).Value Then Date_Week_Column_Color = Date_Week_Column For Number_of_Weeks = 1 To Cells(StartDate_Row, 6).Value If Cells(StartDate_Row, 7).Value = 25 Then Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(204, 255, 299) End If If Cells(StartDate_Row, 7).Value = 50 Then Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(153, 255, 204) End If If Cells(StartDate_Row, 7).Value = 75 Then Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(102, 255, 178) End If If Cells(StartDate_Row, 7).Value = 100 Then Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(50, 200, 100) End If If Cells(StartDate_Row, 7).Value = 0 Then Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(149, 179, 215) End If Date_Week_Column_Color = Date_Week_Column_Color + 1 Next Number_of_Weeks End If Date_Week_Column = Date_Week_Column + 1 Next Total_Weeks Date_Week_Column = 8 StartDate_Row = StartDate_Row + 1 Loop While (Not IsEmpty(Cells(StartDate_Row, 5))) End Sub 

汤姆的build议是一种可能性,但是对于用户来说,每一次macros观运行都会有很多麻烦。

可能的技术1

我从不用数字来引用列或行,原因有二:

  • 列和行可能会像您发现的那样移动。
  • 有人读你的代码必须知道第5列或第6列的含义。

最好使用常量。 例如:

 Const ColXxxx As Long = 5 Const RowYyyy As Long = 8 If Cells(StartDate_Row, ColXxxx).Value = Cells(RowYyyy, Date_Week_Column).Value Then 

我不知道你的行和列是什么,所以我用ColXxxxRowYyyy作为名字。 你会用名字来代替我的名字,告诉读者行和列是什么。

这样的代码需要一点时间来写,但(1)它是自我logging和(2)如果列或行移动,您只需要更改Const语句来解决问题。

注意:我已经使用数据typesLong 。 数据typesInteger定义了一个16位variables,它需要在32位和64位计算机上进行特殊(慢速)处理。

可能的技术2

技巧1要求用户告诉程序员他们想添加一列或移动一行。 如果他们在修改工作表之前忘记了告诉程序员,macros可能会损坏工作表而无法修复。

另一种技术是search第1行的已知列标题并logging下哪里。 也许你有一个专栏标题“开始date”。 “开始date”可以在macros的一次运行的第5列中,在下一次的第6列中可以是“开始date”,并且您的代码将正常工作。

如果这个技术很有趣,我会添加示例代码。

简而言之,答案就是你必须改变你的代码。 随着代码变得越来越复杂,这成为越来越大的问题。 因此,尽可能dynamic地考虑用于指定单元格位置的方法。

例如,您可以在脚本中构build一个用户input,允许用户指定包含所需信息的列。 这样,如果用户在表中添加更多的列,脚本仍然可以正确运行(只要用户select正确的列)。

 'ask user to select the column containing the data which they would like to utilise Dim colRng As Range On Error Resume Next Set colRng = Application.InputBox("Select a cell from anywhere in the column which contains the programme start dates.", Default:="", Type:=8) On Error GoTo 0 If colRng Is Nothing Then 'notify user that the process cannot continue as selection was invalid MsgBox "You must select a cell to enable this process to continue!" 'exit the sub Exit Sub End If 'output the column number (as selected by the user) debug.print colRng.column 

希望有所帮助!

如果您在工作表上定义了命名范围(使用Formulas > Define Name ),则即使插入或删除行和列,该名称也会更新为指向相同的单元格。

在你的代码中,你可以使用(例如) MySheet.[MyRangeName].Row获取名为MyRangeName的范围的第一行的行号,依此类推。