Hi guys
For the last two days I've been trying to write a sub that'll remove all zeros from a given range. The range could look like this:
A
1: 0
2: 2
3: 0
4: 7
5: 0
I have written the following piece of code to remove the zeros and re-print the range without the zeros somewhere else in the sheet:
No matter how I tweak the code, I only manage to get the first or last value from the range re-printed. Right now the code re-prints the first value in the range (8) 35 times. I have tried Googling and going through numerous forums, but I haven't found an answer yet. I'm quite new to VBA and programming in general, therefore I hope one of you might spot my error
Thanks.
Best regards
jlp87
For the last two days I've been trying to write a sub that'll remove all zeros from a given range. The range could look like this:
A
1: 0
2: 2
3: 0
4: 7
5: 0
I have written the following piece of code to remove the zeros and re-print the range without the zeros somewhere else in the sheet:
Code:
Sub PrintArray(Data As Variant, Cl As Range)
Cl.Resize(UBound(Data, 1)) = Data
End Sub
Public Sub Test()
Dim MyArray() As Variant
ReDim MyArray(1 To Range("N273").Value) 'Right now "N273"'s value is 35
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim i As Integer
Set wb = Application.Workbooks(1)
Set ws = wb.Sheets(1)
Set rng = ws.Range("J1275", "J2612")
i = 0
For Each cell In rng.Cells
If cell > 0 Then
i = i + 1
MyArray(i) = cell
End If
Next cell
PrintArray MyArray, Range("L323")
End Sub
No matter how I tweak the code, I only manage to get the first or last value from the range re-printed. Right now the code re-prints the first value in the range (8) 35 times. I have tried Googling and going through numerous forums, but I haven't found an answer yet. I'm quite new to VBA and programming in general, therefore I hope one of you might spot my error

Thanks.
Best regards
jlp87