我是Vba的新手。我已经花了一整天的时间来写这段代码:
Sub ComandsCompactVisualization()
Dim x, i As Integer
Dim CellToAnalyse As Range
x = 2
i = 0
For i = 0 To 5 Step 1
Set CellToAnalyse = Worksheets("Comandi").Cells(x + i, 2)
If Not CellToAnalyse.Font.ColorIndex = 2 Then
Worksheets("Comandi").Rows("x+i:2").Hidden = True
End If
Next i
End Sub我试图隐藏单元格(x+i,2)中没有红色文本的所有行。我就快到了但是..。行似乎不接受作为内容行(“x+i:2”)。
我得到运行时错误13“类型不匹配”。
如果我用行(“2 :2”)替换它的内容,那么第2行就会被删除,但是我不能再隐藏列2中没有红色文本的所有其他行。
想法?
发布于 2014-05-18 03:51:28
引号“像这样”之间的任何东西都只是一个字符串。要对x执行算术运算,您需要先执行此操作,然后将其连接到字符串的另一部分。如下所示:
.Rows((x + i) & ":2")顺便说一下,红色不是3..?
发布于 2014-05-18 16:54:11
Sub ComandsCompactVisualization()
Dim x as long, i As Long 'You must declare ALL variables (x was as variant in your code)
Dim CellToAnalyse As Range
dim WS as Worksheet
'x = 2 'if x is always the same value, no need to calculate it each loop
'i = 0 'numbers are initialized to be 0, strings to be "", boolean to be false.
set WS=Sheets("Commandi")
For i = 0 To 5 ' Step 1
Set CellToAnalyse = WS.Cells(2 + i, 2)
If CellToAnalyse.Font.ColorIndex <> 2 Then
CellToAnalyse.entirerow.hidden=true
' WS.Rows(2+i).entirerow.hidden = true is the same result
End If
Next i
End Subhttps://stackoverflow.com/questions/23715410
复制相似问题