Please see the attachment for understanding the output of my query (I have mentioned header in the image for your understanding by in actual, header is blank for the output).
My code runs only for first iteration of k and then I get the error "Subscript out of range at the line mentioned below. Also, my remove duplicates is not giving the required output in the code. is it because of blank spaces or what and how can I resolve these two issues?
I am using arrays for the very first time.
Dim MoNameArr
Dim arr()
Dim ColLtrg, ColLtrgp, GPLLastCol, GPLLastRow as Long
i = 0
ReDim arr(0)
With wsg
For k = 2 To GPLLastRow
.Cells(k, GPLLastCol + 1).Value = .Cells(k, 2).Value & .Cells(k, 3).Value
If .Cells(k, 4).Value = .Cells(k, 8).Value And .Cells(k, 4).Value = .Cells(k, 9).Value Then
i = k - 2
arr(i) = .Cells(k, 2).Value 'Subscript out of range error
.Cells(k, GPLLastCol + 2).Value = arr(i)
ReDim Preserve arr(i)
End If
Next k
ColLtrg = Replace(.Cells(1, GPLLastCol + 2).Address(True, False), "$1", "")
.Range(ColLtrg & "1:" & ColLtrg & GPLLastRow).RemoveDuplicates Columns:=1, Header:=xlNo
MoNameArr = .Range("AD1:AD" & GetLastRow(wsg, GPLLastCol + 2))
End With
For Each Item In MoNameArr
'Do something
Next Item
Public Function GetLastCol(ByVal ws As Worksheet, rowNum As Long) As Long
With ws
GetLastCol = .Cells(rowNum, Columns.Count).End(xlToLeft).Column
End With
End Function
Public Function GetLastRow(ByVal ws As Worksheet, colNum As Long) As Long
With ws
GetLastRow = .Cells(Rows.Count, colNum).End(xlUp).Row
End With
End Function