Macro to copy cell value if collumn contains a "t"

Nenza

New Member
Joined
Apr 23, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I have an excel file, which contains property ID in column A and a value in column B. The IDs are repeated. I want to move the value to one row, so there will be no repetitions. Here is the data and here is what I want to achieve. I have made a helping column with value T, which appears if there is a duplicated ID in a column below. Could I ask with help on this?
Data:

HotelConcept identifier
AEAUH1YSBB
AEAUH1YSROt
AEDXBCNLBB
AEDXBCNLROt
AEDXBMARBB
AEDXBMARROt
AEDXBMEDBB
AEDXBMEDROt
AEDXBMOTBB
AEDXBMOTROt
AEDXBSLOBB
AEDXBSLOROt
AEFJRRSTBB
AEFJRRSTROt
AMEVN1BB
AMEVN1ROt
ATLNZ1BB
ATLNZ1ROt
ATSZGALTBB
ATSZGALTROt
ATVIESTYBB
ATVIESTYROt
AUNSSYDHSTBB
AUNSSYDHSTROt
AUNSSYDPLZBB
AUNSSYDPLZROt
AUVIMELFLGBB
AUVIMELFLGROt
AZBAK1BB
AZBAK1ROt
BDCGPBAYBB
BDDHAGARBB
BDDHAGARROt
BEANR1BB
BEANR1ROt
BEANRASTBB
BEANRASTROt
BEANRBERBB
BEANRBERROt
BEBRUEURBB
BEBRUEURROt
BEBRUMIDBB
BEBRUMIDROt
BEBRUROYBB
BEBRUROYROt
BELEU1BB
BELEU1ROt
BELGGAIRBB
BELGGAIRROt
BEQHA1BB
BEQHA1ROt
BEQHAHASBB
BEQHAHASROt
BESPABALBB
BESPABALHBt
BESPABALROt
BESPAPALBB
BHBAHDIPBB
BHBAHDIPROt
BNBWNDARBB
BNBWNDARROt
CAONIAG1BB
CAONIAG1ROt
CGBZVMBABB
CGBZVMBAROt
CHANDREUBB
CHBSL1BB
CHBSL1ROt
CHQGL1BB
CHQGL1ROt
CHQLJ1BB
CHQLJ1HBt
CHQLJ1ROt
CHZRHAR2BB
CHZRHAR2ROt
CHZRHAR3BB
CHZRHAR3ROt

And what I want:

HotelConcept identifier
AEAUH1YSBBRO
AEDXBCNLBBRO
AEDXBMARBBRO
AEDXBMEDBBRO
AEDXBMOTBBRO
AEDXBSLOBBRO
AEFJRRSTBBRO
AMEVN1BBRO
ATLNZ1BBRO
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Have a try my macro. You don't need to use a help column (something's wrong with the BESPABAL hotel :p) So, the best thing is that all the columns to the right of "B" be empty. This also means that there will be no limit to how many hotel repetitions are allowed. In case this isn't possible, in the macro you will find the changes to be made.
VBA Code:
Option Explicit
Sub CompactDuplicates()
    Dim rw     As Long
    Dim cnt    As Long
    Application.ScreenUpdating = False
    With ActiveSheet
        .Range("A1").CurrentRegion.Sort Key1:=.Range("A1"), Header:=xlYes 'sorts column A
        For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 3 Step -1
            '--- if there is no data in columns after "B" then use -------------------------------------
            If .Cells(rw, 1) = .Cells(rw - 1, 1) Then
                cnt = cnt + 1
                .Cells(rw, 2).Resize(, cnt).Copy .Cells(rw - 1, Columns.Count).End(xlToLeft).Offset(, 1)
                .Cells(rw, 1).EntireRow.Delete
            Else
                cnt = 0
            End If
            '-------------------------------------------------------------------------------------------
            '--- else if there is data after column "B" limit the columns (as per your example "G") use ---
            'If .Cells(rw, 1) = .Cells(rw - 1, 1) Then
            '    .Cells(rw, 2).Resize(, 5).Copy .Cells(rw - 1, 7).End(xlToLeft).Offset(, 1)
            '    .Cells(rw, 1).EntireRow.Delete
            'End If
            '----------------------------------------------------------------------------------------------
        Next rw
    End With
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
 
Upvote 0
Hi, a VBA demonstration as a starter according to the initial attachment as it is - nothing wrong - where column G is useless :​
VBA Code:
Sub Demo1()
    With [A1].CurrentRegion.Rows
        If .Count = 1 Or .Columns.Count > 2 Then Beep: Exit Sub
        With .Item("2:" & .Count):  V = .Value2:  .Clear:  End With
    End With
    With CreateObject("Scripting.Dictionary")
        For R& = 1 To UBound(V):  .Item(V(R, 1)) = .Item(V(R, 1)) & IIf(.Item(V(R, 1)) > "", vbTab, "") & V(R, 2):  Next
        [A2:B2].Resize(.Count) = Application.Transpose(Array(.Keys, .Items))
        [B2].Resize(.Count).TextToColumns [B2], xlDelimited, , , True
       .RemoveAll
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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