2017-01-06 11 views
0

は、私が使用したコードです。私はそれを実行するとファイルのダイアログボックスを開き、ブックを開いてコピーしたい範囲を選択します。 filedialogは範囲を要求し、私は$b$200:$L$500のようなものを入力します。コピー&ペースト

次に、 '2ND FILE'に貼り付けます。ここでは、データ範囲を貼り付けるファイルダイアログを記入する必要があります。

私の質問は次のとおりです。

1.データ範囲を取得するには、このようなタイプのみを入力する必要があります。200:500のみです。ここで、$ bと$ lは固定列です。私はちょうど「を選択200:500を入力の代わりに、私は、ダイアログボックスの[塗りつぶしなし「2NDファイル」に「」A5「」セルではデフォルトでデータ範囲のペーストを得るために記述する必要があるコード2.Whatこの$b$200:$L$500

を入力したいです先セル」ここ

私はあなたが `を設定しxRng2 = xWb.sheets( "シート1")を使用することができます

Private Sub importbr_Click() 

Dim xWb As Workbook 
Dim xAddWb As Workbook 
Dim xRng1 As Range 
Dim xRng2 As Range 
Set xWb = Application.ActiveWorkbook 


xTitleId = "Select BR file" 
With Application.FileDialog(msoFileDialogOpen) 
    .Filters.Clear 
    .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa" 
    .AllowMultiSelect = False 
    .Show 

    If .SelectedItems.Count > 0 Then 
     Application.Workbooks.Open .SelectedItems(1) 
     Set xAddWb = Application.ActiveWorkbook 

     Set xRng1 = Application.InputBox(prompt:="Select source range", Title:=xTitleId, Default:="A1", Type:=8) 
     xWb.Activate 

     Set xRng2 = Application.InputBox(prompt:="Select destination cell", Title:=xTitleId, Default:="A5", Type:=8) 
     xRng1.Copy xRng2 


     xAddWb.Close False 
    End If 
End With 


End Sub 
+2

を使用したコードです。レンジ( "A5")' – nightcrawler23

+0

第一の部分のために入力を文字列として取得し、 ":"で分割する必要があります番号を別々に入手してください。コード内で使用することができます。 – nightcrawler23

+0

または、最初の部分では入力ボックスを2回使用できます。開始行に1回、終了行に1回。 –

答えて

0
Option Explicit 

Private Sub importbr_Click() 

    Dim xAddWb As Workbook 
    Dim xTitleId As String 
    Dim addStartRow As Integer 
    Dim addEndRow As Integer 
    Dim xRng1 As Range 

    xTitleId = "Select BR file" 
    With Application.FileDialog(msoFileDialogOpen) 
     .Filters.Clear 
     .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa" 
     .AllowMultiSelect = False 
     .Show 

     If .SelectedItems.Count > 0 Then 
      Set xAddWb = Application.Workbooks.Open(.SelectedItems(1)) 
      addStartRow = Application.InputBox(prompt:="Type start row", Title:=xTitleId, Default:="200", Type:=1) 
      addEndRow = Application.InputBox(prompt:="Type end row", Title:=xTitleId, Default:="500", Type:=1) 
      With xAddWb.Sheets(1) 'change the index as needed 
       Set xRng1 = .Range(.Cells(addStartRow, 2), .Cells(addEndRow, 12)) 
      End With 

      'alternative #1 'in case, we want the values in the WB, that has this code 
      xRng1.Copy Destination:=ThisWorkbook.Sheets("Sheet1") 

      'alternative #2 'in case, we want the values in the active WB, the one on top, active window 
      xRng1.Copy Destination:=ActiveWorkbook.Sheets("Sheet1") 

      'alternative #3 'in case, we want the values in the a WB, which is always the same 
      Dim wbTarget As Workbook 
      Set wbTarget = Workbooks("the name of target workbook is always the same") 
      xRng1.Copy Destination:=wbTarget.Sheets("Sheet1") 

      'alternative #4 'we let user select one cell in target workbook. Add some error handling! 
      Dim targetRange As Range 
      On Error Resume Next 
      Set targetRange = Application.InputBox("Select cell where to paste values", "Select target cell", Type:=8) 
      If Not targetRange Is Nothing Then 
       xRng1.Copy Destination:=targetRange 
      Else 
       MsgBox "The target range was not set properly.", vbExclamation, "Input error" 
      End If 

      xAddWb.Close False 
     End If 
    End With 'FileDialog 

End Sub 
+0

数時間後に解決策を探して、ついに解決策が出てきます。助けてくれてありがとう@BranislavKollár。 –

+0

@NadalMirそれを聞いてうれしい。これは多くの努力でした。詳細を理解するのに役立つクールな記事[http://www.techrepublic.com/blog/10-things/10-ways-to-reference-excel-workbooks-and-sheets-using-vba/]が見つかりました。 –