-1
我正在研究一個將從其他工作表信息更新Excel電子表格的宏。但是,更新時我想將兩列移到前面,因爲我不希望它們改變。一切工作到我將兩列移到前面的地步。我選擇它們,剪切它們並粘貼它們,但由於某種原因,在粘貼發生後,它會拋出一個錯誤,指出粘貼失敗(錯誤1004-Range類的PasteSpecial方法失敗)。我很困惑,爲什麼會發生這種情況,任何幫助將不勝感激。Excel更新宏
Sub crossUpdate()
Dim rng1 As Range, rng2 As Range, rng1Row As Range, rng2Row As Range, Key As Range, match As Integer
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks("011 High Level Task List v2.xlsm")
Set wb2 = Workbooks("011 High Level Task List v2 ESI.xlsm")
'Unfilter and Unhide both sheets
With wb1.Sheets("Development Priority List")
.Cells.EntireColumn.Hidden = False
.Cells.EntireRow.Hidden = False
.AutoFilterMode = False
End With
With wb2.Sheets("Development Priority List")
.Cells.EntireColumn.Hidden = False
.Cells.EntireRow.Hidden = False
.AutoFilterMode = False
End With
'Copy and paste original sheet to new temp sheet
wb1.Sheets("Development Priority List").Activate
wb1.Sheets("Development Priority List").Cells.Select
Selection.Copy
Sheets.Add.Name = "SourceData"
wb1.Sheets("SourceData").Paste
'Sort temp sheet by key
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = wb1.Sheets("SourceData").Cells.Range("A2:A" & N)
Set rng1Row = rng1.EntireRow
rng1Row.Sort Key1:=Sheets("SourceData").Range("A1")
'Update sheet sorted by key
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rng2 = wb2.Sheets("Development Priority List").Cells.Range("A2:A" & N)
Set rng2Row = rng2.EntireRow
rng2Row.Sort Key1:=wb2.Sheets("Development Priority List").Range("A1")
'Dev columns moved on update sheet
With wb2.Sheets("Development Priority List")
.Columns("F:G").Cut
.Columns("A:B").Insert Shift:=xlToRight
.Activate
.Columns("A:B").Select
End With
Selection.PasteSpecial <------ Line that throws error
End Sub
這已經回答了你的帖子中的另一個。回去看看它。當你做。激活你會失去你的選擇/剪貼板。 – Sorceri 2014-09-29 16:37:10
所以我只是刪除.activate? – 2014-09-29 16:39:30
在.Insert行之後,添加一行如下:.Columns(「A:B」)。複製這應該可以解決您的問題。請參閱此問題中的答案(其中之一):http://stackoverflow.com/questions/26065376/vba-copy-and-paste-macro – Daniel 2014-09-29 16:58:39