Macro that transposes and concatenates

coops711

New Member
Joined
Jul 11, 2017
Messages
5
Hello

I am trying to write a macro that joins strings of text together whilst also transposing. I'll try provide an example of what I'm after:

So here is table 1, where we write the campaign name, the ad group name and its associated keyword(s).


A B C D
Campaign Name:Campaign 1Campaign 2Campaign 3
Adgroup Name:Ad group 1Ad group 2Ad group 3
Keywords:Shoesdressestowels
blue shoesblue dressesblue towels
pink towels
red towels



What I want is a macro that will assign each keyword with its respective campaign name/ad group, per line, as shown:






<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

A B C
Campaign NameAdgroup nameKeyword
Campaign 1Ad group 1Shoes
Campaign 1Ad group 1blue shoes
Campaign 2Ad group 2dresses
Campaign 2Ad group 2blue dresses
Campaign 3Ad group 3towels
Campaign 3Ad group 3blue towels
Campaign 3Ad group 3pink towels
Campaign 3Ad group 3red towels

<colgroup><col><col><col></colgroup><tbody>
</tbody>


Thanks for your help!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this in a copy of your workbook.
Code:
Sub CampaignDetails()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, r As Long, uba As Long
  
  With Range("A1").CurrentRegion
    a = .Value
    uba = UBound(a)
    ReDim b(1 To Rows.Count, 1 To 3)
    For j = 2 To UBound(a, 2)
      i = 2
      Do
        i = i + 1
        If a(i, j) = "" Then
          i = uba
        Else
          r = r + 1
          b(r, 1) = a(1, j)
          b(r, 2) = a(2, j)
          b(r, 3) = a(i, j)
        End If
      Loop Until i = uba
    Next j
    Application.ScreenUpdating = False
    With .Offset(, .Columns.Count + 1).Resize(, 3)
      .Value = Array("Campaign Name", "Adgroup name", "Keyword")
      .Offset(1).Resize(r).Value = b
      .EntireColumn.AutoFit
    End With
    Application.ScreenUpdating = True
  End With
End Sub

My sample data (A:D) and results (F:H)


Book1
ABCDEFGH
1Campaign Name:Campaign 1Campaign 2Campaign 3Campaign NameAdgroup nameKeyword
2Adgroup Name:Ad group 1Ad group 2Ad group 3Campaign 1Ad group 1Shoes
3Keywords:ShoesdressestowelsCampaign 1Ad group 1blue shoes
4blue shoesblue dressesblue towelsCampaign 2Ad group 2dresses
5pink towelsCampaign 2Ad group 2blue dresses
6red towelsCampaign 3Ad group 3towels
7Campaign 3Ad group 3blue towels
8Campaign 3Ad group 3pink towels
9Campaign 3Ad group 3red towels
Campaign
 
Upvote 0
Try this in a copy of your workbook.
Code:
Sub CampaignDetails()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, r As Long, uba As Long
  
  With Range("A1").CurrentRegion
    a = .Value
    uba = UBound(a)
    ReDim b(1 To Rows.Count, 1 To 3)
    For j = 2 To UBound(a, 2)
      i = 2
      Do
        i = i + 1
        If a(i, j) = "" Then
          i = uba
        Else
          r = r + 1
          b(r, 1) = a(1, j)
          b(r, 2) = a(2, j)
          b(r, 3) = a(i, j)
        End If
      Loop Until i = uba
    Next j
    Application.ScreenUpdating = False
    With .Offset(, .Columns.Count + 1).Resize(, 3)
      .Value = Array("Campaign Name", "Adgroup name", "Keyword")
      .Offset(1).Resize(r).Value = b
      .EntireColumn.AutoFit
    End With
    Application.ScreenUpdating = True
  End With
End Sub

My sample data (A:D) and results (F:H)

ABCDEFGH
1Campaign Name:Campaign 1Campaign 2Campaign 3Campaign NameAdgroup nameKeyword
2Adgroup Name:Ad group 1Ad group 2Ad group 3Campaign 1Ad group 1Shoes
3Keywords:ShoesdressestowelsCampaign 1Ad group 1blue shoes
4blue shoesblue dressesblue towelsCampaign 2Ad group 2dresses
5pink towelsCampaign 2Ad group 2blue dresses
6red towelsCampaign 3Ad group 3towels
7Campaign 3Ad group 3blue towels
8Campaign 3Ad group 3pink towels
9Campaign 3Ad group 3red towels

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Campaign


This is great. Thanks - exactly what im looking for. I haven't done a macro like this before.


Could you please help me edit this in the following two ways:

1) I simplified the data slightly to make it easier to read, in the actual sheet there is a gap between the "campaign name" row and the other two rows.

Specifically, the campaign name row is at A14, the ad group name row is at A32, keyword row is at A33

2) Could the data be pasted into a new sheet?
 
Upvote 0
Best not to fully quote long posts as it makes the thread harder to read/navigate and just occupies storage space needlessly. If you want to quote, quote small, relevant parts only.

Could you please help me edit this in the following two ways:

1) I simplified the data slightly to make it easier to read, in the actual sheet there is a gap between the "campaign name" row and the other two rows.

Specifically, the campaign name row is at A14, the ad group name row is at A32, keyword row is at A33

2) Could the data be pasted into a new sheet?
Try this version with a copy of your workbook.

Code:
Sub CampaignDetails_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, r As Long, uba As Long, lr As Long
  
  lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, SearchFormat:=False).Row
  a = Range("A14:A" & lr).Resize(, Cells(14, Columns.Count).End(xlToLeft).Column).Value
  uba = UBound(a)
  ReDim b(1 To Rows.Count, 1 To 3)
  For j = 2 To UBound(a, 2)
    i = 19
    Do
      i = i + 1
      If a(i, j) = "" Then
        i = uba
      Else
        r = r + 1
        b(r, 1) = a(1, j)
        b(r, 2) = a(19, j)
        b(r, 3) = a(i, j)
      End If
    Loop Until i = uba
  Next j
  Application.ScreenUpdating = False
  Sheets.Add After:=ActiveSheet
  With Range("A1:C1")
    .Value = Array("Campaign Name", "Adgroup name", "Keyword")
    .Offset(1).Resize(r).Value = b
    .EntireColumn.AutoFit
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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