我目前有一本巨大的excel打印机工作簿,每一层都有自己的工作表,我的办公室用它来管理维护。我把所有的打印机都编译到一张纸上,我引用了它。第一列包含打印机名称,中间一列显示打印机状态,第三列是我最终将显示所需的维修部件或任何注释的位置。我正在使用这个sub来确定我需要对哪个工作表进行VLookup引用。
问题1:我需要从我编译的新工作表中引用"A3:A82“,而不仅仅是"A3”。有没有更好的方法让我做我的第三行代码?
问题2:当我的IF语句当前返回MsgBoxs时,我最终希望它们利用VLookups来引用相应楼层的工作表,并在打印机名称的右侧显示3个单元格。使用偏移量的最佳方法是什么?
这是我到目前为止所知道的:
Sub FindFloor()
Dim PrinterName As Long
the_value = Sheets("magic").Range("A3")
If Mid(the_value, 5, 2) = 11 Then
MsgBox "11th floor"
End If
If Mid(the_value, 5, 2) = 12 Then
MsgBox "12th floor"
End If
If Mid(the_value, 5, 2) = 14 Then
MsgBox "14th floor"
End If
If Mid(the_value, 5, 2) = 15 Then
MsgBox "15th floor"
End If
If Mid(the_value, 5, 2) = 16 Then
MsgBox "16th floor"
End If
If Mid(the_value, 5, 2) = 17 Then
MsgBox "17th floor"
End If
End Sub
发布于 2014-07-31 03:54:45
问题1:我需要从我编译的新工作表中引用"A3:A82“,而不仅仅是"A3”。有没有更好的方法让我做我的第三行代码?
好的。您需要在循环中执行此操作。我还修改了它,使其成为一个更有效的函数,以返回楼层名称,如“11楼”等。
Sub FindFloor()
Dim myRange as Range
Dim values as Variant
Dim the_value as Variant
Dim floorName as String
Dim PrinterName As Long
values = Application.Transponse(Sheets("magic").Range("A3:A82"))
For each the_value in values
Select Case Mid(the_value, 5, 2)
Case 11, 12, 14, 15, 16, 17
floorName = Mid(the_value, 5, 2) & "th floor"
Case Else
' you didn't specify anything to do with other
' results, so this does nothing
End Select
MsgBox the_value & " --> " & floorName
Next
End Sub
对于第2部分,我可能需要更多信息。生成的消息框是工作表名称吗(例如,“11楼”)?您将使用VLOOKUP/等搜索该工作表中的哪个范围?
假设您正在A列中查找the_value
,并希望在C列中返回相应的数据,您可以这样做:
Dim returnedValue as Variant
returnedValue = Application.Vlookup(the_value, Sheets(floorName).Range("A1:C100"), 3, False)
If IsError(returnedValue) Then MsgBox the_value & " not found!"
https://stackoverflow.com/questions/25045538
复制相似问题