Macro to cut header and paste on row

sncb

Board Regular
Joined
Mar 17, 2011
Messages
168
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi All,

I currently I have data pasted in my excel sheet as below in a tab called 'codes' something like this:

Excel Workbook
ABCD
1DEUTSCHLAND***
2WarehouseCodeDateItem ID
3WH-5142ABB14/3/11M4600
4WH-5142ABB14/3/11M4600
5WH-5142MNC14/3/11G6500
6WH-5142MNC14/3/11G6500
7WH-5142MNC14/3/11G6500
8WH-5142RGV14/3/11R5566
9WH-5142RGV14/3/11R5566
10FRANCE***
11WarehouseCodeDateItem ID
12WH-9197ABB14/3/11M4600
13WH-9197ABB14/3/11M4600
14WH-9197MNC14/3/11G6500
15WH-9197MNC14/3/11G6500
16WH-9197MNC14/3/11G6500
17WH-9197RGV14/3/11R5566
18WH-9197RGV14/3/11R5566
codes




and many more countries under DE and FR and would like to change it to look like this below in tab called 'changed':


Excel Workbook
ABCDE
1*WarehouseCodeDateItem ID
2DEUTSCHLANDWH-5142ABB14/3/11M4600
3DEUTSCHLANDWH-5142ABB14/3/11M4600
4DEUTSCHLANDWH-5142MNC14/3/11G6500
5DEUTSCHLANDWH-5142MNC14/3/11G6500
6DEUTSCHLANDWH-5142MNC14/3/11G6500
7DEUTSCHLANDWH-5142RGV14/3/11R5566
8DEUTSCHLANDWH-5142RGV14/3/11R5566
9*****
10*WarehouseCodeDateItem ID
11FRANCEWH-9197ABB14/3/11M4600
12FRANCEWH-9197ABB14/3/11M4600
13FRANCEWH-9197MNC14/3/11G6500
14FRANCEWH-9197MNC14/3/11G6500
15FRANCEWH-9197MNC14/3/11G6500
16FRANCEWH-9197RGV14/3/11R5566
17FRANCEWH-9197RGV14/3/11R5566
changed


Could anyone recommend the macro?

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi,

Thanks for replying. No they are all different countries.

B regards
 
Upvote 0
No..There are no stars...assume them absent..

Thanks for replying

BRegards
 
Upvote 0
Hi ArticW and Weaver,

Please ignore the above request sicne Ive found a way to solve the issue at the source itself. Im getting the data from a report and have decided to change the report so that the data can be received just the way I want.

So thanks for looking into it and didn't want to waste your time in case u'all are still looking.

Have a nice day.

B Regards
 
Upvote 0
LOL i've just done it too:

Code:
Sub change_code()
    Dim ws1 As Worksheet, ws2 As Worksheet, NextRow As Long, NextRow2 As Long, x As Integer
    Dim code1 As String, codeA As String, codeB As String, codeC As String, codeD As String
    Set ws1 = Sheets("codes")
    Set ws2 = Sheets("changed")
    With ws2.Range("A1:E1")
            .ColumnWidth = Array(20, 20, 8, 12, 10)
    End With
    With ws2.Range("C1:E1")
            .EntireColumn.HorizontalAlignment = xlCenter
    End With
    ws1.Select
    ws1.Range("A1").Select
    code1 = Selection.Value

    NextRow = ws2.Range("A65536").End(xlUp).Row
    NextRow1 = ws1.Range("A65536").End(xlUp).Row + 1

    With ws1.Range("A:A")
        For x = 2 To NextRow1 Step 1
            With .Cells(x, 1)
            If .Value = "" Then GoTo ENDME
            ws2.Range("A" & NextRow).Value = code1
            ws2.Range("B" & NextRow).Value = ws1.Range("A" & x).Value
            ws2.Range("C" & NextRow).Value = ws1.Range("B" & x).Value
            ws2.Range("D" & NextRow).Value = ws1.Range("C" & x).Value
            ws2.Range("E" & NextRow).Value = ws1.Range("D" & x).Value
            NextRow = ws2.Range("A65536").End(xlUp).Row + 1
            If ws1.Range("A" & x).Offset(2, 0).Value = "Warehouse" Then
            code1 = ws1.Range("A" & x).Offset(1, 0).Value
            x = x + 1
             GoTo SNEXT
            End If
            End With
SNEXT:
        Next x
    End With
ENDME:
End Sub

arctic
 
Upvote 0
Hi ArticW and Weaver,

Please ignore the above request sicne Ive found a way to solve the issue at the source itself. Im getting the data from a report and have decided to change the report so that the data can be received just the way I want.

So thanks for looking into it and didn't want to waste your time in case u'all are still looking.

Have a nice day.

B Regards

Lol I was going to have a crack at this during my lunch break!

Glad you got it sorted - it's usually best if you can fix the issue at source in any case.

W
 
Upvote 0
I did it anyway, cos I was bored.

Code:
Sub sncb()
    Dim ttl, lr, f
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Columns(1).Insert shift:=xlToRight
    For i = 1 To lr
    If WorksheetFunction.CountA(Cells(i, 2).Resize(, 4)) = 1 Then
        ttl = Cells(i, 2)
        f = False
    Else
        If f Then
            Cells(i, 1) = ttl
        Else
            f = True
        End If
    End If
    Next i
    On Error Resume Next
    Columns(3).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Columns(1).AutoFit
End Sub
 
Upvote 0
Thanks both again for your help. The macro worked and Im sure I can use it somewhere else where I cannot change the design of the report.

Once again, Thanks for your time and Cheers!!
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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