我想知道是否有人知道如何选择矩形范围的值?此范围不会修复。对于这个特殊的例子,它将以矩形形式选择B5-G7然后它将设置条件格式以添加某些颜色。
我尝试过代码,但是在这部分给我一个错误
ActiveSheet.Cells(colorrow & "2", _
ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select
想知道有谁知道为什么?会很感激的!
我试图写出一些编码。
我的代码如下:
Sub Macro2()
Dim thevaluestocopy As Variant, colorCell as Range, colorrow as Long, thefirstcolorrow as Long
colorrow = 1
Do
Set colorCell = Sheets("Sheet1").Cells(colorrow, 1)
'check for test1-test6 if its around do nothing, else goes to the next row and next column
If colorCell = "test1" Or colorCell = "test2" Or colorCell = "test3" _
Or colorCell = "test4" Or colorCell = "test5" Or colorCell = "test6" _ Then
'Do nothing
Else
thefirstcolorrow = Sheets("Sheet1").Cells(colorrow, 2)
'This statement gives me the error.. not sure why it cant work
ActiveSheet.Cells(colorrow & "2", _
ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select
Exit Do
End If
colorrow = colorrow + 1
Loop
'add colors into cell
ActiveCell.Select
Selection.FormatConditions.AddColorScale ColorScaleType:=3
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 8109667
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValuePercentile
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 8711167
End With
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 7039480
End With
End Sub
答案 0 :(得分:0)
我不确定您的意思,但如果您想在工作表中引用矩形范围,可以使用以下内容:
With Sheet1
.Range(.Range("B5"), .Range("G7")).Select
End With
那将在名为Sheet1的对象中选择B5:G7。或者,您可以使用工作表名称:
With Sheets("Sheet 1")
.Range(.Range("B5"), .Range("G7")).Select
End With
请注意,Sheet1.Name很可能等于“Sheet 1”,即Sheet1是对象,“Sheet 1”是对象的名称。如果你理解(或学会理解)这种区别,你可能会为自己做一个很好的服务。
编辑: 变化
ActiveSheet.Cells(colorrow & "2", _ ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select
到
ActiveSheet.Range(ActiveSheet.Cells(colorrow & "2"), ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select
答案 1 :(得分:0)
我找到了办法。并希望与其他人分享..
Sub Macro2()
Dim colorCell As Range, colorrow As Long, thefirstcolorrow As Long, colorrow1 As Long, colorCell1 As Range, nvalue As Long
Dim thelastcolorRow As Long, n As Long, LastColtocolor As Long
colorrow = 1
LastColtocolor = Sheets("Sheet1").Cells("1" & Columns.Count).End(xlToLeft).Column
Do
Set colorCell = Sheets("Sheet1").Cells(colorrow, 1)
'Check if cell holds any value of test1 - test6, etc...
If colorCell = "test1" Or colorCell = "test2" Or colorCell = "test3" _
Or colorCell = "test4" Or colorCell = "test5" Or colorCell = "test6" _ Then
'Do nothing
Else
thefirstcolorrow = colorrow
Exit Do
End If
colorrow = colorrow + 1
Loop
colorrow1 = 1
Do
'Look for last row that has values
Set colorCell1 = Sheets("Sheet1").Cells(colorrow1, 1)
If colorCell1 = "" Then
thelastcolorRow = colorrow1
Exit Do
End If
colorrow1 = colorrow1 + 1
Loop
For nvalue = 1 To colorrow1 - 1 - colorrow
Sheets("Sheet1").Range(Cells(thefirstcolorrow, 2), Cells(thefirstcolorrow + nvalue, LastColtocolor)).Select
Next nvalue
End sub