简体   繁体   中英

Using a VBA function, How can I fix my loop to copy cell values to a new row based on a matching cell values in a column?

I am attempting to clean up some CSV report files that contain network asset information.

Column A contains PluginIDs for the various values. I want to copy the offset (0, x) and paste into a cell further up in the same column, based on matching host IPs in column B. Here is what I have so far.

Sub finalDataMove()

Dim LR As Long

LR = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LR
    If Range("A" & i).Value = "11936" Then
        Range("A" & i).Offset(0, 3).Copy
        **"Help me please!"**.PasteSpecial xlPasteAll
    End If

    If Range("A" & i).Value = "12053" Then
        Range("A" & i).Offset(0, 4).Copy
        **"Help me please!"**.PasteSpecial xlPasteAll
    End If

Next i

Application.CutCopyMode = False


End Sub

Your code can be written in a better way but i just tried to correct your code. i hope it will help you.

Sub finalDataMove()

Dim LR          As Long
Dim varCol      As Variant

LR = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LR
    If Range("A" & i).Value = "11936" Then
        varCol = Application.Match(Range("B" & i).Value, [B:B], 0)            
        Range("A" & i).Offset(0, 3).Copy
        Range("A" & varCol).Offset(0, 3).PasteSpecial xlPasteAll
    End If

    If Range("A" & i).Value = "12053" Then
        varCol = Application.Match(Range("B" & i).Value, [B:B], 0)           
        Range("A" & i).Offset(0, 4).Copy
       Range("A" & varCol).Offset(0, 4).PasteSpecial xlPasteAll
    End If

Next i

Application.CutCopyMode = False


End Sub

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