Macro To Copy Down UserID

jo15765

Well-known Member
Joined
Sep 23, 2011
Messages
591
Hello - I have an excel worksheet where in Column A it has a user id, then column B it shows websites visited. It's in an unusable format currently, and I'd like to get it usable. Sample data is like this
Code:
userID     Name/site
abc123    Jack Johnson
               www.espn.com
               aol
               google
xyz789    Bob Godfrey
               msnbc
               toolscool.com
era292    Jason McKnight
               google.com
               www.google.com

What I need is to remove the actual name and add the userId to each site visited so that the excel sheet will look like this
Code:
abc123    www.espn.com
abc123    aol
abc123   google
xyz789    msnbc
xyz789    toolscool.com
era292    google.com
era292    www.google.com

What is a macro/formula/vba that will achieve this result?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this:
Results in D2 onwards

VBA Code:
Sub CopyUserID()
  Dim i As Long, j As Long
  Dim ant As String
  j = 2
  For i = 2 To Range("B" & Rows.Count).End(3).Row
    If ant <> Range("A" & i).Value And Range("A" & i).Value = "" Then
      Range("D" & j).Value = ant
      Range("E" & j).Value = Range("B" & i).Value
      j = j + 1
    End If
    If Range("A" & i).Value <> "" Then ant = Range("A" & i).Value
  Next
End Sub
 
Upvote 0
Another option

VBA Code:
Sub InsertIDs()
  Dim rA As Range
  
  For Each rA In Range("A2", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).Areas
    rA.Value = rA.Cells(0, 1).Value
    Rows(rA.Row - 1).Delete
  Next rA
End Sub

Before:

jo15765.xlsm
AB
1userIDName/site
2abc123Jack Johnson
3www.espn.com
4aol
5google
6xyz789Bob Godfrey
7msnbc
8toolscool.com
9era292Jason McKnight
10google.com
11www.google.com
Sheet1


After:

jo15765.xlsm
AB
1userIDName/site
2abc123www.espn.com
3abc123aol
4abc123google
5xyz789msnbc
6xyz789toolscool.com
7era292google.com
8era292www.google.com
9
10
11
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,616
Members
449,238
Latest member
wcbyers

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