2017-07-24 47 views
0

我在驅動器「D」中有一個excel文件。我想將工作簿中的數據從工作簿 「工作表」複製到sheet1中的另一工作簿「SC」 BW」。從一個工作簿中提取數據並將其粘貼到另一箇中

我正在使用下面的代碼,從一個工作簿中提取數據並將其粘貼到另一個工作簿。

Sub extract() 
Dim x As Workbook 
Dim y As Workbook 
Dim Val As Variant 
Dim filename As String 
Dim LastCell As Range 
Dim LastRow As Long 
CopyCol = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X", ",") 
LR = Cells(Rows.Count, 1).End(xlUp).Row 
LC = Cells(1, Columns.Count).End(xlToLeft).Column 
LCell = Selection.SpecialCells(xlCellTypeLastCell).Address 
LCC = Selection.SpecialCells(xlCellTypeLastCell).Column 
LCR = Selection.SpecialCells(xlCellTypeLastCell).Row 
Set y = ThisWorkbook ' set ThisWorkbook object (where this code lies) 
Set x = Workbooks.Open("D:\Student\Jenny\Raw.xlsx") 
For Count = 0 To UBound(CopyCol) 
Set temp = Range(CopyCol(Count) & "1:" & CopyCol(Count) & LCR) 
If Count = 0 Then 
Set CopyRange = temp 
Else 
Set CopyRange = Union(CopyRange, temp) 
End If 
Next 
CopyRange.Copy 
y.Sheets("BW").Range("A5").PasteSpecial 
x.Close 
End Sub 

此代碼幹活,但問題是,在我的片,而「sheet1」我有我的數據從A4開始,並想在目的地表「BW」從A5的數據複製。 當前的代碼,粘貼從A7複製的數據。我怎樣才能修改這種方式來粘貼來自A5的複製數據。

任何潛在客戶都將有所幫助。

+1

在設置溫度設置溫度=範圍(CopyCol(Count)&「1:」&CopyCol(Count) &LCR)' – Mrig

+0

它工作:)你也可以告訴我,我如何從源表中選擇一個特定的表。 ?因爲在一個工作簿中,我有幾張紙,而且我需要「結果」表中的數據 – Jenny

回答

2

Set temp嘗試4代替1作爲

Set temp = Range(CopyCol(Count) & "1:" & CopyCol(Count) & LCR) 

如何可以選擇從源片的特定片材(表結果)。 ?

使用

With x.Sheets("Result") 
. 
. 
. 
End With 

x.Sheets("Result").或任何你正在嘗試。

您有許多未使用和未聲明的變量。您的更新代碼可能看起來像這樣:

Option Explicit 

Sub extract() 
    Dim x As Workbook, y As Workbook 
    Dim temp As Range, CopyRange As Range 
    Dim LR As Long, LC As Long, LCR As Long, Count As Long 
    Dim copycol 
    copycol = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X", ",") 
    LR = Cells(Rows.Count, 1).End(xlUp).Row 
    LC = Cells(1, Columns.Count).End(xlToLeft).Column 
    Set y = ThisWorkbook ' set ThisWorkbook object (where this code lies) 
    Set x = Workbooks.Open("D:\Student\Jenny\Raw.xlsx") 

    With x.Sheets("Result") 
    LCR = .Cells(.Rows.Count, 1).End(xlUp).Row 
     For Count = 0 To UBound(copycol) 
      Set temp = .Range(copycol(Count) & "4:" & copycol(Count) & LCR) 
      If Count = 0 Then 
       Set CopyRange = temp 
      Else 
       Set CopyRange = Union(CopyRange, temp) 
      End If 
     Next 
     CopyRange.Copy 
     y.Sheets("BW").Range("A5").PasteSpecial 
    End With 
    x.Close 
End Sub 
+0

@珍妮 - 請參閱我的代碼。我通過刪除未使用和未聲明的變量來更新它。 – Mrig

相關問題