简体   繁体   English

如何使用VBA在Excel中将后缀添加到特定数据

[英]How to add the suffix to the particular data in excel using VBA

I have maintained a worksheet which contains the parameters of a particular product. 我维护了一个工作表,其中包含特定产品的参数。

Here when i click ADD button all the contents of the CONTROL POWER TRANSFORMERS should get copied and it should get pasted below. 在这里,当我单击“添加”按钮时,“控制电源变压器”的所有内容都应复制并粘贴到下面。

Here i am successful in copying the entire product details and pasting below using the vba code as mentioned below: 在这里,我成功复制了完整的产品详细信息,并使用如下所述的vba代码在下面粘贴:

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

            WsEPC.Range(cF.Offset(-1, 0), cF.Offset(3, 3).End(xlDown)).Copy

            WsEPC.Range("A" & RowNumber + 1).Select


                Selection.Insert Shift:=xlDown
                Application.CutCopyMode = False

    End With
 End With

 MsgBox " Successfully added the product to EPC"

The above code is successfully copying the above product details below as shown below 上面的代码成功复制了下面的上述产品详细信息,如下所示

在此处输入图片说明

Here "CTPT1" is the unique product id given to the product CONTROL POWER TRANSFORMERS and the same id is copying when i click ADD, instead of copying CTPT1 as shown in the figure(A22) i want it to automatically make itself to "CTPT2" when Add button is clicked, like wise if the add button is again clicked it should become "CTPT3" and so on. 这里“ CTPT1”是产品CONTROL POWER TRANSFORMERS的唯一产品ID,当我单击ADD时,正在复制相同的ID,而不是复制CTPT1(如图(A22)所示),我希望它自动使自己成为“ CTPT2”单击“添加”按钮时,同样明智的做法是,如果再次单击“添加”按钮,则应变为“ CTPT3”,依此类推。

Can anyone tell me how to achieve this automatic generation of the unique id using excel VBA. 谁能告诉我如何使用excel VBA自动生成唯一ID。 Any help is Appreciated! 任何帮助表示赞赏!

This should do the trick : 这应该可以解决问题:

Dim WbEPC As Workbook, _
    WbCPT As Workbook, _
    WsEPC As Worksheet, _
    WsCPT As Worksheet, _
    StringToSearch As String, _
    ButtonRow As Long, _
    WriteRow As Long, _
    cF As Range


Set WbEPC = Workbooks("EPC 1.xlsm")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet1")
StringToSearch = "CTPT"

With WsEPC
    .Activate
    ButtonRow = .Shapes("CommandButton21").TopLeftCell.Row
    .Rows(ButtonRow + 1 & ":" & ButtonRow + 1).Insert Shift:=xlDown, _
                                                    CopyOrigin:=xlFormatFromLeftOrAbove
    With .Columns(1)
        Set cF = .Find(what:=StringToSearch, _
                        lookat:=xlPart, _
                        searchorder:=xlByRows, _
                        searchdirection:=xlNext, _
                        MatchCase:=False, _
                        SearchFormat:=False)
        WsEPC.Range(cF.Offset(-1, 0), cF.Offset(3, 3).End(xlDown)).Copy
        WriteRow = WsEPC.Range("B" & WsEPC.Rows.Count).End(xlUp).Row + 2
        WsEPC.Range("A" & WriteRow).Insert Shift:=xlDown
        Application.CutCopyMode = False

        cF.Value = CStr(StringToSearch & CLng(Replace(cF.Value, StringToSearch, vbNullString)) + 1)
    End With
End With

MsgBox " Successfully added the product to EPC"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM