2017-01-11 139 views
-2

我不熟悉VBA在Excel宏,我需要創建爲Mac在Excel中的宏,其執行以下操作:Excel宏 - 從其他選項卡中填充電子表格值

我有2個選項卡( wkst1和wkst2)並且需要填充第三個(wkst3)

wkst1有一列名稱列表 例如

A 

B 

wkst2有兩列

X  1 

Y  4 

我需要填充wkst3確保wkst2所有行關聯到wkst1列出的每一個名字。

例如我希望的結果是:

A  X  1 

A  Y  4 

B  X  1 

B  Y  4 

你可以建議我應該用這個宏的代碼嗎? 在此先感謝!

回答

0
I managed to find the solution by surfing on the web. 

Sub writeall() 

Dim Ws As Worksheet 
Dim Ws1 As Worksheet 
Dim Ws2 As Worksheet 

Dim X As Long 
Dim Y As Long 
Dim count As Long 

Application.ScreenUpdating = False 

Set Ws = ActiveWorkbook.Worksheets(1) 
Set Ws1 = ActiveWorkbook.Worksheets(2) 
Set Ws2 = Workbooks("xxxx.xlsx").Sheets("2017") ' xxxx is the name of the xlsx 

count = 1 


For X = 2 To Ws2.Range("A" & Rows.count).End(xlUp).Row 
For Y = 1 To Ws1.Range("A" & Rows.count).End(xlUp).Row 
    count = count + 1 
    Ws.Cells(count, 1).Value = Ws2.Cells(X, 1).Value 
    Ws.Cells(count, 2).Value = Ws1.Cells(Y, 2).Value 
    Ws.Cells(count, 3).Value = Ws1.Cells(Y, 1).Value 

Next Y 
Next X 

Application.ScreenUpdating = True 
MsgBox ("Spreadsheet updated") 


End Sub 
相關問題