简体   繁体   中英

Excel VBA How to Loop Through a Dataset and Skip Every Other Row?

I have in Excel worksheet in column A a list of employee Numbers. Each number shows up twice in the list with same name but different company. I want to loop each number in the list and compare it to another list to check in which comapny the employee works now.

for example:

112 Rafael ABC
112 Rafael NBS
223 Jose ACM
223 Jose NBS
345 Dave ACM
345 Dave NBS

I want to check Rafael's Number once and then Jose's and the Dave's. I need help with the loop itself - how to skip a row every time.

Sub RemoveDuplicats()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
Application.Calculation = xlManual

Dim MainWB As Workbook
Dim MainSheet As Worksheet
Dim MLRow As Long
Dim WS As Worksheet
Dim emp As Range

Set MainWB = ThisWorkbook
Set MainSheet = MainWB.Worksheets("Main")

'Here is not relevant code
.
.
.
With MainSheet
     MLRow = .Cells(Rows.Count, "A").End(xlUp).Row
     Set emp = .Range("A1:A" & MLRow)
    .Range("A1").PasteSpecial Paste:=xlPasteValues
    .Range("A1:M" & MLRow).Sort key1:=Range("A2:A" & MLRow), order1:=xlAscending, Header:=xlNo
End With

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
End Sub

I dont know, if this is what you want and the data is exactly as you have shown.

With MainSheet
     MLRow = .Cells(Rows.Count, "A").End(xlUp).Row
     Set emp = .Range("A1:A" & MLRow)
    .Range("A1").PasteSpecial Paste:=xlPasteValues
    .Range("A1:M" & MLRow).Sort key1:=Range("A2:A" & MLRow), order1:=xlAscending, Header:=xlNo
End With
'Loop the cells and skip 1
For x = 1 To MLRow
     'Your Code
x = x + 1
Next x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM