【发布时间】:2019-03-07 16:13:06
【问题描述】:
我的代码一直从下一个单元格运行到下一个列循环,有人可以帮忙吗?主要目的是使用 Input Rng 突出显示整个范围,并在每一列中读取一个红色值,并将寄存器编号返回到输出列的偏移量左侧对于每个单元格的每一列。
ObtainSCEs()
Dim InputRng As Range
Dim OutputRng As Range
Dim Rng As Range
xTitleID = "ObtainSCE"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("select data Range:", xTitleID, InputRng.Address, Type:=8)
Set OutputRng = Application.InputBox("select output Range:", xTitleID, Type:=8)
Dim C As Long
C = 0
Dim B As Long
B = InputRng.Columns.Count
Dim A As Long
A = 1
Dim Cell As Range
Dim Column As Range
For Each Column In InputRng
For Each Cell In Column
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng.Offset(0, 0)) > 0 Then
OutputRng.Offset(0, C).Value = OutputRng.Offset(0, C).Value & ","
OutputRng.Offset(0, C).Value = OutputRng.Offset(0, C) & Cell.Offset(0, -1 - C).Value
Else
OutputRng.Offset(0, C) = Cell.Offset(0, -1 - C).Value
End If
End If
Next Cell
Next Column
End Sub
我有第二个代码,我尝试使用不同的方法,但它继续运行第一列。如下图所示
Sub ObtainSCEs()
Dim InputRng As Range
Dim OutputRng As Range
Dim Rng As Range
xTitleID = "ObtainSCE"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("select data Range:", xTitleID, InputRng.Address, Type:=8)
Set OutputRng = Application.InputBox("select output Range:", xTitleID, Type:=8)
Dim C As Long
C = 0
Dim B As Long
B = InputRng.Columns.Count
Dim A As Long
A = 0
Dim Cell As Range
Dim Column As Range
For n = 1 To 5
InputRng.Columns(n).Select
For Each Cell In InputRng.Columns.Cells
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng.Offset(0, 0)) > 0 Then
OutputRng.Offset(0, C) = OutputRng.Offset(0, C).Value & ","
OutputRng.Offset(0, C) = OutputRng.Offset(0, C) & Cell.Offset(0, -1 - C).Value
Else
OutputRng.Offset(0, C) = Cell.Offset(0, -1 - C).Value
End If
End If
Next Cell
C = C + 1
Next n
End Sub
这是我目前用于执行此操作的代码,现在我手动选择所有 6 列,但我想选择 1 个整个范围,然后将该范围拆分为相应的列。
Sub GetSCE()
Application.Volatile True
Dim Rng As Range
Dim InputRng1 As Range, OutputRng As Range
Dim InputRng2 As Range, InputRng3 As Range
Dim InputRng4 As Range, InputRng5 As Range
Dim InputRng6 As Range
Dim Cell As Range
Dim sev1 As Integer
sev1 = 1
Dim sev2 As Integer
sev2 = 2
Dim sev3 As Integer
sev3 = 3
Dim sev4 As Integer
sev4 = 4
Dim sev5 As Integer
sev5 = 5
Dim sev6 As Integer
sev6 = 6
xTitleID = "ObtainSCE"
Set InputRng1 = Application.Selection
Set InputRng1 = Application.InputBox("Select Data Range1:", xTitleID, InputRng1.Address, Type:=8)
Set InputRng2 = Application.InputBox("Select Data Range2:", xTitleID, Type:=8)
Set InputRng3 = Application.InputBox("Select Data Range3:", xTitleID, Type:=8)
Set InputRng4 = Application.InputBox("Select Data Range4:", xTitleID, Type:=8)
Set InputRng5 = Application.InputBox("Select Data Range5:", xTitleID, Type:=8)
Set InputRng6 = Application.InputBox("Select Data Range6:", xTitleID, Type:=8)
Set OutputRng1 = Application.InputBox("Select Starting Cells:", xTitleID, Type:=8)
Set OutputRng2 = Application.InputBox("Select Starting Cells:", xTitleID, Type:=8)
Set OutputRng3 = Application.InputBox("Select Starting Cells:", xTitleID, Type:=8)
Set OutputRng4 = Application.InputBox("Select Starting Cells:", xTitleID, Type:=8)
Set OutputRng5 = Application.InputBox("Select Starting Cells:", xTitleID, Type:=8)
Set OutputRng6 = Application.InputBox("Select Starting Cells:", xTitleID, Type:=8)
For Each Cell In InputRng1
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng1) > 0 Then OutputRng1.Value = OutputRng1.Value & ","
OutputRng1.Value = OutputRng1.Value & Cell.Offset(0, -sev1).Value
Else
End If
Next Cell
For Each Cell In InputRng2
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng2) > 0 Then OutputRng2.Value = OutputRng2.Value & ","
OutputRng2.Value = OutputRng2.Value & Cell.Offset(0, -sev2).Value
Else
End If
Next Cell
For Each Cell In InputRng3
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng3) > 0 Then OutputRng3.Value = OutputRng3.Value & ","
OutputRng3.Value = OutputRng3.Value & Cell.Offset(0, -sev3).Value
Else
End If
Next Cell
For Each Cell In InputRng4
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng4) > 0 Then OutputRng4.Value = OutputRng4.Value & ","
OutputRng4.Value = OutputRng4.Value & Cell.Offset(0, -sev4).Value
Else
End If
Next Cell
For Each Cell In InputRng5
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng5) > 0 Then OutputRng5.Value = OutputRng5.Value & ","
OutputRng5.Value = OutputRng5.Value & Cell.Offset(0, -sev5).Value
Else
End If
Next Cell
For Each Cell In InputRng6
If Cell.DisplayFormat.Interior.ColorIndex = 3 Then
If Len(OutputRng6) > 0 Then OutputRng6.Value = OutputRng6.Value & ","
OutputRng6.Value = OutputRng6.Value & Cell.Offset(0, -sev6).Value
Else
End If
Next Cell
End Sub
如果有人需要更清晰的图片,这就是我正在尝试做的事情 Picture of what im trying to do
感谢您的帮助
【问题讨论】:
-
无法清楚地理解您的目标。但是 C 始终为零,如果找到红细胞,您希望 c 值是多少?
-
c 应该在每列完成后 +1 到原始值,但我现在无法到达那里,所以我故意将其省略,我将它编辑到第二个代码中,这样你就可以看到它应该在哪里是,哪一部分你不明白,我试着解释得更清楚,
-
您需要单独遍历每一行还是只需要遍历每个单元格?此外,遍历单元格中的每个单元格是没有意义的...对于 InputRng 中的每个列都不是您要经过的列。有了这个,你就可以遍历 InputRng 中的每个单元格。
-
我需要遍历所需范围的每一列中的每个单元格,但所需的返回值是第一列中的行标签(我假设您已经看到了图片)。哦,如果我想遍历 InputRng 中的每一列应该是什么?
-
我在工作中看不到图片,被屏蔽了。