Hello All,
I recently begin to learn VBA and I've stuck somewhere in the code and I couldn't figure out how to solve it. I hope you can help me. I'll try to explain as easy as possible. I hope I can explain correctly.Sorry english is not my first language.
What I want to do is there is a spare parts name in excel like below. When spare parts name changed I want to insert a row. Below code works fine but because of cells inserted,code stop working somewhere in the middle and not going all to the end. although I try to make it dynamic with cellnum=cellnum+1,counter variable only consider its first value begging of the code.
Thanks in advance for everybody.
A
A1 abc
A2 abc
A3 bca
A4 bca
I recently begin to learn VBA and I've stuck somewhere in the code and I couldn't figure out how to solve it. I hope you can help me. I'll try to explain as easy as possible. I hope I can explain correctly.Sorry english is not my first language.
What I want to do is there is a spare parts name in excel like below. When spare parts name changed I want to insert a row. Below code works fine but because of cells inserted,code stop working somewhere in the middle and not going all to the end. although I try to make it dynamic with cellnum=cellnum+1,counter variable only consider its first value begging of the code.
Thanks in advance for everybody.
A
A1 abc
A2 abc
A3 bca
A4 bca
VBA Code:
Sub Test()
Dim counter As Integer
Dim cellnum
cellnum = Cells(Rows.Count, 1).End(xlUp).Row
For counter = 1 To cellnum
If Range("A" & counter) <> Range("A" & counter + 1) Then
Rows(counter + 1).Insert Shift:=xlDown
counter = counter + 1
cellnum = cellnum + 1
End If
Next counter
End Sub