How to add to rather than replace using this code snippet

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
How to modify this code so that it actually adds to the list of results rather than replace:
Code:
Sub EncID_IP()
'.:: Extract EncID
'
    Dim lRow As Long ' East to West
    Dim bCount As Byte '1,2,3
    Dim sServiceHit As String 'Service used to find EncID
    Dim rRange As Range, rCell As Range 'What am I cycling through?
    Dim ws As Worksheet 'Canvas

    Sheets("IP Breakout").Select
    Set ws = Sheets("Results IP") 'dumping ground
    ws.Range("A1") = "EncID" 'extract data
    Set rRange = Range("A2", Range("A" & Rows.Count).End(xlUp)) 'Create a dynamic range to search

    lRow = 2 ' go to 2 deep
    For Each rCell In rRange 'look at every cell in the dynamic range
        If sServiceHit <> rCell.Offset(, 4) Then
           sServiceHit = rCell.Offset(, 4)
           bCount = 0
        End If
        If bCount < 2 Then ' if  X < 2
            ws.Cells(lRow, "A") = rCell 'Canvas cell A + X
            bCount = bCount + 1 'Add to my count by 1
            lRow = lRow + 1 ' Add to my row by 1
        End If
    Next rCell

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How to modify this code so that it actually adds to the list of results rather than replace:
Code:
Sub EncID_IP()
'.:: Extract EncID
'
    Dim lRow As Long ' East to West
    Dim bCount As Byte '1,2,3
    Dim sServiceHit As String 'Service used to find EncID
    Dim rRange As Range, rCell As Range 'What am I cycling through?
    Dim ws As Worksheet 'Canvas

    Sheets("IP Breakout").Select
    Set ws = Sheets("Results IP") 'dumping ground
    ws.Range("A1") = "EncID" 'extract data
    Set rRange = Range("A2", Range("A" & Rows.Count).End(xlUp)) 'Create a dynamic range to search

    lRow = 2 ' go to 2 deep
    For Each rCell In rRange 'look at every cell in the dynamic range
        If sServiceHit <> rCell.Offset(, 4) Then
           sServiceHit = rCell.Offset(, 4)
           bCount = 0
        End If
        If bCount < 2 Then ' if  X < 2
             [COLOR=red]ws.Cells(lRow, "A") = ws.Cells(lRow, "A") + rCell[/COLOR] 'Canvas cell A + X
            bCount = bCount + 1 'Add to my count by 1
            lRow = lRow + 1 ' Add to my row by 1
        End If
    Next rCell

End Sub
Just guessing because I don't really know what the code was designed to do, and it would help if you posted some sample data to illustrate.

But, if you change the red line as indicated, does it do anything useful for you?
 
Upvote 0
When updating the original (source) table, do you replace all data with new set or you just append it (add records at the bottom)?

Please explain this point because when you want to add rather than replace it means that the old set has been replaced with another one.

By the way, this is part of your previous thread, you could have just posted there.
 
Upvote 0
The source table is the same data, but I am using a different sort filter which in effect creates a new source table. This is true because the current code checks to see if the Service Hit is the same and pulls the first 2 it runs across. I need it to append to the previous list what is finds.

Mohammad Basem you are correct about where I should have posted this. I will connect the two once this has post has been made complete.
 
Upvote 0
But in this case you will have duplicates; the first list and subsequent lists of same data. Is this the case?
 
Upvote 0

Forum statistics

Threads
1,215,519
Messages
6,125,297
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top