从封闭的工作簿中复制和粘贴

时间:2017-01-06 09:04:01

标签: excel vba excel-vba

我想从'1ST FILE'文件中复制范围并粘贴到'2ND FILE'

这是我使用的代码。当我运行它时会弹出文件对话框以选择我要打开的工作簿并复制它的范围数据。 filedialog会询问范围,我会输入类似$b$200:$L$500的内容。

然后它会粘贴'2ND FILE',在这里我需要填写文件对话框,粘贴数据范围。

我的问题是;

1.我需要编写什么代码来获取数据范围,只需按此类200:500输入。其中$ b和$ l是修复列。我只想输入200:500而不是输入此$b$200:$L$500

2.我需要编写什么代码才能在“2ND FILE”上的“A5”单元格上默认粘贴数据范围而不填写对话框“选择目标单元格”

这是我使用的代码

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

1 个答案:

答案 0 :(得分: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