find the last used column and paste the rows values alongside the last column.

nihal90

New Member
Joined
Mar 19, 2014
Messages
1
I am trying to format my worksheet. I am trying write a macro that finds the last used column (in this case it is Column V) and select the rows range for each company(Column E to Column J) and paste to the Column W( next to the last used column). And finally delete the empty rows. What I need is a row for each company. It would be nice if someone could help me on this.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
nihal
the following macro will copy the headers in E1:J1 and transpose them to the column after the last.

I don't know what you mean with 'delete the empty rows'


<font face=Courier New>Option Explicit<br><br><SPAN style="color:#00007F">Sub</SPAN> TransposeCompanies()<br><SPAN style="color:#007F00">' macro to transpose the headers in range E1:J1 to after _<br>  the last column in use.</SPAN><br>  <br>    <SPAN style="color:#00007F">Dim</SPAN> rTo <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> vCompHdr<br>    <SPAN style="color:#00007F">Const</SPAN> sHeaders = "E1:J1" <SPAN style="color:#007F00">' easy if you want to change this in future</SPAN><br>    <br>    <SPAN style="color:#007F00">' find last column in use</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rTo = Cells.Find("*", after:=Cells(1, Columns.Count), SearchOrder:=xlByColumns, searchdirection:=xlPrevious)<br>    <SPAN style="color:#007F00">' error check in case used on wrong sheet and sheet is empty</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> rTo <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox "Sheet is empty", vbCritical<br>        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#007F00">' now transpose the headers of rangre E1:J1 to the column after the last in use</SPAN><br>    <SPAN style="color:#007F00">' load the headers in a variant array</SPAN><br>    vCompHdr = Range(sHeaders).Value<br>    <SPAN style="color:#007F00">' and put this transposed in next column</SPAN><br>    <SPAN style="color:#007F00">' ensure sufficient cells are allocated, in case your input range changes size in the future</SPAN><br>    Cells(1, rTo.Column + 1).Resize(UBound(vCompHdr, 2), 1).Value = Application.WorksheetFunction.Transpose(vCompHdr)<br>    <br>    <SPAN style="color:#007F00">'clean up</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rTo = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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