VBA: Put header text before cell data

azov5

New Member
Joined
Dec 27, 2018
Messages
40
VBA: I have multi column data with header.
I want to add the header text before text in every cell data in respective column.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Something like this. This code assumes your data begins in cell A1. You may need to adjust how the range is set depending on what your data looks like.

Code:
Sub mk()
Dim r As Range: Set r = Range("A1").CurrentRegion
Dim ar() As Variant: ar = Application.Transpose(r.Value)


For co = 1 To UBound(ar)
    For ro = 2 To UBound(ar, 2)
        ar(co, ro) = ar(co, 1) & ar(co, ro)
    Next ro
Next co


r.Value = Application.Transpose(ar)


End Sub
 
Upvote 0
Similar, but without the Transpose
Rich (BB code):
Sub azov5()
    Dim Ary As Variant
    Dim r As Long, c As Long

    Ary = Range("A1").CurrentRegion.Value2
    For r = 2 To UBound(Ary)
        For c = 1 To UBound(Ary, 2)
            Ary(r, c) = Ary(1, c) & " " & Ary(r, c)
        Next c
    Next r
    Range("A1").CurrentRegion.Value2 = Ary
End Sub
This will also add a space between the data & the header
 
Upvote 0
Fluff

Run time error 1004
Application-defined or object defined error


"Range("A1").CurrentRegion.Value2 = Ary"

Its not working on all rows
 
Last edited:
Upvote 0
Are your headers in row 1 starting in col A?
Also do you have any blank rows in you data?
 
Upvote 0
Are your headers in row 1 starting in col A?
Yes !
Also do you have any blank rows in you data?
Yes !
 
Upvote 0
What range is your data in Column A through Column ...?
 
Upvote 0
See if this works.

Code:
Sub mk()
Dim r As Range: Set r = Range("A1:BW" & Range("A" & Rows.Count).End(xlUp).Row)
Dim ar() As Variant: ar = Application.Transpose(r.Value)


For co = 1 To UBound(ar)
    For ro = 2 To UBound(ar, 2)
        ar(co, ro) = ar(co, 1) & ar(co, ro)
    Next ro
Next co


r.Value = Application.Transpose(ar)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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