2017-02-22 33 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調用並直接使用你的對象引用。