VBA 使用多行范围中的单个行

假设我正在创建一个临时范围变量,如下所示

With wsProj
    Set projStartCell = .Range("D8")
    Set startCell = .Columns(StartCellSearchColumn).Find(What:=TargetText,LookIn:=xlValues,_
                     LookAt:=xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=True,SearchFormat:=False)
    
    Set tempRangeEnd = projStartCell.End(xlToRight).Offset(1,0)
    Set tempRange = .Range(startCell,tempRangeEnd)        
    
    ' insert two news rows and ensure they have no fill
    tempRange.Insert Shift:=xlDown,CopyOrigin:=xlFormatFromLeftOrAbove
    Set insertedRange = tempRange.Offset(-2,0)
    Call ClearRangeOfFill(insertedRange)

所以现在我有了我想要的正确范围,我需要分别格式化该范围内的两行中的每一行,因为它们具有不同的格式。

你能单独格式化每一行吗?即为范围的第一行插入的Range.Row(1)(是的,但这显然不起作用) 或者我是否必须为每一行创建一个临时范围,然后对其进行格式化并为下一行再次执行?

daohaoa 回答:VBA 使用多行范围中的单个行

Range.Row 是一个 Long Property,它提供指定范围的第一行,不接受参数。示例:

MsgBox insertedRange.Row 'What row does the insertedRange start on?

Range.Rows 是一个 Range Property,它提供一个代表范围中指定行的对象,并接受单行的数字参数或多行的文本参数。示例:

insertedRange.Rows(1).Interior.ColorIndex = 3 'Make the first row Red
insertedRange.Rows("3:5").Interior.ColorIndex = 5 'Make the third,fourth & fifth rows Blue

因此,您应该使用 insertedRange.Rows(1),而不是 insertedRange.Row(1)

(这是一个容易犯的错误,同样适用于 Range.ColumnRange.Columns。如果你想要完整的行/列,你需要添加 .EntireRow property 或 { {1}} 个属性)

本文链接:https://www.f2er.com/2623.html

大家都在问