|
老师好: 下面的这段vba代码是正确的,我测试了没有任何问题,但是他只能在人工输入的数字该vba才能识别,但是我加到用公式提取的数字里面就无法识别了,也没有报错,这是什么问题?帮我分析下 谢谢
Sub Main222()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' 假设工作表名为Sheet1
' 定义数组来存储每行中符合条件的列索引(相对于段起始列的偏移量)
Dim ar(2 To 204, 1 To 10) As Integer
Dim segments As Variant
segments = Array(15, 24, 30, 39, 45, 54) ' 定义每段的起始和结束列号
Dim i As Integer, j As Integer, k As Integer, segStart As Integer, segEnd As Integer
' 遍历每个段
For k = LBound(segments) To UBound(segments) Step 2 ' Step 2 因为我们有两个元素定义一个段(起始和结束)
segStart = segments(k)
segEnd = segments(k + 1)
' 重置数组(可选,如果每次处理前都要清除旧数据)
' For i = LBound(ar, 1) To UBound(ar, 1)
' For j = LBound(ar, 2) To UBound(ar, 2)
' ar(i, j) = 0
' Next j
' Next i
' 遍历行和列(当前段)
For i = 2 To 204
Dim foundCount As Integer
foundCount = 0
For j = segStart To segEnd
If ws.Cells(i, j).Interior.Color = RGB(255, 0, 0) Then ' 假设您检查的颜色是红色
foundCount = foundCount + 1
ar(i, foundCount) = j - segStart + 1 ' 存储相对于段起始列的偏移量
End If
Next j
Next i
' 绘制连接线
For j = 1 To 10 ' 假设每行最多找到10个符合条件的列
For i = 2 To 203 ' 绘制到第203行,因为第17行没有下一行可以连接
If ar(i, j) > 0 And ar(i + 1, j) > 0 Then
' 计算起点和终点的坐标
Dim colOffset As Integer
colOffset = segments(k) - 1 ' 获取当前段的起始列号(Excel中的列号减1)
startX = ws.Cells(i, ar(i, j) + colOffset).Left + ws.Cells(i, ar(i, j) + colOffset).Width / 2
startY = ws.Cells(i, ar(i, j) + colOffset).Top + ws.Cells(i, ar(i, j) + colOffset).Height / 2
endX = ws.Cells(i + 1, ar(i + 1, j) + colOffset).Left + ws.Cells(i + 1, ar(i + 1, j) + colOffset).Width / 2
endY = ws.Cells(i + 1, ar(i + 1, j) + colOffset).Top + ws.Cells(i + 1, ar(i + 1, j) + colOffset).Height / 2
' 添加直线
ws.Shapes.AddLine startX, startY, endX, endY
End If
Next i
Next j
Next k
End Sub
|
|