|
|
发表于 2024-12-27 16:21:52
|
显示全部楼层
- 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)), "")
-
- ' 第一分支选择逻辑
- Call BasicConditionCheck(text, changedRow)
- End If
- End If
- End Sub
- Sub BasicConditionCheck(ByVal text As String, ByVal row As Long)
- ' 判断字符串中是否包含 "无缝", "钢管" 且不包含 "锌", "zinc", "galv"
- If InStr(text, "无缝") > 0 And InStr(text, "钢管") > 0 Then
- If InStr(text, "锌") = 0 And InStr(text, "zinc") = 0 And InStr(text, "galv") = 0 Then
- Cells(row, "H").Value = "无缝钢管"
-
- ' 后续判定并赋值I, J, K列
- AssignStandards_I text, row
- AssignMaterials_J text, row
- AssignDimensions_K text, row
- ElseIf InStr(text, "锌") > 0 Or InStr(text, "zinc") > 0 Or InStr(text, "galv") > 0 Then
- Cells(row, "H").Value = "无缝钢管(镀锌)"
- End If
- End If
- End Sub
- 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
- Sub AssignDimensions_K(ByVal text As String, ByVal row As Long)
- Dim pattern As String
- Dim match As Object
- Dim regex As Object
- ' 正则表达式查找形如 "Φ133x4", "Φ133x4.5", "Φ45*4.5"
- pattern = "Φ\d+[\*x]\d+\.?\d*"
-
- Set regex = CreateObject("VBScript.RegExp")
- With regex
- .Pattern = pattern
- .Global = False
- .IgnoreCase = True
- End With
- If regex.Test(text) Then
- Set match = regex.Execute(text)(0)
- Cells(row, "K").Value = match.Value
- End If
- End Sub
复制代码
此代码通过事件处理和分支判断实现了所需功能。其中:
- `Worksheet_Change` 事件函数用于监控特定列的变化,触发相应的操作。
- `BasicConditionCheck` 用于基础条件判断,并调用其他子程序对特定列进行操作。
- `AssignStandards_I` 和 `AssignMaterials_J` 用于在满足条件时填充I和J列。
- `AssignDimensions_K` 使用正则表达式查找特定格式的数据并赋值K列。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|