gpt4 book ai didi

excel - 从另一个文件中获取值

转载 作者:行者123 更新时间:2023-12-04 20:37:47 25 4
gpt4 key购买 nike

我想得到来自另一个基于 的工作簿编号并在列之间插入一个新列。

例如,我有这样的数据:
第一个工作簿在左边,第二个工作簿在右边。
enter image description here

在第二个工作簿上,我想在列之间插入一个新列,并根据 放置值编号点击后命令按钮1 .

所以第二个工作簿在宏之后看起来像这样
enter image description here

最佳答案

尝试:

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

关于excel - 从另一个文件中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39470053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com