RegEx Row Column Listing Error

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
177
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
coming from this thread String Parsing

I was able to convert @Peter_SSs function code to an ordinary procedure. (thanks again @Peter_SSs )
VBA Code:
Sub PetListII()
 
  Dim RX As Object, M As Object, RXCtr As Integer
    
    Dim K As Integer, ConfisKated As String, FullName As Variant, MaxNumero() As Integer

    Set WSCopy = ActiveSheet
    ActiveSheet.Select
        
    'Find the last non-blank cell in row 1
    LastRow = Cells(rows.Count, 1).End(xlUp).Row
    'Find the last non-blank cell in COL 1
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        
    ReDim MaxNumero(LastRow)
'------------------------------
'
'   Count The "," in a string
'   Result will be used for the looping sequence
'
'------------------------------
    For K = 2 To LastRow
        MaxNumero(K) = Len(WSCopy.Range("A" & K)) - Len(Application.WorksheetFunction.Substitute(WSCopy.Range("A" & K), ",", "")) + 1
    Next K
    LastMaxNum = WorksheetFunction.Max(MaxNumero)
 
'------------------------------
'
'   RegEx Pattern
'   by  Peter_SSs
'   @ https://www.mrexcel.com/
'
'------------------------------
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.Pattern = "([^,]+\*.+?)(\,?)(?=([^,]+\*)|$)"
    
    Set M = RX.Execute(Cells(2, 1))
    RXCtr = 0
    For K = 2 To LastRow
        For iCTR = 2 To LastMaxNum
            Cells(K, iCTR).Value = M(RXCtr).SubMatches(0)
            RXCtr = RXCtr + 1
        Next
    Next
    
End Sub


But got hit by another roadblock. what I'm trying to achieved here is to process all rows and output in their respective cell address.
I think the code
VBA Code:
Set M = RX.Execute(Cells(2, 1))
outside of the loop is causing the "Run-time error '5'; Invalid Procedure call or argument" in
Code:
Cells(K, iCTR).Value = M(RXCtr).SubMatches(0)

X TEMPLATE.xlsm
ABCD
1EntryRESULT
2Dog*Brown,White,Black,Dragon*Golden,Green,Red,Black,Eagle*Red,Black,GreenDog*Brown,White,BlackDragon*Golden,Green,Red,BlackEagle*Red,Black,Green
3Dog*Brown,White,Black,Eagle*Red,Black,GreenDog*Brown,White,BlackEagle*Red,Black,Green
4Dragon*Golden,Green,Red,BlackDragon*Golden,Green,Red,Black
5Dog*Brown,White,Black,Dragon*Golden,Green,Red,Black,Eagle*Red,Black,GreenDog*Brown,White,BlackDragon*Golden,Green,Red,BlackEagle*Red,Black,Green
6Dog*Brown,White,Black,Dragon*Golden,Green,Red,Black,Eagle*Red,Black,GreenDog*Brown,White,BlackDragon*Golden,Green,Red,BlackEagle*Red,Black,Green
StringParsing (3)
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this

VBA Code:
Sub Pet_Lists()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "(\,)(?=([^,]+\*)|$)"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      a(i, 1) = RX.Replace(a(i, 1), ";")
    Next i
    With .Offset(, 1)
      .Value = a
      .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
    End With
  End With
End Sub
 
Upvote 0
Solution
Try this

VBA Code:
Sub Pet_Lists()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "(\,)(?=([^,]+\*)|$)"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      a(i, 1) = RX.Replace(a(i, 1), ";")
    Next i
    With .Offset(, 1)
      .Value = a
      .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
    End With
  End With
End Sub
It's working the way It was needed but after running the code this message keeps popping up..
 

Attachments

  • 2022.03.18 - ERR 02.JPG
    2022.03.18 - ERR 02.JPG
    140.6 KB · Views: 9
Upvote 0
The code is looking for any data in column A and expecting columns B,C, D, ... etc to be blank, ready to receive the results.
Do you already have something in columns B,C, D, ... etc?
 
Upvote 0
The code is looking for any data in column A and expecting columns B,C, D, ... etc to be blank, ready to receive the results.
Do you already have something in columns B,C, D, ... etc?
I started it without contents... but still it keeps on popping...
columns B onward is empty..
 
Upvote 0
I started it without contents... but still it keeps on popping...
columns B onward is empty..
If they are actually empty the alert will not pop up. There must be something somewhere in the road of the results.

If still problems, try adding this line where shown.

Rich (BB code):
With .Offset(, 1)
  .Resize(, 50).ClearContents
  .Value = a
  .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
End With
 
Upvote 0
If they are actually empty the alert will not pop up. There must be something somewhere in the road of the results.

If still problems, try adding this line where shown.

Rich (BB code):
With .Offset(, 1)
  .Resize(, 50).ClearContents
  .Value = a
  .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
End With
I don't know why but even with that "clearcontents" it's still popping
even if I manually clear the cell values to no avail...
 
Upvote 0
What cell values did you clear?
column B onwards prior to running the code! but still not empty warning keeps popping up...
what I did is issued display alert to off/false, for now it's working until we figure out where is that warning coming from....
again Big Tango! (Big Thanks)
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,521
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top