我想根据特定条件在excel表单个单元格中输入多个值,如果工作簿中有多个工作表,那么如果任何以TC开头的工作表中包含颜色,那么我要去在另一个工作表中输入Excel工作簿的自述部分中的信息。我的代码的问题是它没有显示包含着色的唯一工作表...假设工作表“TC_1”和“TC_3”在任何单元格中包含颜色,然后它将输出显示为“; TC_3; TC_3; TC_3;”虽然这里的预期输出是“TC_1; TC_3”。
这里是代码:
Sub ErrorInSheet()
Dim Row
Dim Names As String
Names = ""
For Row = 2 To tsheet.UsedRange.Rows.Count
For Chkcol = 1 To tsheet.UsedRange.Columns.Count
If tsheet.Cells(Row, Chkcol).Interior.ColorIndex = 3 Then
Names = Names & ";" & tsheet.Name
End If
Next
Next Row
Sheets("Read Me").Cells(13, 5).Value = Names
End Sub
Sub iterateSheets()
For Each sheet1t In Worksheets
If InStr(1, sheet1t.Name, "TC") Then
Set tsheet = sheet1t
Call ErrorInSheet
End If
Next
End Sub
答案 0 :(得分:2)
我认为这对你有用 - 我测试了它并为我工作。
Sub FindErrors()
Dim sht As Worksheet, cl As Range, shtNames As String
shtNames = vbNullString
For Each sht In Worksheets
If Left$(sht.Name, 2) = "TC" Then
For Each cl In sht.UsedRange.Cells
If cl.Interior.ColorIndex = 3 Then
shtNames = IIf(shtNames = vbNullString, sht.Name, shtNames & ";" & sht.Name)
End If
Next cl
End If
Next sht
Worksheets("Read Me").Cells(13, 5) = shtNames
End Sub
注意:
Left$
,但如果您愿意,可以使用InStr
IIF
语句阻止您获得领先;
Sub
中,但如果您愿意,可以将其拆分