2014-10-02 64 views
1

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

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 
+0

你不能。您修改代码。 – 2014-10-02 10:17:16

+0

它实际上可能只是在插入一些额外的列后调整'Date_Week_Column = 8'来匹配右列。另外'如果单元格(StartDate_Row,7)'第7列'当前是硬编码的 - 也可能需要调整。 – 2014-10-02 10:32:41

+0

你可以给你的数据库在Excel中的截图。比重新创建场景更容易 – ZAT 2014-10-02 10:36:15

回答

0

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

E.g.您可以在脚本中构建用户输入,以便用户指定包含所需信息的列。这样,如果用户向表中添加更多列,该脚本仍将正确运行(只要用户选择了正确的列)。

'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 

希望有帮助!

1

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

可能的技术1

我从来指的行或列的数字,原因有二:当你发现

  • 列和行可以移动。
  • 阅读代码的人必须知道第5列或第6列的含义。

最好使用常量。佛例如:

​​

我不知道你的行和列,所以我已经使用ColXxxxRowYyyy姓名。你会用名字替换我的名字,告诉读者行和列是什么。

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

注:我已经使用数据类型Long。数据类型Integer定义了一个16位变量,它需要在32位和64位计算机上进行特殊(慢速)处理。

可能的技术2

技术1需要用户知道他们要添加一列或移动行的程序员。如果他们忘记在修改工作表的runnibg宏之前告诉程序员,该宏可能会损坏工作表而无法修复。

另一种技术是在行1中搜索知道的列标题并记录存在的位置。也许你有一个专栏标题“开始日期”。 “开始日期”可以在宏的一次运行的第5列中,在下一次的第6列中可以是“开始日期”,并且您的代码将正常工作。

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

0

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

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

相关问题