InputBox中的行参考?

这里是我目前在VBA(Excel)中的代码。 它大部分来自我制作的macros观录音。 我正在寻找的是能够插入,例如,行10只是10在input框中,而不必把它放在10:10。 有没有办法让我编辑我的当前代码,以允许这个? 我试过使用行(“TargetRow:TargetRow”),但是这给了奇怪的结果。

Dim TargetRow As Variant TargetRow = InputBox("Insert row # where data should be inserted. This should take the format XX:XX (eg 90:90 for row 90)", "All Industries Row", "XX:XX") wbThis = ThisWorkbook.Name Windows(wbThis).Activate Rows(TargetRow).Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove Windows("otherworksheet.xlsx").Activate Range("A119:J119").Select Application.CutCopyMode = False Selection.Copy Windows(wbThis).Activate Range(TargetRow).Select Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _ xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False 

使用下面的子选项使用inputinputboxselect行

 Sub SelectRow() Dim lnRow As Long lnRow = InputBox("Enter Row number.", "Row Input") Rows(lnRow & ":" & lnRow).Select End Sub 

如果您需要用户input的Range ,最简单的方法是使用types为“8”的Excel版本Application.InputBox ( 请参阅此处的文档 )。

 Dim TargetRow As Range Set TargetRow = Application.InputBox("Select the row where data should be inserted.", _ "All Industries Row", , , , , , 8) Debug.Print TargetRow.Address 

请注意,您应该也可以摆脱SelectActivate调用,并直接使用您的对象引用。