5

私は現在、VBAコードに次の行がありますInputbox

Set myValues = Application.InputBox("Please select on the spreadsheet the first cell in the column with the values that you are looking for:", Type:=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)"

myValuesはセルA2などで始まりますが、動的リストを使用しているため、数式がリストにコピーされるときにルックアップ値をA3、A4、A5などに変更する必要があります。$ A $ 2を使用する入力ボックスでは、ルックアップ式はそのセル参照のみを調べます。

4

2 に答える 2

2

問題はInputBoxではなく、Address()メソッドの動作にあります。

デフォルトでAddress()は、パラメータなしで絶対アドレスを返します。

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

「固定されていない」範囲アドレスを返します。

于 2012-11-20T17:20:16.553 に答える
2

ティムが言うように、あなたの問題はInputBoxにありません。むしろ、範囲全体の数式を一度に設定するとFirstRow、各セルの数式に使用されます。理想的には.FormulaR1C1、数式を設定するために使用します。これにより、1回のパスで数式を相対的なものにすることができます。

以下の解決策は、最初のセルに非相対アドレスを配置するようにコードを変更し、残りのセルの数式を最初のセルの数式と同じに設定するだけです。そのような数式を割り当てると、それらは相対的なものになります。

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
于 2012-11-20T18:12:16.093 に答える