tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,913
- Office Version
- 365
- 2019
- Platform
- 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:
This is Class1:
I have discovered I have to change this line from:
to:
to make it work.
However, the following code (using strings) does work:
Class2:
Can someone pleae explain why I needed to change:
to
to make it work using arrays but I didn't have to change anything if I use string?
Thanks
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