Forum Discussion

Edo's avatar
Edo
Copper Contributor
Sep 10, 2019

How can I generate a number patterns from a source value

I'am filling some electricty products on my exceel , the problem is that i have 35K products for the momment and i'am filling one by one , is there any formula to fill faster.
I'am asking to fill faster Column B.

If someone know how to do it , can he send me a video how to do.

https://i.imgur.com/e6pWrsM.jpg

3 Replies

  • Edo 

    If you are open to a VBA solution, the following macro will fill the column B as desired.

    In the attached, click the button on Sheet1 to run the code.

    The code is placed on Module1.

     

    Sub FillPattern()
    Dim Rng         As Range
    Dim Cel         As Range
    Dim LR          As Long
    Dim strNum      As String
    Dim Num         As Variant
    Dim ZeroCnt     As Long
    
    Application.ScreenUpdating = False
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    Set Rng = Range("A1:A" & LR)
    
    For Each Cel In Rng
        strNum = getNumber(Cel.Value)
        strNum = Left(strNum, Len(strNum) - 1)
        
        If Left(strNum, 1) = "R" Then
            Num = Replace(strNum, "R", "")
            Num = "0." & Num
            Cel.Offset(0, 1).NumberFormat = "0.00"
        Else
            ZeroCnt = Right(strNum, 1)
            Num = Left(strNum, Len(strNum) - 1)
            Num = Num & WorksheetFunction.Rept("0", ZeroCnt) & ".0"
            Cel.Offset(0, 1).NumberFormat = "0.0"
        End If
        
        Cel.Offset(0, 1).Value = Num
    Next Cel
    
    Application.ScreenUpdating = True
    End Sub
    
    Function getNumber(ByVal str As String) As String
    Dim strNum      As String
    Dim Num         As Long
    Dim Matches     As Object
    
    With CreateObject("VBScript.RegExp")
        .Global = False
        .Pattern = "R?(\d+)[UV]"
        If .test(str) Then
            Set Matches = .Execute(str)
            getNumber = Matches(0)
        End If
    End With
    End Function

     

    To implement this code to your original workbook, follow these steps...

    1. Open your file and press Alt+F11 to open the VB Editor.
    2. On VB Editor Ribbon--> Insert Tab --> Choose Module and then copy the above code and paste it into the opened code window.
    3. Close the VB Editor window.
    4. Save your Excel Workbook as Macro-Enabled Workbook,
    5. To insert a button, you may insert any Shape of your choice, edit it's text and right click on the inserted Shape and choose Assign Macro and select the macro "FillPattern" in the next Macro Window which would appear with the available macros in the workbook and then click ok to finish. So now you may click the inserted Shape to run your code.
    6. You may also run the code after pressing the Alf+F8 to open the Macro Window, selecting the macro from the list and clicking on the Run button.

     

      • Subodh_Tiwari_sktneer's avatar
        Subodh_Tiwari_sktneer
        Silver Contributor

        Edo 

        You're welcome! Glad I could help.

        Please take a minute to accept the post with the proposed solution as a Best Answer/Response which will mark your question as Solved.

        You may hit the Like button under the post which resolved your question, that's another way to say thanks.

Resources