首页 > 解决方案 > VBA Application.InputBox 验证。只在一定范围内选择

问题描述

我正在寻找一个输入框供用户选择一个单元格,宏将通过用户选择过滤一些外部数据。

我想为选择添加验证,以便用户只能选择一个值为“TRUE”的单元格,并且只能在某个范围内,例如列 Range("J4:J100")

到目前为止的代码:

Dim rng As Range
Set rng = Range("J4:J100")

Dim criteria1 As Range
Set criteria1 = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)

Dim criteria2 As String
criteria2 = criteria1.Offset(0, -9)

Dim ws As Worksheet
Set ws = Worksheets("Reference_log")

ws.Select

Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

Dim LastColumn As Long 'Find the last non-blank cell in row 2
LastColumn = Cells(2, Columns.Count).End(xlToLeft).Column

Set Rng1 = Cells(2, 8)
Set Rng2 = Cells(LastRow, LastColumn)



ActiveSheet.Range(Rng1, Rng2).AutoFilter Field:=2, criteria1:=Array("*" & criteria2 & "*"), Operator:=xlFilterValues

标签: excelvba

解决方案


推荐阅读