向上移动到某个细胞范围

时间:2017-01-17 09:36:41

标签: vba excel-vba excel

我正在尝试运行一个程序,允许我在Microsoft Excel中的某个时间看到大学里的哪些房间是免费的。

我遇到的问题是在确定一个空类槽之后:

  • 如何将其重新编码为所有教室的名称(所有名称都在第2行)
  • 并存储此值。

我尝试过抵消,但这对我不起作用。

我已添加Sample Data以进一步澄清

Public Sub EXq3()

Dim rnR1 As Range, roomNum As Integer

Const rooms = 13 ' Counter amount 
Set rgR1 = ActiveCell.Offset(0, 1)
timeSolt = InputBox("What time")  ' asks user what time to enter

Cells.find(What:=timeSolt, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
          xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
         , SearchFormat:=False).Activate    ' search and find code

For counter = 1 To rooms
     If rgR1.Value = "" Then roomNum = rgR1.Offset(Range(2, rgR1.Value)) ' attempt at getting it to go to range 2
     rgR1.Activate
     Set rgR1 = rgR1.Offset(0, 1)
Next counter
MsgBox roomNum

End Sub

3 个答案:

答案 0 :(得分:0)

你可能会去"去2"意思是"去第2行"我是对的?如果是,这是您的解决方案:

For counter = 1 To rooms
     If rgR1.Value = "" Then 
         roomNum =  Cells(2, rgR1.Column).Value
     End If 
     rgR1.Activate
     Set rgR1 = rgR1.Offset(0, 1)
Next counter

修改

好的,所以我假设您在A列中有一些时间选项,而B列中的Room 1,C列中的Room 2等有一些值。我已经重构了您的代码以摆脱移动的活动单元格。它在A列中找到一些时间,并使用此时间选项检查行中是否有一些空单元格,并返回包含此房间数量的消息。

我的测试表:

table

代码:

Public Sub EXq3()

Dim rnR1 As Range, roomNum As String, rooms As Integer Dim timeSolt As String

rooms = 13 ' Counter amount timeSolt = InputBox("What time") ' asks user what time to enter

Set rnR1 = ActiveSheet.Columns("A:A").Find(What:=timeSolt, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) ' search and find code

If rnR1 Is Nothing Then
    MsgBox "Something is wrong with Input." 
Else
    For col = 2 To rooms + 1
        If Cells(rnR1.Row, col).Value = "" Then
            roomNum = Cells(2, col).Value
            MsgBox roomNum
        End If
    Next col 
End If

End Sub

所以,我。即当您在弹出窗口中键入17时,结果将是" Room 4"和" 10号房间"。

答案 1 :(得分:0)

无需Set rgR1 = ActiveCell.Offset(0, 1),您只需搜索整个工作表中TimeSlot处输入的InputBox

此外,最好远离ActivateActiveCell,而是使用引用的Range

由于有可能在一段时间内有一些可用空间,您需要将其存储为数组,并提高每次匹配=""时找到的房间索引。

下面的代码注释中有更多解释。

<强>代码

Option Explicit

Public Sub EXq3()

Dim rnR1 As Range, roomNum As Variant, TimeSlot
Dim FindRng As Range, i As Integer, Counter As Integer

Const rooms = 13 ' Counter amount
ReDim roomNum(1 To 1000) ' init Rooms avaialable array to a large size
i = 1 '<-- reset Rooms Array index

TimeSlot = InputBox("What time")  ' asks user what time to enter

Set FindRng = Cells.Find(What:=TimeSlot, After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
                    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
                    , SearchFormat:=False)  ' search and find TimeSlot

If Not FindRng Is Nothing Then '<-- was able to find the timeslot in the worksheet
    For Counter = 1 To rooms
        If Cells(FindRng.Row, Counter + 1).Value = "" Then '<-- add 1 to counter since starting from Column B
            roomNum(i) = Cells(2, Counter + 1).Value '<-- save room number inside the array
            i = i + 1
        End If
    Next Counter

    ReDim Preserve roomNum(1 To i - 1) ' <-- resize array back to number of available rooms found

    ' loop through all available rooms in the array, and show a msgbox for each one
    For i = 1 To UBound(roomNum)
        MsgBox "Room number " & roomNum(i) & " is available at " & TimeSlot
    Next i
Else  '<-- could bot find the timeslot in the worksheet
    MsgBox "Couldn't find " & TimeSlot & " inside the worksheet!"
End If

End Sub

答案 2 :(得分:0)

你可以试试这个:

Public Sub EXq3()
    Dim rnR1 As Range
    Dim rooms As Integer
    Dim timeSolt As String, roomNum As String

    rooms = 13 ' Counter amount
    With ActiveSheet
        Do
            timeSolt = Application.InputBox("What time", "Input time", Type:=2)
            If timeSolt = CStr(False) Then Exit Sub '<--| exit if user cancels the dialogbox
            Set rnR1 = .Columns("A:A").SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues).Find(What:=timeSolt, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False) ' search and find code
            If Not rnR1 Is Nothing Then Exit Do
            MsgBox timeSolt & " is not a vaild time" & vbCrLf & vbCrLf & "please try again"
        Loop

        With .Range(rnR1.Offset(, 1), .Cells(rnR1.Row, .Columns.count).End(xlToLeft))
            If WorksheetFunction.CountBlank(.Cells) = 0 Then
                MsgBox "Sorry! No rooms left for the input time"
            Else
                roomNum = .Parent.Cells(2, .SpecialCells(xlCellTypeBlanks).Cells(1, 1).Column)
                MsgBox "First room available at " & timeSolt & " is room " & roomNum
            End If
        End With
    End With
End Sub