2017-02-22 31 views
1

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

Dim TargetRow As Variant 
TargetRow = InputBox("Insert row # where data should be inserted. This should take the format XX:XX (e.g. 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 

回答

1

使用下面的子使用inputbox

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

这个小组将选择从Inputbox()输入的行然后用你的代码调整这个子集。 – harun24hr

0

选择行如果从用户输入需要Range,最简单的方法是使用Excel版本Application.InputBox有型的“8”(see the documentation here )。

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调用并直接使用你的对象引用。