Young Grasshopper
Board Regular
- Joined
- Dec 9, 2022
- Messages
- 58
- Office Version
- 365
- 2016
- Platform
- Windows
Hi,
I have this worksheet where which is devided into sections (TestCampaign1 etc). In the example underneath there are just two sections, but in the finished worksheet there will be 100 sections. Everytime the worksheet is used, there will be diffrent informasjon in each section and numbers of rows will also vary. The numbers of sections used will vary from time to time.
There will also be common that only one column in a section is being used.
I need a code that do the following in a efficiant and smart way:
(let's use the example in the picture as a example)
1. Copy filled cells from C4 and down (.End(xlDown)) and paste it to say M1 f.ex.
2. Then copy filled cells from D4 and downwards and paste it to M1.End(xlDown).Offset(1) with "" on each side of the value, so like "KeywordExample"
3. Same with e4. Paste to with [ and ] on each side of the value, so like ([KeywordExample])
4. Then the section name and group name needs to be added to the adjecent cells for each entry under m1. So TestCampaign1 in the K column, and TestGroup1 in the L column.
5. Then move on to the next "section", G4 and repete the prosess, pasting to ("M1").End(xlDown).Offset(1), same with H (with "value") and I (with [value]), and TestCampaign2 and TestGroup2 added to the adjecent cells to these.
6. then repeting the prosess until the loop hits a section where none of the columns are being used.
So the result will be one long column in m, with every entry stacked ontop of eachother, with the correct section name and group name in the adjecent cells. There could potentially be 150 000 entries, so the faster the code the better
I tried a code that went something like this, but this was just messy and didn't really work..
I'm really stuck here, so would appreciate any help:D
I have this worksheet where which is devided into sections (TestCampaign1 etc). In the example underneath there are just two sections, but in the finished worksheet there will be 100 sections. Everytime the worksheet is used, there will be diffrent informasjon in each section and numbers of rows will also vary. The numbers of sections used will vary from time to time.
There will also be common that only one column in a section is being used.
I need a code that do the following in a efficiant and smart way:
(let's use the example in the picture as a example)
1. Copy filled cells from C4 and down (.End(xlDown)) and paste it to say M1 f.ex.
2. Then copy filled cells from D4 and downwards and paste it to M1.End(xlDown).Offset(1) with "" on each side of the value, so like "KeywordExample"
3. Same with e4. Paste to with [ and ] on each side of the value, so like ([KeywordExample])
4. Then the section name and group name needs to be added to the adjecent cells for each entry under m1. So TestCampaign1 in the K column, and TestGroup1 in the L column.
5. Then move on to the next "section", G4 and repete the prosess, pasting to ("M1").End(xlDown).Offset(1), same with H (with "value") and I (with [value]), and TestCampaign2 and TestGroup2 added to the adjecent cells to these.
6. then repeting the prosess until the loop hits a section where none of the columns are being used.
So the result will be one long column in m, with every entry stacked ontop of eachother, with the correct section name and group name in the adjecent cells. There could potentially be 150 000 entries, so the faster the code the better
I tried a code that went something like this, but this was just messy and didn't really work..
VBA Code:
Private Sub Test()
Dim MyRange As Range
Dim MyRange2 As Range
Dim MyRange3 As Range
Dim Cell As Range
If ThisWorkbook.Worksheets("KeywordsTest").Range("D5").value <> "" Then
Dim rSource As Range, rDest As Range, rDest2 As Range, rDest3 As Range, r As Range
Dim tbl As Range, rowNum As Integer
Const colNum = 3
Dim Campaign As String
Dim Adset As String
Set rDest = ThisWorkbook.Worksheets("KeywordsTest").Range("M1")
Set rDest2 = ThisWorkbook.Worksheets("KeywordsTest").Range("P1")
Set rDest3 = ThisWorkbook.Worksheets("KeywordsTest").Range("S1")
Set rSource = ThisWorkbook.Worksheets("KeywordsTest").Range("C4")
Set r = rSource
While r <> ""
'Bred
Set tbl = Range(r, r.End(xlDown))
rDest.value = tbl.value
Set MyRange = Range(rDest, rDest.End(xlDown))
For Each Cell In MyRange
If Cell.value <> "" And Cell.Offset(, -2).value = "" Then
Cell.Offset(, -2).value = r.Offset(-3, 1).value
Cell.Offset(, -1).value = r.Offset(-2, 1).value
End If
Next Cell
Set rDest = rDest.End(xlDown).Offset(1, 0)
'Setning
Set r = r.Offset(, 1)
Set tbl = Range(r, r.End(xlDown))
tbl.Copy
rDest2.PasteSpecial (xlPasteValues)
Set MyRange2 = Range(rDest2, rDest2.End(xlDown))
For Each Cell In MyRange2
If Cell.value <> "" And Cell.Offset(, -2).value = "" Then
Cell.Offset(, -2).value = r.Offset(-3, 1).value
Cell.Offset(, -1).value = r.Offset(-2, 1).value
End If
Next Cell
Set rDest2 = rDest2.End(xlDown).Offset(1, 0)
'Eksakt
Set r = r.Offset(, 1)
Set tbl = Range(r, r.End(xlDown))
tbl.Copy
rDest3.PasteSpecial (xlPasteValues)
Set MyRange3 = Range(rDest3, rDest3.End(xlDown))
For Each Cell In MyRange3
If Cell.value <> "" And Cell.Offset(, -2).value = "" Then
Cell.Offset(, -2).value = r.Offset(-3, 1).value
Cell.Offset(, -1).value = r.Offset(-2, 1).value
End If
Next Cell
rDest3 = rDest3.End(xlDown).Offset(1, 0)
r = r(0, 2)
For Each Cell In MyRange2
If Cell.value <> "" Then
Cell.value = "[" & Cell.value & "]"
End If
Next Cell
Wend
End If
End If
Next Cell
End Sub
I'm really stuck here, so would appreciate any help:D