如何使用excel vba将数据从指定地址复制到下一个指定位置
我有一个工作表,其中包含每个产品的详细信息 在这里,我加上了一个按钮(ADD),通过点击它,我想复制控制电源变压器块的所有细节,并将其复制到下面(我的意思是从B20复制) 我已经编写了一个代码来精确定位CTPT(这是该产品的唯一id),并将其作为参考,我已经使用下面的代码复制了整个块,直到行结束如何使用excel vba将数据从指定地址复制到下一个指定位置,vba,excel,Vba,Excel,我有一个工作表,其中包含每个产品的详细信息 在这里,我加上了一个按钮(ADD),通过点击它,我想复制控制电源变压器块的所有细节,并将其复制到下面(我的意思是从B20复制) 我已经编写了一个代码来精确定位CTPT(这是该产品的唯一id),并将其作为参考,我已经使用下面的代码复制了整个块,直到行结束 Set cF = .Find(what:="CTPT", _ lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
Set cF = .Find(what:="CTPT", _
lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(-1, 3), cF.Offset(-1, 1).End(xlDown)).Copy
现在在粘贴单元格时,我需要做几件事
子测试_Karthik()
Sub test_Karthik()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsx")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet2")
With WsEPC
.Activate
With .Range("A1:A10000")
'First, define properly the Find method
Set cF = .Find(What:="CTPT", _
After:=ActiveCell, _
LookIn:=xlValues, _
Lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If there is a result, keep looking with FindNext method
If Not cF Is Nothing Then
FirstAddress = cF.Address
Do
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(0, 1).End(xlUp), cF.Offset(0, 3).End(xlDown)).Copy
WriteRow = WsCPT.Range("E" & WsCPT.Rows.Count).End(xlUp).Row + 1
WsCPT.Range("E" & WriteRow).PasteSpecial (xlPasteValues)
cF.EntireRow.Insert xlDown, False
Set cF = .FindNext(cF)
'Look until you find again the first result
Loop While Not cF Is Nothing And cF.Address <> FirstAddress
End If
End With
End With
End Sub
将WbEPC作为工作簿_
WbCPT作为工作簿_
WsEPC作为工作表_
WsCPT作为工作表_
FirstAddress作为字符串_
WriteRow只要_
cF作为范围_
num作为字符串
设置WbEPC=工作簿(“EPC 1.xlsx”)
设置WbCPT=工作簿(“控制电源变压器.xlsm”)
设置WsEPC=WbEPC.Sheets(“Sheet1”)
设置WsCPT=WbCPT.Sheets(“Sheet2”)
与WsEPC
.激活
范围(“A1:A10000”)
'首先,正确定义Find方法
设置cF=.Find(内容:=“CTPT”_
之后:=ActiveCell_
LookIn:=xlValues_
看:=xlPart_
搜索顺序:=xlByRows_
SearchDirection:=xlNext_
MatchCase:=假_
SearchFormat:=False)
'如果有结果,请继续使用FindNext方法查找
如果不是,那么cF什么都不是
FirstAddress=cF.地址
做
num=cF.Address'这里我们将获得CTPT的单元地址($14澳元)
WsEPC.Range(cF.Offset(0,1).结束(xlUp),cF.Offset(0,3).结束(xlDown)).Copy
WriteRow=WsCPT.Range(“E”&WsCPT.Rows.Count)。End(xlUp)。Row+1
WsCPT.Range(“E”和WriteRow).PasteSpecial(XLPasteValue)
cF.EntireRow.Insert xlDown,False
设置cF=.FindNext(cF)
“看看,直到你再次找到第一个结果
循环而非cF为Nothing且cF.Address为FirstAddress
如果结束
以
以
端接头
(1)什么是CTPT,为什么需要它,在哪里使用,等等?(2) 你到底想复制什么(比你目前所说的更具体)以及粘贴到哪里?你能再解释一下“我需要通过找到点击按钮的单元格地址来插入一行”吗?是否要在单击的按钮前插入一行?“CTPT”是为每个产品维护的唯一id。如果我需要有一个类似的产品多副本,那么这些是我下面的步骤。我将找到“CTPT”的单元格地址,通过保留其引用,我将复制属于该特定产品的所有参数。然后,我将选择ADD按钮的单元格地址,通过保留它的引用,我将首先再添加一行,然后通过选择新插入行的第二列来插入复制的数据。现在我有了产品详细信息的副本感谢您的帮助@R3uK,通过对我的代码进行一些轻微的修改,我在下面发布了代码。对于这种大小的代码块,您确实需要添加一些解释,说明您所做的事情以及原因。
Private Sub CommandButton21_Click()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsm")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet1")
Dim b As Object, RowNumber As Integer
Set b = ActiveSheet.Shapes("CommandButton21")
With b.TopLeftCell
RowNumber = .Row
End With
Rows(RowNumber + 1 & ":" & RowNumber + 1).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
With WsEPC
.Activate
With .Range("A1:A10000")
Set cF = .Find(what:="CTPT", _
lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(-1, 3), cF.Offset(-1, 1).End(xlDown)).Copy
WsEPC.Range("B" & RowNumber + 1).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
End With
End With
MsgBox " Successfully added the product to EPC"
End Sub