如何使用VBA从另一个工作表中的不同行中提取多个单元格

时间:2015-05-15 08:28:48

标签: excel vba excel-vba

目前,我需要从另一个工作表的不同行中的不同单元格中提取值,如下所示:

enter image description here

我只需要来自某些列和某些行的数据,但不同行上的所需数据具有相同的列位置。我设想了很多方法,比如用户先选择整行并提取行号,然后添加列号。但是,我觉得应该有一种更简单,更智能的方法将这些数据提取到另一张表中,我已经有了代码来执行这些操作:

Sub ImportDatafromotherworksheet()
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As Range
Dim rngDestination As Range
Set wkbCrntWorkBook = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa"
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count > 0 Then
        Workbooks.Open .SelectedItems(1)
        Set wkbSourceBook = ActiveWorkbook
        Set rngSourceRange = Application.InputBox(Prompt:="Select source row number", Title:="Source Range", Default:="press Ctrl for multiple selection", Type:=8)

        rngSourceRange.Font.Bold = True
        wkbCrntWorkBook.Activate
        Set rngDestination = Application.InputBox(Prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
        rngSourceRange.Copy rngDestination
        rngDestination.CurrentRegion.EntireColumn.AutoFit
        wkbSourceBook.Close False
    End If
End With
End Sub

感谢任何提出更好想法的人来提取这些细胞。

0 个答案:

没有答案