I want to append names from column g to the end of column A and then remove duplicates

danyu

New Member
Joined
Nov 22, 2022
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
Hi Everyone,

I want to append names from column g to the end of column A and then remove duplicates. I was able to do it this way below, but I feel like this way will be prone to glitching or something. Is there a better way to do this? Also, is there a simple way to have vlookup match with names that have Jr. Sr. etc? I've read that it can be done with vlookup wildcards, but it doesnt seem to work for me. Thanks in advance!

Sheets("Names").Select
Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
Selection.End(xlDown).Select
ActiveWindow.SmallScroll Down:=18
Range("A700").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("A:A").Select
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$1500").RemoveDuplicates Columns:=1, Header:=xlYes
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi danyu,

if sheets and columns are always the same for the code to run on:

VBA Code:
Sub MrE_1222800_161460A()
Dim lngCount As Long
Dim lngIndex As Long
Dim strSheets As String
Dim strCols As String
Dim varSheets As Variant
Dim varCols As Variant

'adjust sheetnames and columns
strSheets = "First|Last|Home|Everywhere"
strCols = "G|I|C|L"

varSheets = Split(strSheets, "|")
varCols = Split(strCols, "|")
If UBound(varCols) <> UBound(varSheets) Then
  MsgBox "Please check sheetnames and Columns as they do not have the same number", vbInformation, "Ending here..."
  Exit Sub
End If

For lngIndex = LBound(varSheets) To UBound(varSheets)
  With Worksheets(varSheets(lngIndex))
     lngCount = .Cells(.Rows.Count, varCols(lngIndex)).End(xlUp).Row - 1
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(lngCount, 1).Value = .Cells(2, varCols(lngIndex)).Resize(lngCount, 1).Value
    .Columns(1).RemoveDuplicates Columns:=1, Header:=xlYes
  End With
Next lngIndex
End Sub

Ciao,
Holger
 
Upvote 0
For specific sheets:
VBA Code:
Sub myFunction()
  Dim gRow As Integer, aRow As Integer, c As Integer
  Dim wSheets As Variant
  Dim ws As Variant
 
  wsheets = Array("Blitz", "CBS")

  For Each ws In wSheets
    With ws
      c = Application.WorksheetFunction.Match("Names", .Rows(1), 0) 'Replace with your column header
      gRow = .Cells(Rows.Count, c).End(xlUp).Row
      aRow = .Cells(Rows.Count, 1).End(xlUp).Row

      For i = 2 To gRow
         .Cells((aRow + i) - 1, 1).Value =  .Cells(i, c).Value
      Next

      .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    End With
  Next ws
End Sub
For all sheets:
VBA Code:
Sub myFunction()
    Dim gRow As Integer, aRow As Integer, c As Integer
  Dim ws As Worksheet

  For Each ws In ThisWorkbook.Worksheets
    With ws
      c = Application.WorksheetFunction.Match("Names", .Rows(1), 0) 'Replace with your column header
      gRow = .Cells(Rows.Count, c).End(xlUp).Row
      aRow = .Cells(Rows.Count, 1).End(xlUp).Row

      For i = 2 To gRow
         .Cells((aRow + i) - 1, 1).Value =  .Cells(i, c).Value
      Next

      .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    End With
  Next
End Sub
 
Upvote 0
For specific worksheets:
VBA Code:
Sub myFunction()
  Dim gRow As Integer, aRow As Integer
  Dim wsheets As Variant
  Dim ws As Variant
 
  wsheets = Array("Blitz", "CBS")

  For Each ws In wsheets
    With ws
      gRow = .Cells(Rows.Count, 7).End(xlUp).Row
      aRow = .Cells(Rows.Count, 1).End(xlUp).Row

      For i = 2 To gRow
         .Cells((aRow + i) - 1, 1).Value =  .Cells(i, 7).Value
      Next

      .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    End With
  Next ws
End Sub
To loop through all worksheets:
VBA Code:
Sub myFunction()
  Dim gRow As Integer, aRow As Integer
  Dim ws As Worksheet

  For Each ws In ThisWorkbook.Worksheets
    With ws
      gRow = .Cells(Rows.Count, 7).End(xlUp).Row
      aRow = .Cells(Rows.Count, 1).End(xlUp).Row

      For i = 2 To gRow
         .Cells((aRow + i) - 1, 1).Value =  .Cells(i, 7).Value
      Next

      .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    End With
  Next
End Sub
When I tried the first code (for specific sheets) I got this issue:

1669406911255.png


1669406935530.png


Do you have any ideas on how to fix this please? Thanks!
 
Upvote 0
Hi danyu,

if sheets and columns are always the same for the code to run on:

VBA Code:
Sub MrE_1222800_161460A()
Dim lngCount As Long
Dim lngIndex As Long
Dim strSheets As String
Dim strCols As String
Dim varSheets As Variant
Dim varCols As Variant

'adjust sheetnames and columns
strSheets = "First|Last|Home|Everywhere"
strCols = "G|I|C|L"

varSheets = Split(strSheets, "|")
varCols = Split(strCols, "|")
If UBound(varCols) <> UBound(varSheets) Then
  MsgBox "Please check sheetnames and Columns as they do not have the same number", vbInformation, "Ending here..."
  Exit Sub
End If

For lngIndex = LBound(varSheets) To UBound(varSheets)
  With Worksheets(varSheets(lngIndex))
     lngCount = .Cells(.Rows.Count, varCols(lngIndex)).End(xlUp).Row - 1
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(lngCount, 1).Value = .Cells(2, varCols(lngIndex)).Resize(lngCount, 1).Value
    .Columns(1).RemoveDuplicates Columns:=1, Header:=xlYes
  End With
Next lngIndex
End Sub

Ciao,
Holger
Hi Holger,

I tried your code and I got this error:

1669407209401.png

1669407229081.png

Could you please help me figure out what I did wrong?
Thanks!
 
Upvote 0
Change this line
VBA Code:
wsheets = Array("Blitz", "CBS")
to this
VBA Code:
wSheets = Array("Blitz", "CBS")
capital S

Change this line
VBA Code:
wsheets = Array("Blitz", "CBS")
to this
VBA Code:
wSheets = Array("Blitz", "CBS")
capital S
Hi, I really appreciate your patience in helping me.

I've changed to capital S but I still get the same error. (I get an error with your newer code with the headers as well).

1669416680359.png


Thanks
Daniel
 
Upvote 0
Hi Daniel,

1004 is the error which is causes the most trouble to spot. It would be a good idea to display the look of one of the sheets which raises the error (if possible using XL2BB) or giving us a download to a copy of the workbook on a free downloader site like dropbox.

@Flashbond:

maybe the error lies in using only ws
Rich (BB code):
  For Each ws In wSheets
    With ws
as ws should only hold the name of a worksheet but not a reference to it like using
Rich (BB code):
  For Each ws In wSheets
    With Worksheets(ws)
My version of Excel2019 didn't like the first sniplet but accepted the second.

Ciao,
Holger
 
Upvote 0
Hi Daniel,

1004 is the error which is causes the most trouble to spot. It would be a good idea to display the look of one of the sheets which raises the error (if possible using XL2BB) or giving us a download to a copy of the workbook on a free downloader site like dropbox.

@Flashbond:

maybe the error lies in using only ws
Rich (BB code):
  For Each ws In wSheets
    With ws
as ws should only hold the name of a worksheet but not a reference to it like using
Rich (BB code):
  For Each ws In wSheets
    With Worksheets(ws)
My version of Excel2019 didn't like the first sniplet but accepted the second.

Ciao,
Holger
Yeah, they are just strings aren't they? Sometimes I make such mistakes. @danyu try like that:
VBA Code:
Sub myFunction()
  Dim gRow As Integer, aRow As Integer, c As Integer
  Dim wSheets As Variant
  Dim ws As Variant
 
  wsheets = Array("Blitz", "CBS")

  For Each ws In wSheets
    With Worksheets(ws)
      c = Application.WorksheetFunction.Match("Names", .Rows(1), 0) 'Replace with your column header
      gRow = .Cells(Rows.Count, c).End(xlUp).Row
      aRow = .Cells(Rows.Count, 1).End(xlUp).Row

      For i = 2 To gRow
         .Cells((aRow + i) - 1, 1).Value =  .Cells(i, c).Value
      Next

      .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    End With
  Next ws
End Sub
 
Upvote 0
Solution
Yeah, they are just strings aren't they? Sometimes I make such mistakes. @danyu try like that:
VBA Code:
Sub myFunction()
  Dim gRow As Integer, aRow As Integer, c As Integer
  Dim wSheets As Variant
  Dim ws As Variant
 
  wsheets = Array("Blitz", "CBS")

  For Each ws In wSheets
    With Worksheets(ws)
      c = Application.WorksheetFunction.Match("Names", .Rows(1), 0) 'Replace with your column header
      gRow = .Cells(Rows.Count, c).End(xlUp).Row
      aRow = .Cells(Rows.Count, 1).End(xlUp).Row

      For i = 2 To gRow
         .Cells((aRow + i) - 1, 1).Value =  .Cells(i, c).Value
      Next

      .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    End With
  Next ws
End Sub
Hi, sorry I've been swamped with other work related stuff and I had to put this on hiatus. I just took a look at it now, and it seems to work! Thanks so much! I'll follow up with any questions but I think this might be it.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,865
Messages
6,121,988
Members
449,060
Latest member
mtsheetz

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