2015-04-16 89 views
0

我想從用戶選定的工作簿中導入特定列以激活工作簿。如何導入工作表的列以激活工作簿

當我複製並粘貼列時,它無法粘貼到目標工作表中。我究竟做錯了什麼?

這裏是我的代碼:

Sub Import_sheet() 

Dim fDialog As Office.FileDialog 
Dim varFile As Variant 
Dim wb As Workbook 
Dim activeWB As Workbook 
Set activeWB = Application.ActiveWorkbook 

Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
    With fDialog 
     .AllowMultiSelect = True 
     .Title = "Select Files to prepare" 
     .Filters.Add "All Files", "*.*" 

     If .Show = True Then 

     For Each varFile In .SelectedItems 
      MsgBox varFile 
      Application.ScreenUpdating = False 
      Application.DisplayAlerts = False 

      Set wb = Application.Workbooks.Open(varFile) 
      'wb.Worksheets(1).Copy _ 
      After:=activeWB.Sheets(activeWB.Sheets.Count) 

      wb.Sheets(1).Columns("C").Select 
      Selection.Copy 

      activeWB.Sheets("template").Columns("B").Select 
      Selection.Paste 

      wb.Close False 

      Application.ScreenUpdating = True 
      Application.DisplayAlerts = True 
     Next 

     Else 
     MsgBox "You clicked Cancel in the file dialog box." 
     End If 
    End With 

End Sub 

回答

0

你需要複製前啓動工作簿或粘貼,所以它不是超出範圍。

wb.activate 
    wb.Sheets(1).select 
    wb.Sheets(1).Columns("C").Copy 


    activeWB.activate 
    activeWB.Sheets("template").select 
    activeWB.Sheets("template").Cells("B1").Paste 
+0

非常感謝您...... –

+0

沒問題,你能不能請回答:) –

+0

喜傑斯的側點擊正確的符號,沒有代碼做的伎倆? –

相關問題