|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
考虑写一个excel工作表change的事件
如果行数大于3,当列数为B,C,D,E的单元格内容发生改变时候,
首先清空该行的H,I,J,K的单元格的数据
将变更单元格行的B,C,D,E的单元格的内容合并为一个字符串
对这个字符串进行分析,
分支选择如下
1.如果字符串包含以下字段组合,"无缝","钢管",且不含"锌"或者"zinc"或者"galv",将该单元格所在行的H列单元格赋值为"无缝钢管",此处是后续判断的基础,作为程序设计标记为"判断1.1"
在"判断1.1"的基础上增加判断,
如果字符串包含"3405",将该单元格所在行的I列单元格赋值为"SH/T3405";
如果字符串包含"36.10",将该单元格所在行的I列单元格赋值为"ASME B36.10";
如果字符串包含"36.19",将该单元格所在行的I列单元格赋值为"ASME B36.19";
如果字符串包含"20553"且含有"a",将该单元格所在行的I列单元格赋值为"HG/T20553(Ⅰa)";
如果字符串包含"20553"且含有"b",将该单元格所在行的I列单元格赋值为"HG/T20553(Ⅰb)";
如果字符串包含"20553"且含有"Ⅱ",将该单元格所在行的I列单元格赋值为"HG/T20553(Ⅱ)";
如果字符串包含"17395"且含有"Ⅰ",将该单元格所在行的I列单元格赋值为"GB/T17395(Ⅰ)";
如果字符串包含"17395"且含有"Ⅱ",将该单元格所在行的I列单元格赋值为"GB/T17395(Ⅱ)";
如果字符串包含"17395"且含有"Ⅲ",将该单元格所在行的I列单元格赋值为"GB/T17395(Ⅲ)";
在"判断1.1"的基础上增加判断,
如果字符串包含"20"且含有"8163",将该单元格所在行的J列单元格赋值为"20-GB/T8163";
如果字符串包含"20"且含有"9948",将该单元格所在行的J列单元格赋值为"20-GB/T9948";
如果字符串包含"20"且含有"3087",将该单元格所在行的J列单元格赋值为"20-GB/T3087";
如果字符串包含"20"且含有"5310",将该单元格所在行的J列单元格赋值为"20G-GB/T5310";
如果字符串包含"15Cr"且不含有"9948",将该单元格所在行的J列单元格赋值为"15CrMoG-GB/T5310";
如果字符串包含"12Cr",将该单元格所在行的J列单元格赋值为"12Cr1MoVG-GB/T5310";
如果字符串包含"15CrMo"且含有"9948",将该单元格所在行的J列单元格赋值为"15CrMo-GB/T9948";S30403-GB/T14976
如果字符串包含"30403",将该单元格所在行的J列单元格赋值为"S30403-GB/T14976";
如果字符串包含"316",将该单元格所在行的J列单元格赋值为"S31603-GB/T14976";
如果字符串包含"310",将该单元格所在行的J列单元格赋值为"S31008-GB/T14976";
如果字符串包含"2205",将该单元格所在行的J列单元格赋值为"S22053-GB/T14976"
如果字符串包含"304"且含有"13296",将该单元格所在行的J列单元格赋值为"S30408-GB/T13296"
如果字符串包含"304"且含有"312",将该单元格所在行的J列单元格赋值为"TP304-A312"
如果字符串包含"304且不含有包含"30403",将该单元格所在行的J列单元格赋值为"S30408-GB/T14976";
如果字符串包含以下字段组合,"无缝","钢管",且含"锌"或者"zinc"或者"galv",将该单元格所在行的H列单元格赋值为"无缝钢管(镀锌)"
要求该代码以VBA写,且重复得用sub,function进行封装简化,保证后续程序重复调用,并且充分得写注释
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changedRow As Long
Dim text As String
' 监控范围:B, C, D, E 列
If Not Intersect(Target, Me.Range("B:E")) Is Nothing Then
changedRow = Target.Row
' 如果行数大于3
If changedRow > 3 Then
' 清空该行的H, I, J, K单元格的数据
Me.Range("H" & changedRow & ":K" & changedRow).ClearContents
' 合并B, C, D, E列内容为一个字符串
text = Join(Application.Transpose(Application.Transpose(Me.Range("B" & changedRow & ":E" & changedRow).Value)), "")
' 第一个判断:无缝钢管且不含锌
If CheckBasicCondition(text, False) Then
Me.Cells(changedRow, "H").Value = "无缝钢管"
' 后续条件分析并赋值I, J列
AssignStandards_I text, changedRow
AssignMaterials_J text, changedRow
End If
' 第二个判断:无缝钢管且含锌
If CheckBasicCondition(text, True) Then
Me.Cells(changedRow, "H").Value = "无缝钢管(镀锌)"
End If
End If
End If
End Sub
Function CheckBasicCondition(ByVal text As String, ByVal containsZinc As Boolean) As Boolean
If InStr(text, "无缝") > 0 And InStr(text, "钢管") > 0 Then
If containsZinc Then
CheckBasicCondition = (InStr(text, "锌") > 0 Or InStr(text, "zinc") > 0 Or InStr(text, "galv") > 0)
Else
CheckBasicCondition = (InStr(text, "锌") = 0 And InStr(text, "zinc") = 0 And InStr(text, "galv") = 0)
End If
Else
CheckBasicCondition = False
End If
End Function
Sub AssignStandards_I(ByVal text As String, ByVal row As Long)
Select Case True
Case InStr(text, "3405") > 0
Cells(row, "I").Value = "SH/T3405"
Case InStr(text, "36.10") > 0
Cells(row, "I").Value = "ASME B36.10"
Case InStr(text, "36.19") > 0
Cells(row, "I").Value = "ASME B36.19"
Case InStr(text, "20553") > 0
If InStr(text, "a") > 0 Then
Cells(row, "I").Value = "HG/T20553(Ⅰa)"
ElseIf InStr(text, "b") > 0 Then
Cells(row, "I").Value = "HG/T20553(Ⅰb)"
ElseIf InStr(text, "Ⅱ") > 0 Then
Cells(row, "I").Value = "HG/T20553(Ⅱ)"
End If
Case InStr(text, "17395") > 0
If InStr(text, "Ⅰ") > 0 Then
Cells(row, "I").Value = "GB/T17395(Ⅰ)"
ElseIf InStr(text, "Ⅱ") > 0 Then
Cells(row, "I").Value = "GB/T17395(Ⅱ)"
ElseIf InStr(text, "Ⅲ") > 0 Then
Cells(row, "I").Value = "GB/T17395(Ⅲ)"
End If
End Select
End Sub
Sub AssignMaterials_J(ByVal text As String, ByVal row As Long)
Select Case True
Case InStr(text, "20") > 0 And InStr(text, "8163") > 0
Cells(row, "J").Value = "20-GB/T8163"
Case InStr(text, "20") > 0 And InStr(text, "9948") > 0
Cells(row, "J").Value = "20-GB/T9948"
Case InStr(text, "20") > 0 And InStr(text, "3087") > 0
Cells(row, "J").Value = "20-GB/T3087"
Case InStr(text, "20") > 0 And InStr(text, "5310") > 0
Cells(row, "J").Value = "20G-GB/T5310"
Case InStr(text, "15Cr") > 0 And InStr(text, "9948") = 0
Cells(row, "J").Value = "15CrMoG-GB/T5310"
Case InStr(text, "12Cr") > 0
Cells(row, "J").Value = "12Cr1MoVG-GB/T5310"
Case InStr(text, "15CrMo") > 0 And InStr(text, "9948") > 0
Cells(row, "J").Value = "15CrMo-GB/T9948"
Case InStr(text, "30403") > 0
Cells(row, "J").Value = "S30403-GB/T14976"
Case InStr(text, "316") > 0
Cells(row, "J").Value = "S31603-GB/T14976"
Case InStr(text, "310") > 0
Cells(row, "J").Value = "S31008-GB/T14976"
Case InStr(text, "2205") > 0
Cells(row, "J").Value = "S22053-GB/T14976"
Case InStr(text, "304") > 0 And InStr(text, "13296") > 0
Cells(row, "J").Value = "S30408-GB/T13296"
Case InStr(text, "304") > 0 And InStr(text, "312") > 0
Cells(row, "J").Value = "TP304-A312"
Case InStr(text, "304") > 0 And InStr(text, "30403") = 0
Cells(row, "J").Value = "S30408-GB/T14976"
End Select
End Sub
以上的VBA代码通过Worksheet_Change事件实现了所需的功能。代码将事件处理程序封装在各自的Sub和Function中,便于重复调用和简化代码结构。注释和函数命名提供了逻辑的清晰划分,可以帮助理解和维护代码。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|