基本上我只是在2小时前学习宏excel。 所以我只是想从这门语言中学习一些基础知识,所以问题是我只想基于 No.Id 从另一个工作簿中获取值并在之间插入一个新列柱。 例如,我在第一个工作簿上有这样的数据在左边,右边是第二个工作簿 1st and 2nd Workbook
在第二个工作簿上,我想在列之间插入一个新列,并在点击 CommandButton1
后根据 No.Id 放置值所以第二个工作簿就像这样After Macro
你能帮助我,告诉我它是如何运作的吗?拜托......:D
答案 0 :(得分:1)
尝试:
Dim wsSource As Worksheet
Set wsSource = ActiveWorkbook.Worksheet("NAME SOURCE SHEET")
Dim wsOutput As Worksheet
Set wsOutput = ActiveWorkbook.Worksheet("NAME OUTPUT SHEET")
Dim ID as Range
Dim FindID as Range
Dim lRowSource as Integer
Dim lRowOutput as Integer
lRowSource = wsSource.Range("A" & Rows.Count).End(xlUp).row
lRowOutput = wsSource.Range("A" & Rows.Count).End(xlUp).row
With wsOutput
Range("B1").EntireColumn.Insert
For each ID in .Range("A1:A" & lRowSource)
Set FindID = wsSource.Range("A1:A" & lRowOutput).Find(What:=ID, LookIn:=xlValues, lookat:=xlWhole)
If Not FindID is Nothing then
wsSource.Range("B" & FindID.Row).Copy Destination:=wsOutput.Range("B" & FindID.Row)
Else
Exit Sub
End If
End With
或者您也可以使用:
Dim wsSource As Worksheet
Set wsSource = ActiveWorkbook.Worksheet("NAME SOURCE SHEET")
Dim wsOutput As Worksheet
Set wsOutput = ActiveWorkbook.Worksheet("NAME OUTPUT SHEET")
Dim i As Long
wsOutput.Range("B1").EntireColumn.Insert
For i=1 To wsSource.UsedRange.Rows.Count
If wsSource.Range("A" & i) = wsOutput.Range("A" & i) Then
wsSource.Range("B" & i).Copy Destination:=wsOutput.Range("B" & i)
End If
Next i