Infinite Loop

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a column of data in column A. All I want to do is add zeros if the length is less than 5, eg 1 becomes 00001.

This code gets stuck in an infinite loop:

Code:
Public Sub UsingArray()
     
    Dim OrigVals As Variant
 
    OrigVals = Sheet1.Cells(1, 1).CurrentRegion.Value
 
    Dim a As Class1
    Set a = New Class1
 
    With a
     
        Set .ws = Sheet1
     
        .DataArray = OrigVals
     
        Call .CreateList

    End With
 
End Sub

This is Class1:

Code:
Private pDataArray As Variant
 
    Private pws As Worksheet

Public Property Get DataArray() As Variant

    DataArray = pDataArray
 
End Property

Public Property Let DataArray(ByVal DArray As Variant)

    pDataArray = DArray
 
End Property

Public Property Get ws() As Worksheet

    Set ws = pws
 
End Property

Public Property Set ws(ByVal w As Worksheet)

    Set pws = w
 
End Property

Public Sub CreateList()
    
    Dim DataArrayRows As Long
 
    DataArrayRows = UBound(Me.DataArray, 1)
 
    Dim Counter As Long
 
    For Counter = 1 To DataArrayRows
 
        If Len(Me.DataArray(Counter, 1)) < 5 Then
     
            Do Until Len(Me.DataArray(Counter, 1)) = 5
         
                Me.DataArray(Counter, 1) = "0" & Me.DataArray(Counter, 1)
         
            Loop
     
        End If
    
    Next Counter
 
    Me.ws.Cells(1, 3).Resize(DataArrayRows, 1).Value = Me.DataArray
 
End Sub

I have discovered I have to change this line from:

Code:
    Me.DataArray(Counter, 1) = "0" & Me.DataArray(Counter, 1)

to:

Code:
    pDataArray(Counter, 1) = "0" & Me.DataArray(Counter, 1)

to make it work.


However, the following code (using strings) does work:

Code:
Public Sub UsingString()

    Dim Val As String
 
    Val = Sheet1.Cells(1, 1).Value

    Dim b As Class2
    Set b = New Class2
 
    With b
 
        Set .ws = Sheet1
     
        .Val = Val
     
        Call .CreateList
 
    End With
 
End Sub

Class2:

Code:
    Private pVal As String
    Private pws As Worksheet

Public Property Get Val() As String

    Val = pVal
 
End Property

Public Property Let Val(ByVal V As String)

    pVal = V
 
End Property

Public Property Get ws() As Worksheet

    Set ws = pws
 
End Property

Public Property Set ws(ByVal w As Worksheet)

    Set pws = w
 
End Property

Public Sub CreateList()

    If Len(Me.Val) < 5 Then
     
        Do Until Len(Me.Val) = 5
     
            Me.Val = "0" & Me.Val
     
        Loop
 
    End If
    
    Me.ws.Cells(1, 5).Value = Me.Val
 
End Sub

Can someone pleae explain why I needed to change:

Code:
Me.DataArray(Counter, 1) = "0" & Me.DataArray(Counter, 1)

to

Code:
p.DataArray(Counter, 1) = "0" & Me.DataArray(Counter, 1)

to make it work using arrays but I didn't have to change anything if I use string?

Thanks
 
For example:

VBA Code:
Option Explicit

Private pDataArray As Variant
 
    Private pws As Worksheet

Public Property Get DataArray() As Variant

    DataArray = pDataArray
 
End Property

Public Property Let DataArray(ByVal DArray As Variant)

    pDataArray = DArray
 
End Property
Property Get DataArrayItem(ByVal RowIndex As Variant, ByVal ColIndex As Variant) As Variant

    DataArrayItem = pDataArray(RowIndex, ColIndex)

End Property

Public Property Let DataArrayItem(ByVal RowIndex As Variant, ByVal ColIndex As Variant, MArray As Variant)

    pDataArray(RowIndex, ColIndex) = MArray
  
End Property
Public Property Get ws() As Worksheet

    Set ws = pws
 
End Property

Public Property Set ws(ByVal w As Worksheet)

    Set pws = w
 
End Property

Public Sub CreateList()
   
    Dim DataArrayRows As Long
 
    DataArrayRows = UBound(Me.DataArray, 1)
 
    Dim Counter As Long
 
    For Counter = 1 To DataArrayRows
 
        If Len(Me.DataArrayItem(Counter, 1)) < 5 Then
    
            Do Until Len(Me.DataArrayItem(Counter, 1)) = 5
        
                Me.DataArrayItem(Counter, 1) = "0" & Me.DataArrayItem(Counter, 1)
        
            Loop
    
        End If
   
    Next Counter
 
    Me.ws.Cells(1, 3).Resize(DataArrayRows, 1).Value = Me.DataArray
 
End Sub
Many thanks
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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