2017-01-17 8 views
0

私は、大学のどの部屋がMicrosoft Excelの特定の時間に無料であるかを確認できるプログラムを実行しようとしています。特定のセルの範囲に移動

私は空のクラススロットを識別した後、私が午前問題は次のとおりです。

  • 、それはすべてのクラスの部屋の名前に巻き戻ることをどのようにdoIiコード(すべての名称は、行2である)
  • これの値を格納します。

私はオフセットを試みましたが、それは私にとっては役に立たないでしょう。

私はおそらく、「2の範囲に行く」ことでは、「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 
+0

をやってみたいです。 – Prisoner

+0

私はあなたが配列とループを使用することをお勧めしたい!それははるかに読みやすく効率的です!;) – R3uK

+0

'rgR1.Offset(Range(2、rgR1.Value))'の意味は分かりません。あなたは何を達成しようとしていますか?あなたの投稿にも "すべてのクラスルームの名前にバックアップするにはどうすればいいですか(すべての名前は範囲2にあります)" - どこで定義し、 'Set Range2'ですか? –

答えて

0

を追加していますか?そうならば、これはあなたのソリューションです:

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 

EDIT

オクラホマので、あなたが列Bのルーム1、ルーム2の列Aでいくつかの時間オプション、およびいくつかの値のを持っていることを私は仮定します私はあなたのコードをリファクタリングして移動するアクティブなセルを取り除きました。列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 

だから、私は。 e。ポップアップウィンドウに17と入力すると、結果は「部屋4」と「部屋10」になります。

+0

ええ、私はサンプルデータのように2番目の行に行くことを意味するが、私はそれにカウンターを実行し、私は理由がわからない – dave

+0

それは、このコードで行を続けて、私は部屋の名前が記載されている2番目の行に行くことを意味する – dave

+0

私の編集を参照してください。 – Limak

0

Set rgR1 = ActiveCell.Offset(0, 1)の必要はありません。ワークシート全体でInputBoxに入力されたTimeSlotを検索することができます。

また、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 
0

あなたはこの試みることができる:たぶん、あなただけの参照を取得し、何をあなたの操作を行い、あなたのVBAでセルをアクティブにしないようにしてください、あなたのコードを変更することができます

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 
関連する問題