[英]Type mismatch error when running a macro and the sheet has specific code
My Program: I have a program that imports data and splits it up into different sheets within the same workbook. 我的程序:我有一个程序可以导入数据并将其拆分为同一工作簿中的不同工作表。 Once the data has been split the purpose is for the end user to be able to go into those sheets and update the data. 数据分割后,目的是使最终用户能够进入这些工作表并更新数据。 If the user updates the data, that specific cell will change colors. 如果用户更新数据,则该特定单元格将更改颜色。 So I have created a code specifically for those sheets to automatically update when the end user enters the data. 因此,我创建了一个专门用于这些工作表的代码,以在最终用户输入数据时自动更新。
My Problem: The split macro works perfectly on its own, the specific sheet code works perfectly on its own. 我的问题:split宏完全可以独立运行,特定的工作表代码可以完全独立运行。 However, when I go to run the whole program together - vba proceeds a typed mismatched error. 但是,当我一起运行整个程序时-vba会出现类型错误的错误。
This is the specific sheet code 这是特定的工作表代码
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Target.Select
Range("E" & ActiveCell.Row).Select
ActiveCell.Interior.ColorIndex = 24
If Target.Value < 2000000 Then 'ERROR OCCURS HERE WHEN I CLICK MY SPLIT BUTTON
If Target.Column = 16 Then
Range("R" & Target.Row) = "Low"
End If
End If
If Target.Value >= 2000000 And Target.Value < 10000000 Then
If Target.Column = 16 Then
Range("R" & Target.Row) = "Medium"
End If
End If
If Target.Value >= 10000000 Then
If Target.Column = 16 Then
Range("R" & Target.Row) = "High"
End If
End If
If Target.Value < 600000 Then
If Target.Column = 17 Then
Range("R" & Target.Row) = "Low"
End If
End If
If Target.Value >= 600000 And Target.Value < 3000000 Then
If Target.Column = 17 Then
Range("R" & Target.Row) = "Medium"
End If
End If
If Target.Value >= 3000000 Then
If Target.Column = 17 Then
Range("R" & Target.Row) = "High"
End If
End If
Application.EnableEvents = True
End Sub
Good programming practice states: if some piece of code reapeats at least two times, move it into separate procedure/function. 良好的编程习惯指出:如果某段代码至少重复两次,请将其移至单独的过程/函数中。
Playing with Excel VBA, to not ever use Select statement, unless it's necessary. 除非有必要,否则不要使用Excel VBA进行选择 。
Improved procedure could look like: 改进的过程如下所示:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Range("E" & Target.Row).Interior.ColorIndex = 24
Range("R" & Target.Row) = GetStatus(Target.Value)
Application.EnableEvents = True
End Sub
Function GetStatus(ByVal curVal As Integer, ByVal iCol As Integer) As String
Dim sRetVal As String
Select Case iCol
Case 16
Select Case curVal
Case Is < 2000000
sRetVal = "Low"
Case 2000000 To 10000000
sRetVal = "Medium"
Case Is >= 10000000
sRetVal = "High"
End Select
Case 17
Select Case curVal
Case Is < 600000
sRetVal = "Low"
Case 600000 To 299999.9999
sRetVal = "Medium"
Case Is >= 3000000
sRetVal = "High"
End Select
End Select
End Function
But, why to force doors wide open? 但是,为什么要强迫门敞开呢? Use conditional formatting . 使用条件格式 。
Cheers, 干杯,
Maciej Maciej
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.