从file.dialog中选择工作表,然后从活动单元格中获取一些数据

时间:2018-05-28 21:26:19

标签: excel vba excel-vba

当我的VBA脚本无法弄清楚如何调用"选定的工作表

我需要做什么:

  • 选择一个文件(如果它可以自动从文件夹中的所有文件中提取,那将很棒,但不知道如何...)
  • 从中复制一些数据并粘贴到当前打开的Excel中

    Private Sub openDialog()
    Dim fd As Office.FileDialog
    
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
     With fd
    
      .AllowMultiSelect = True
    
      ' Set the title of the dialog box.
      .Title = "Please select the file."
    
      ' Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "All Files", "*.*"
    
      ' Show the dialog box. If the .Show method returns True, the
      ' user picked at least one file. If the .Show method returns
      ' False, the user clicked Cancel.
      If .Show = True Then
    
    
    'edit data
    Range("K4").Select
    ActiveCell.FormulaR1C1 = "=LEFT(RIGHT(R[-2]C[-10],65),20)"
    Range("L4").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=R[5]C[-3]"
    Range("M4").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=R[6]C[-4]"
    Range("N4").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=R[7]C[-5]"
    Range("O4").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=R[8]C[-6]"
    
    ' copy data
    Range("K4:O4").Select
    Selection.Copy
    
    ' paste on active cell
    For i = 1 To LastRow
    ws.Range ("A" & i)
    ActiveSheet.PasteSpecial Paste:=xlPasteValues
    Next i
    
    'DO NOT KNOW WHAT TO USE HERE
    
      End If
     End With
    
    End Sub
    

0 个答案:

没有答案