VBA to copy strings according to predefined markers (numbers)

smide

Board Regular
Joined
Dec 20, 2015
Messages
162
Office Version
  1. 2016
Platform
  1. Windows
Hello.

In Sheet2, Column B (B3:B50) I have a strings which I need to copy in Sheet1 , Row 3 in cells B3,D3,F3, ... V3 and X3 (12 in total).

In Sheet2, Column G is so called "markers" Column because it contains markers/numbers for each product and those markers define copy position for each cell (copy position in Sheet1, row3).

So, if let say (in Sheet2) cell G3=1 than string from cell B3 (Sheet2) should be copied in B3 (Sheet1) and so on...

- IF some cell(s) in G column (G3:G50) is empty (without marker) than delete appropriate string in column B.
- IF Column G is completely empty than JUST copy first 12 strings from Sheet2 (B3:B14) into Sheet1 (B3,D3,F3,...X3).
(B3 to B3, B4 to D3 ...)

Example.

Sheet2


BG
1
2
3home3
4shop
5mrexcel
1
6pattern
6
7question
2
8product
9best
5
10forward
4
11.........

<tbody>
</tbody>

Sheet1 (after macro run)

ABCDEFGHIJKLMN
1
2
3mrexcel
question
home
forward
best
pattern
...

<tbody>
</tbody>

Sheet2 (after macro run)

BG
1
2
3home3
4
5mrexcel1
6pattern6
7question2
8
9best5
10forward4
11....

<tbody>
</tbody>
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
In Sheet2, Column B (B3:B50) I have a strings which I need to copy in Sheet1 , Row 3 in cells B3,D3,F3, ... V3 and X3 (12 in total).
3home3In Sheet2, Column G is so called "markers" Column because it contains markers/numbers for each product and those markers define copy position for each cell (copy position in Sheet1, row3).
4
5mrexcel1So, if let say (in Sheet2) cell G3=1 than string from cell B3 (Sheet2) should be copied in B3 (Sheet1) and so on...
6pattern6
7question2- IF some cell(s) in G column (G3:G50) is empty (without marker) than delete appropriate string in column B.
8 - IF Column G is completely empty than JUST copy first 12 strings from Sheet2 (B3:B14) into Sheet1 (B3,D3,F3,...X3).
9best5(B3 to B3, B4 to D3 ...)
10forward4
this macro does it - I think…
For j = 3 To 10
If Cells(j, 3) = "" Then Cells(j, 2) = Cells(1, 1)
Next j
.... ccol = 2
num = 1
20 For k = 3 To 10
If Cells(k, 3) = num Then Cells(26, ccol) = Cells(k, 2): num = num + 1: ccol = ccol + 2: GoTo 20
Next k
End Sub
col BDFHJL
mrexcelquestionhomeforwardbestpattern

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
See if this code does what you want...
Code:
[table="******* 500"]
[tr]
	[td]Sub ProductMarkers()
  Dim UnusedColumn As Long, LastRow As Long, NewLastRow As Long
  UnusedColumn = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
  LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  Application.ScreenUpdating = False
  Cells(3, UnusedColumn).Resize(LastRow - 2) = Evaluate("ROW(3:" & LastRow & ")-2")
  Range("A3", Cells(LastRow, UnusedColumn)).Sort Range("G3"), xlAscending
  NewLastRow = Cells(Rows.Count, "G").End(xlUp).Row
  Sheets("Sheet1").Range("B3").Resize(, 2 * (NewLastRow - 2) - 1) = Split(Join(Application.Transpose(Range("B3").Resize(NewLastRow - 2)), Chr(1) & Chr(1)), Chr(1))
  Range("A3", Cells(LastRow, UnusedColumn)).Sort Cells(3, UnusedColumn), xlAscending
  Columns(UnusedColumn).Delete
  Application.ScreenUpdating = True
  Range("G3:G" & LastRow).SpecialCells(xlBlanks).EntireRow.Clear
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
See if this code does what you want...
Code:
[TABLE]
<tbody>[TR]
[TD]Sub ProductMarkers()
  Dim UnusedColumn As Long, LastRow As Long, NewLastRow As Long
  UnusedColumn = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
  LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  Application.ScreenUpdating = False
  Cells(3, UnusedColumn).Resize(LastRow - 2) = Evaluate("ROW(3:" & LastRow & ")-2")
  Range("A3", Cells(LastRow, UnusedColumn)).Sort Range("G3"), xlAscending
  NewLastRow = Cells(Rows.Count, "G").End(xlUp).Row
  Sheets("Sheet1").Range("B3").Resize(, 2 * (NewLastRow - 2) - 1) = Split(Join(Application.Transpose(Range("B3").Resize(NewLastRow - 2)), Chr(1) & Chr(1)), Chr(1))
  Range("A3", Cells(LastRow, UnusedColumn)).Sort Cells(3, UnusedColumn), xlAscending
  Columns(UnusedColumn).Delete
  Application.ScreenUpdating = True
  Range("G3:G" & LastRow).SpecialCells(xlBlanks).EntireRow.Clear
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

That's exactly what I wanted.
Thank you! ;)
 
Upvote 0

Forum statistics

Threads
1,215,965
Messages
6,127,969
Members
449,414
Latest member
sameri

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