0
以下代碼返回允許我將標題與我的儀表板文件和apac文件進行匹配,並將數據複製到儀表板文件和「臨時計算」表。問題是,apac不是唯一的文件,我需要能夠通過彈出窗口選擇文件,並在循環上運行此代碼,以便它將粘貼來自每個文件後的數據匹配標題後在「temp calc」中使用行。我無法做到這兩個,請指教?通過彈出窗口選擇.xlsx文件並從它們複製數據並將它們粘貼到循環中上次使用的行之後
感謝,
馬修
Sub copyCol()
Sheets("Temp Calc").Select
'Clear existing sheet data except headers
Rows(1).Offset(1, 0).Resize(Rows.Count - 1).ClearContents
Dim lastCol, lastRow As Long, k As Long, a As Variant, b As Variant, cmpRng As Range
Dim mastCol As Long, mastRng As Range, n As Long
Dim Wbk As Workbook
Application.ScreenUpdating = False
Worksheets("Temp Calc").Select
lastCol = Worksheets("Temp Calc").Cells(1, Columns.Count).End(xlToLeft).Column
lastRow = Worksheets("Temp Calc").Cells(Rows.Count, 1).End(xlDown).Row
Set cmpRng = Range(Cells(1, 1), Cells(1, lastCol))
a = cmpRng
Set Wbk = Workbooks.Open("G:\work\APAC.xlsx")
Worksheets("Sheet1").Select
mastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Set mastRng = Range(Cells(1, 1), Cells(1, mastCol))
b = mastRng
For k = 1 To lastCol
For n = 1 To mastCol
If UCase(a(1, k)) = UCase(b(1, n)) Then
Windows("APAC-Personal Assignment.xlsx").Activate
Worksheets("Sheet1").Range(Cells(2, n), Cells(lastRow, n)).Copy
Windows("Dashboard_for_Roshan.xlsm").Activate
Worksheets("Temp Calc").Select
Cells(2, k).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Exit For
End If
Next
Next
Application.ScreenUpdating = True
Exit Sub
End Sub
這是非常有益的KazjJaw,我希望我可以進一步建設。 :) 你能不能幫我把我的代碼和你的代碼結合起來,讓我能更好地理解它。我已經去了很多類似的例子,但我無法理解這個過程。 感謝隊友, 馬修 – mathew
在這種情況下,整合是非常個人的問題,沒有測試,很難支持。但是我認爲你會應付一些建議:在這行'a = cmpRng'後面添加我的代碼,並將代碼從'Set Wbk ...'開始移動到第二個'next',在我的循環中我聲明瞭''您的代碼'。接下來用F8鍵運行它,以檢查是否正常工作...... –
我做上面的,我就在這行 的Windows(「APAC-個人Assignment.xlsx」)的錯誤。激活 參考的問題,做我改變它? – mathew