VBA Add Text & Merge Sheets to 1 Sheet

cjvenables

Board Regular
Joined
Aug 2, 2011
Messages
65
Hello,

I am looking for a Macro that will take the name of the worksheet (off the tab), add it after the last column of data, autofill to the last row, and then merge all sheets to a new sheet in the workbook (the exisiting workbook).

I have a bunch of different contracts on different sheets for 1 customer. For example, if the last column of data is in column J in the sheet, I want to add the name of the worksheet to column K, and then autofill all the way down to the last row of data. Some sheets may have more columns than others, as well as more rows than others, so I need the ranges to be flexible.

Since the customers obviously have different names, as well as the contracts, everything has to be generic.

Hope this is enough information. If not, let me know and I will happily clarify where needed.

Thanks guys!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Maqybe (untested)

Code:
Sub Difficult()
Dim ws As Worksheet, ns As Worksheet
Dim LR As Long, LC As Long
Dim i As Long
For Each ws In ActiveWorkbook.Worksheets
    LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    LC = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
    .Range(, Cells(1, LC + 1), Cells(LR, LC + 1)).Value = .Name
Next ws
Set ns = Worksheets.Add(xlWBATWorksheet, after:=Sheets(Sheets.Count))
For i = 1 To Worksheets.Count - 1
    Worksheets(i).UsedRange.Copy Destination:=ns.Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1)
Next i
End Sub
 
Upvote 0
Try

Code:
Sub Difficult()
Dim ws As Worksheet, ns As Worksheet
Dim LR As Long, LC As Long
Dim i As Long
For Each ws In ActiveWorkbook.Worksheets
    With ws
        LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
        LC = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
        .Range(, Cells(1, LC + 1), Cells(LR, LC + 1)).Value = .Name
    End With
Next ws
Set ns = Worksheets.Add(after:=Sheets(Sheets.Count))
For i = 1 To Worksheets.Count - 1
    Worksheets(i).UsedRange.Copy Destination:=ns.Range("A" & Rows.Count).End(xlUp).Offset(1)
Next i
End Sub
 
Upvote 0
@VoG There is a new error:

Compile Error: Argument not Optional. The range row is now highlighted.

Thanks for your help on this!
 
Upvote 0
Sorry, try

Code:
Sub Difficult()
Dim ws As Worksheet, ns As Worksheet
Dim LR As Long, LC As Long
Dim i As Long
For Each ws In ActiveWorkbook.Worksheets
    With ws
        LR = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
        LC = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
        .Range(.Cells(1, LC + 1), .Cells(LR, LC + 1)).Value = .Name
    End With
Next ws
Set ns = Worksheets.Add(after:=Sheets(Sheets.Count))
For i = 1 To Worksheets.Count - 1
    Worksheets(i).UsedRange.Copy Destination:=ns.Range("A" & Rows.Count).End(xlUp).Offset(1)
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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