VBA。用于设置范围

时间:2015-01-29 02:23:30

标签: vba find range

我是Excel的新手,但有一点似乎超级强大的是使用.Find来命名范围。不幸的是,我之后才看到人们将它与消息框和类似的东西一起使用。我试图做的事情有点复杂。

我正试图找到一个名为“状态”的单元格。在' rec'的某个地方。工作表。在找到它之后,我需要在“状态”之上的一个单元格中设置一个范围。细胞增加六个细胞。

这是我到目前为止所做的:

Dim rec As Worksheet

Set rec = ThisWorkbook.Sheets("Rec")

Dim State As String

Dim MoveHeadersUp As Range

State = "State"


Set MoveHeadersUp = rec.Rows.Find(What:=State, LookIn:=xlValues, _
Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If MoveHeadersUp Is Nothing Then

MsgBox "Uh-Oh. Column A is missing a header titled 'State'. You better create one!"

Else

???????

 End Sub

2 个答案:

答案 0 :(得分:0)

Else

'''THIS IS THE ANSWER
Set spaceabove = Range(MoveHeadersUp.Offset(IIf(MoveHeadersUp.Row > 1, -1, 0), 0).Address, MoveHeadersUp.Offset(IIf(MoveHeadersUp.Row > 6, -6, (MoveHeadersUp.Row - 1) * -1), 0).Address)

Debug.Print spaceabove.Address

End If
'''I was trying to delete the rows 
spaceabove.EntireRow.Delete

End Sub

答案 1 :(得分:0)

Dim SpaceAbove as Range 

If MoveHeadersUp.Row>6 Then
    Set SpaceAbove = MoveHeadersUp.Offset(-6,0).Resize(6,1)
    Debug.print SpaceAbove.Address
Else
    msgbox "Can't move up 6 rows from 'MoveHeadersUp' !"
End If