How to transpose data with VBA

SydneyLucy

New Member
Joined
Sep 12, 2010
Messages
4
I have an excel sheet with columns of data that then needs to be transposed into rows, however each column is a different length and there are loads of columns so to do each one individually takes too long. I have managed to get the keywords over and walking through the macro it brings the top 2 titles over but the pulls down the wrong data.

Original columns look like this, with various numbers of keywords

campaign1
adgroup1
keyword1
keyword2
keyword3
keyword4
keyword5

this needs to move to the spreadsheet to look like this

campaign1 adgroup1 keyword1
campaign1 adgroup1 keyword2
campaign1 adgroup1 keyword3 etc

This is the macro I'm using. Any help really appreciated!

Sub Keyword_Transform()
Dim lngInputColumnID As Long, lngOutputRow As Long
Dim wksSource As Worksheet, wksdest As Worksheet
Dim lngColNo As Long
Dim strTemp As String

Set wksSource = Worksheets("Campaign Structure")
Set wksdest = Worksheets("DS_Kwds")

Application.ScreenUpdating = False
' Delete existing content
wksdest.Range("A7:Z7", wksdest.Range("A7:Z7").End(xlDown)).ClearContents

lngOutputRow = 7

For lngInputColumnID = 2 To 256

Application.StatusBar = "Processing column " & lngInputColumnID - 1 & " of 255"

'see if there's an Ad group title & at least 1 keyword
If wksSource.Cells(10, lngInputColumnID).Value <> "" Then

If wksSource.Cells(71, lngInputColumnID).Value <> "" Then

'wksSource.Cells(10, lngInputColumnID).Copy
wksdest.Cells(lngOutputRow, 17).Value = wksSource.Cells(10, lngInputColumnID).Value 'Campaign
wksdest.Cells(lngOutputRow, 16).Value = wksSource.Cells(70, lngInputColumnID).Value 'Ad group

strTemp = wksSource.Cells(68, lngInputColumnID).Value 'cost per click
strTemp = Application.WorksheetFunction.Substitute(strTemp, "$", "") 'remove dollar signs ($)
wksdest.Cells(lngOutputRow, 9).Value = strTemp 'PasteSpecial xlPasteValues
wksdest.Cells(lngOutputRow, 10).Value = strTemp 'PasteSpecial xlPasteValues

wksdest.Cells(lngOutputRow, 5).Value = wksSource.Cells(65, lngInputColumnID).Value 'Link URL

'Keywords
If wksSource.Cells(71, lngInputColumnID).Offset(1, 0) = "" Then
wksSource.Cells(71, lngInputColumnID).Copy
Else
wksSource.Range(wksSource.Cells(71, lngInputColumnID), wksSource.Cells(71, lngInputColumnID).End(xlDown)).Copy
End If
wksdest.Cells(lngOutputRow, 2).PasteSpecial xlPasteValues

'Engine-specific data
Select Case wksSource.Range("Engine").Value
Case "Yahoo"
wksdest.Cells(lngOutputRow, 3).Value = "Advanced" 'Match type
wksdest.Cells(lngOutputRow, 8).Value = 0.1 'Min bid
Case "Google"
wksdest.Cells(lngOutputRow, 3).Value = "Broad" 'Match type
wksdest.Cells(lngOutputRow, 8).Value = 0.01 'Min bid
End Select

If wksdest.Cells(lngOutputRow, 2).Offset(1, 0).Value = "" Then
lngOutputRow = lngOutputRow + 1

Else
With wksdest.Cells(lngOutputRow, 2).End(xlDown).Offset(0, 1)
wksdest.Range(.Cells, .End(xlUp)).FillDown
'lngOutputRow = .Offset(1).Row
End With
With wksdest.Cells(lngOutputRow, 2).End(xlDown).Offset(0, 3).Range("A1:m1")
wksdest.Range(.Cells, .End(xlUp)).FillDown
lngOutputRow = .Offset(1).Row
End With

End If
End If
End If

Next lngInputColumnID

wksdest.Select
wksdest.Range("A6").Select
' wksSource.Select
Application.ScreenUpdating = True
Application.StatusBar = False
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
This is the initial data

Excel Workbook
BCDEF
10Campaign 2Campaign 3Campaign 4Campaign 5Campaign 6
12adgroup1adgroup2adgroup3adgroup4adgroup5
14Ad title
15Description 1
16Description 2
17Display URL
20
21Click-thru URLClick-thru URLClick-thru URLClick-thru URLClick-thru URL
22
23
24Cost Per ClickCost Per ClickCost Per ClickCost Per ClickCost Per Click
25
26
27adgroup1adgroup2adgroup3adgroup4adgroup5
28Keyword1Keyword1Keyword1Keyword1Keyword1
29Keyword2Keyword2Keyword2Keyword2Keyword2
30Keyword3Keyword3Keyword3Keyword3Keyword3
31Keyword4Keyword4Keyword4Keyword4Keyword4
32Keyword5Keyword5Keyword5Keyword5Keyword5
33Keyword6Keyword6Keyword6Keyword6Keyword6
34Keyword7Keyword7Keyword7Keyword7Keyword7
35Keyword8Keyword8Keyword8Keyword8Keyword8
36Keyword9Keyword9Keyword9Keyword9Keyword9
37Keyword10Keyword10Keyword10Keyword10Keyword10
38Keyword11Keyword11Keyword11Keyword11Keyword11
39Keyword12Keyword12Keyword12Keyword12Keyword12
40Keyword13Keyword13Keyword13Keyword13Keyword13
41Keyword14Keyword14Keyword14Keyword14Keyword14
42Keyword15Keyword15Keyword15Keyword15Keyword15
43Keyword16Keyword16Keyword16Keyword16Keyword16
44Keyword17Keyword17Keyword17Keyword17
45Keyword18Keyword18Keyword18Keyword18
46Keyword19Keyword19
47Keyword20Keyword20
48Keyword21Keyword21
49Keyword22Keyword22
50Keyword23
51Keyword24
Campaign Structure
 
Upvote 0
This is how I want it to end up, the highlighted yellow columns are the ones I'm worried about, not too concerned about the rest.

Excel Workbook
BCDEFGHIJKLMNOPQ
6KeywordMatch TypeKeyword Alt. TextLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryAd GroupCampaign
7Keyword1BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
8Keyword2BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
9Keyword3BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
10Keyword4BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
11Keyword5BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
12Keyword6BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
13Keyword7BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
14Keyword8BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
15Keyword9BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
16Keyword10BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
17Keyword11BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
18Keyword12BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
19Keyword13BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
20Keyword14BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
21Keyword15BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
22Keyword16BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
23Keyword17BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
24Keyword18BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
25Keyword19BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
26Keyword20BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
27Keyword21BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
28Keyword22BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
29Keyword23BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
30Keyword24BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup1Campaign 2
31Keyword1BroadLink URLURL ParametersExchange ParametersMin. BidSearch Engine BidMax. BidBid StrategyTarget Pos.Display URLKeyword TypeCategoryadgroup2Campaign 3
DS_Kwds
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,842
Members
449,193
Latest member
MikeVol

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