Vba 向上移动到某个单元格范围
我正在尝试运行一个程序,让我可以在Microsoft excel中查看大学中的哪些房间在特定时间是免费的 我遇到的问题是在我识别出一个空的类槽之后:Vba 向上移动到某个单元格范围,vba,excel,Vba,Excel,我正在尝试运行一个程序,让我可以在Microsoft excel中查看大学中的哪些房间在特定时间是免费的 我遇到的问题是在我识别出一个空的类槽之后: 我是如何编码它以返回到所有教室的名称中的(所有名称都在第2行) 并存储该值 我尝试过抵消,但那对我不起作用 我已补充了进一步澄清的理由 Public Sub EXq3() Dim rnR1 As Range, roomNum As Integer Const rooms = 13 ' Counter amount Set rgR1 = A
- 我是如何编码它以返回到所有教室的名称中的(所有名称都在第2行)
- 并存储该值
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
你说的“进入第二排”可能是指“进入第二排”,对吗?如果是,这是您的解决方案:
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列中有一些房间1的值,在C列中有一些房间2的值,等等。我已经重构了您的代码,以避免移动活动单元格。它在A列中查找一些时间,并使用此时间选项检查行中是否有一些空单元格,然后返回带有此房间编号的消息
我的试卷:
代码:
所以,我。E当您在弹出窗口中键入17时,结果将是“Room 4”和“Room 10”。无需
设置rgR1=ActiveCell.Offset(0,1)
,您只需在整个工作表中搜索输入框中输入的时隙
另外,最好不要使用Activate
和ActiveCell
,而是使用referencedRange
s
由于在特定时间内可能有一些可用房间,因此需要将其存储为一个数组,并提高在=“”
的每个匹配项上找到的房间的索引
下面的代码注释中有更多解释
代码
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
选项显式
公共子系统EXq3()
Dim rnR1作为范围,roomNum作为变量,时隙
Dim FindRng作为范围,i作为整数,计数器作为整数
施工间=13'柜台金额
ReDim roomNum(1到1000)'init Rooms可用于大尺寸阵列
i=1'您可以尝试以下方法:
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
Public Sub EXq3()
变暗rnR1 As范围
将房间变暗为整数
Dim timeSolt作为字符串,roomNum作为字符串
房间=13'柜台金额
使用ActiveSheet
做
timeSolt=Application.InputBox(“什么时间”,“输入时间”,类型:=2)
如果timeSolt=CStr(False),则退出Sub“也许您可以更改代码,尝试不激活VBA中的单元格,只获取引用并执行您想要执行的操作。我建议您使用数组和循环!它将更具可读性和效率!;)不确定您所说的rgR1.Offset(范围(2,rgR1.Value))
是什么意思?你想达到什么目标?同样,在你的帖子中,你提到“我如何对其进行编码以返回到所有教室的名称(所有名称都在范围2上)”-你在哪里定义和设置范围2
?@ShaiRado我正试图跳回第2行,如上面的示例数据所示。我以为这会定义这个范围,但我必须wrong@dave看到我的答案和下面的代码了吗?我的意思是转到第二行,如示例数据中所示,但当我尝试运行此代码时,在我运行计数器后,它会一直转到随机行,我不知道为什么它会一直转到带有此代码的行,我想走到第二排,那里有房间的名字,我在那里看到了,是的,这正是我想尝试的,非常感谢你。如果某人键入的时间超出了可接受的时间范围,则消息框是否位于第一个if之后?我遗漏了一些代码格式,请参见编辑。是的,当A列中没有类似timeSolt
的值时,会弹出消息“输入有问题”。
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