2017-01-11 100 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 
相关问题