Excel VBA输入框-选择范围(类型8),但不作为固定范围,即A1不是$a$1

Excel VBA输入框-选择范围(类型8),但不作为固定范围,即A1不是$a$1,excel,vba,lookup,inputbox,Excel,Vba,Lookup,Inputbox,目前,我的VBA代码中有一行代码用于输入框: Set myValues=Application.InputBox(“请在电子表格中选择列中第一个包含您要查找的值的单元格:”,键入:=8) 但是,当我选择单元格时,它会自动输入,例如,$A$1。用户无需手动删除$即可更改此设置,以便Inputbox将自动拾取单元格引用作为A1 它是自动化的VLookup宏的一部分,除了整列中固定的VLookup值之外,该宏工作得非常好 提前谢谢 更新-以下是完整代码: Dim FirstRow As Long Di

目前,我的
VBA
代码中有一行代码用于
输入框

Set myValues=Application.InputBox(“请在电子表格中选择列中第一个包含您要查找的值的单元格:”,键入:=8)

但是,当我选择单元格时,它会自动输入,例如,
$A$1
。用户无需手动删除$即可更改此设置,以便
Inputbox
将自动拾取单元格引用作为
A1

它是自动化的
VLookup
宏的一部分,除了整列中固定的
VLookup
值之外,该宏工作得非常好

提前谢谢

更新-以下是完整代码:

Dim FirstRow As Long
Dim FinalRow As Long
Dim myValues As Range
Dim myResults As Range
Dim myCount As Integer

Sub VlookupMacroNew()  

Set myValues = Application.InputBox("Please select on the spreadsheet the first cell in the column with the values that you are looking for:", Default:=Range("A1").Address(0, 0), Type:=8)

Set myResults = Application.InputBox("Please select on the spreadsheet the first cell where you want your lookup results to start:", Type:=8)

myCount = Application.InputBox("Please enter the column number of the destination range:", Type:=1)

On Error Resume Next
myResults.EntireColumn.Insert Shift:=xlToRight
Set myResults = myResults.Offset(, -1)
FirstRow = myValues.Row
FinalRow = Cells(65536, myValues.Column).End(xlUp).Row

Range(myResults, myResults.Offset(FinalRow - FirstRow)).Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address & ", " & _
"'S:\Payroll\CONTROL SECTION\[Latest Data.xls]Sheet1'!$A$1:$B$100" & ", " & myCount & ", False)"

MyValue将从单元格A2开始,但当我们使用动态列表时,我需要将查找值更改为A3、A4、A5等,因为公式将复制到列表中。通过使用$A$2的inputbox,查找公式只查看该单元格引用。

您的问题不在于inputbox,而在于
Address()
方法的行为

默认情况下,不带参数的
Address()
返回绝对地址

Cells(FirstRow, myValues.Column).Address(False,False)

将返回“非固定”范围地址

正如蒂姆所说,你的问题不在于输入框。相反,当您一次为整个范围设置公式时,它会对每个单元格的公式使用
FirstRow
。理想情况下,您应该使用
.FormulaR1C1
来设置公式。这将允许您在一次过程中使公式成为相对的

Cells(FirstRow, myValues.Column).Address(False,False)
下面的解决方案只是修改代码,将非相对地址放在第一个单元格中,然后将剩余单元格中的公式设置为与第一个单元格中的公式相等。当您指定这样的公式时,会使它们成为相对的:

Sub VlookupMacroNew()

Set myValues = Application.InputBox("Please select on the spreadsheet the first cell in the column with the values that you are looking for:", Default:=Range("A1").Address(0, 0), Type:=8)
Set myResults = Application.InputBox("Please select on the spreadsheet the first cell where you want your lookup results to start:", Type:=8)
myCount = Application.InputBox("Please enter the column number of the destination range:", Type:=1)

On Error Resume Next
myResults.EntireColumn.Insert Shift:=xlToRight
Set myResults = myResults.Offset(, -1)
FirstRow = myValues.Row
FinalRow = Cells(65536, myValues.Column).End(xlUp).Row

Range(myResults, myResults.Offset(FinalRow - FirstRow)).Cells(1).Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False) & ", " & _
"'S:\Payroll\CONTROL SECTION\[Latest Data.xls]Sheet1'!$A$1:$B$100" & ", " & myCount & ", False)"
Range(myResults, myResults.Offset(FinalRow - FirstRow)).Formula = _
Range(myResults, myResults.Offset(FinalRow - FirstRow)).Cells(1).Formula
End Sub

我不明白你所说的“整列中的VLookup值是固定的”是什么意思。很容易将所选单元格更改为相对地址,但是,了解更多关于如何使用它的信息会很有帮助,特别是因为您无法按所需的方式更改InputBox行为。我已更新了描述,以包含查找宏Cheers Doug的完整代码,这非常有效。我会记住那条规则,以备将来参考。