我试图编写一个小宏来搜索一系列值(由一个充满用户输入的数组提供),并且在大多数情况下,返回它们的当前行。这个范围是一个可打印的工具表,我只需要打印我做出更改的页面,因此我试图通过输入已更新的工具来简化我的工作,并让宏告诉我要打印的页面。
我试图尽可能地理解这一点,但如果需要澄清,请告诉我。
当我尝试使用' Application.WorksheetFunction'
搜索工作表时出现问题Sub FunWithArrays()
Dim ToolNumber()
Dim i As Integer
i = 1
ReDim ToolNumber(1)
Do
ToolNumber(i) = InputBox("Please type the name of the tool with proper capitalization.", "Tool Number")
ReDim Preserve ToolNumber(UBound(ToolNumber) + 1)
i = i + 1
Loop Until ToolNumber(i - 1) = "Done"
MsgBox "Thank you for inputting the tool numbers.", vbOKOnly, "Input Complete"
Dim j As Integer
Dim ToolNoRow As Range
Dim PageNo As Double
For j = 1 To (i - 2)
ToolNoRow = Application.WorksheetFunction.Range("A:A").Text.Find(what:=ToolNumber(j), LookIn:=xlValues, lookat:=xlWhole)
PageNo = Application.WorksheetFunction.RoundUp((ToolNoRow / 1), 0)
MsgBox "Please print page " & PageNo
Next
End Sub
感谢任何和所有帮助!谢谢!
答案 0 :(得分:0)
我不确定我是否完全理解你的宏是如何工作的,因为看起来你想要返回行号,但你谈到了返回页码。
但是,如果要返回找到给定输入的单元格的范围(因为您已声明ToolNoRow As Range
),您可以通过以下方式执行此操作:
Set ToolNoRow = Worksheets("Sheet1").Range("A:A").Text.Find(what:=ToolNumber(j), LookIn:=xlValues, lookat:=xlWhole)
请注意,您必须说明要在哪个工作表中执行搜索。
如果您想要返回找到此输入的行,您可以声明ToolNoRow as Long
然后使用:
ToolNoRow = Worksheets("Sheet1").Range("A:A").Text.Find(what:=ToolNumber(j), LookIn:=xlValues, lookat:=xlWhole).Row
您可能应该在代码中包含一些错误处理,因为只要找不到搜索项,Excel就会返回错误消息。
答案 1 :(得分:0)
试试这个 获得单元格地址后,您可能需要修改逻辑
Dim ToolNumber()
Dim i As Integer
i = 1
ReDim ToolNumber(1)
Do
ToolNumber(i) = InputBox("Please type the name of the tool with proper capitalization.", "Tool Number")
ReDim Preserve ToolNumber(UBound(ToolNumber) + 1)
i = i + 1
Loop Until ToolNumber(i - 1) = "Done"
MsgBox "Thank you for inputting the tool numbers.", vbOKOnly, "Input Complete"
Dim j As Integer
Dim ToolNoRow As Range
Dim PageNo As Double
For j = 1 To (i - 2)
With Range("A:A")
Set ToolNoCell = Find(what:=ToolNumber(j), LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
'Do whatever Here
PageNo = Application.WorksheetFunction.RoundUp((ToolNoRow / 1), 0)
MsgBox "Please print page " & PageNo
Next