Option Explicit
Sub MergeAll()
' constants
Const ksSummaryWS = "Hoja0"
Const ksSummaryRange = "Table0"
' declarations
Dim rngS As Range, rngW As Range
Dim I As Long, J As Integer
' start
Set rngS = Worksheets(ksSummaryWS).Range(ksSummaryRange)
With rngS
If .Rows.Count > 1 Then .Range(.Rows(2), .Rows(.Rows.Count)).ClearContents
End With
I = 1
' process
For J = 1 To Worksheets.Count
If Worksheets(J).Name <> ksSummaryWS Then
Set rngW = Worksheets(J).Range(rngS.Address).EntireColumn.CurrentRegion
With rngW
If .Rows.Count > 1 Then
.Range(.Rows(2), .Rows(.Rows.Count)).Copy rngS.Cells(I + 1, 1)
I = I + .Rows.Count - 1
End If
End With
End If
Next J
' end
Set rngW = Nothing
Set rngS = Nothing
Beep
End Sub
Juzzvinay - I recently found this thread. I am sure you have figured this out by now, but I did not see a reply here. I can help with this question if you still need it.
"I also have a cell (formatted as Number - cannot be non Number format) with 2 leading zeros, but when I refer to the cell using "&" and a fixed word - it does not copy using the 2 leading zeros."
Hi, Cruiser!
Thought I forgot about this? Ok, you guessed. Give a look at this link:
https://dl.dropboxusercontent.com/u/60558749/Merge_Stack Multiple Named Ranges (Across Multiple Worksheets) in a Master Sheet (for juzzvinay at chandoo.org).xlsm
This is the code behind the cyan button, clear, neat, simple and fast (so says the author! ):
Code:Option Explicit Sub MergeAll() ' constants Const ksSummaryWS = "Hoja0" Const ksSummaryRange = "Table0" ' declarations Dim rngS As Range, rngW As Range Dim I As Long, J As Integer ' start Set rngS = Worksheets(ksSummaryWS).Range(ksSummaryRange) With rngS If .Rows.Count > 1 Then .Range(.Rows(2), .Rows(.Rows.Count)).ClearContents End With I = 1 ' process For J = 1 To Worksheets.Count If Worksheets(J).Name <> ksSummaryWS Then Set rngW = Worksheets(J).Range(rngS.Address).EntireColumn.CurrentRegion With rngW If .Rows.Count > 1 Then .Range(.Rows(2), .Rows(.Rows.Count)).Copy rngS.Cells(I + 1, 1) I = I + .Rows.Count - 1 End If End With End If Next J ' end Set rngW = Nothing Set rngS = Nothing Beep End Sub
Just advise if any issue.
Regarsd!
Hi, Cruiser!
Thought I forgot about this? Ok, you guessed. Give a look at this link:
https://dl.dropboxusercontent.com/u/60558749/Merge_Stack Multiple Named Ranges (Across Multiple Worksheets) in a Master Sheet (for juzzvinay at chandoo.org).xlsm
Regarsd!
@Cruiser
Thanks - would appreciate the help on the question. I solved it somehow, but I don't think I will be able to do it again. So it would be good to know an alternative.
Private Sub Worksheet_Activate()
Dim lo As ListObject
Dim lr As ListRow
Dim rngSource As Range
Dim rngDest As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set lo = [Summary].ListObject
With lo
On Error Resume Next
.DataBodyRange.Rows.Delete
On Error GoTo 0
Set rngSource = [Mumbai].ListObject.DataBodyRange
Set rngDest = .HeaderRowRange.Offset(1)
rngDest.Resize(rngSource.Rows.Count).Value = rngSource.Value
Set rngSource = [Bangalore].ListObject.DataBodyRange
Set rngDest = .ListRows.Add.Range
rngDest.Resize(rngSource.Rows.Count).Value = rngSource.Value
Set rngSource = [Chennai].ListObject.DataBodyRange
Set rngDest = .ListRows.Add.Range
rngDest.Resize(rngSource.Rows.Count).Value = rngSource.Value
With .Sort
.SortFields.Clear
.SortFields. _
Add Key:=Range("Summary[[#All],[Value Date]]"), _
SortOn:=xlSortOnValues, _
Order:=xlDescending, _
DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
I tried to mean: chose if going with jeffreyweir's solution or with mine, hence ask the proper author. So you're cheating.Just please tell me which path would you be following so as I know if I have to work a little more or I can go for a couple of Carlsberg.
Set rngSource = [Mumbai].ListObject.DataBodyRang
Set wb = Workbooks.Open("<full path>\Named Ranges - Sample (Extract).xlsm")
Set rngSource = wb.Worksheets("M").[Mumbai].ListObject.DataBodyRange
Dim wb as workbook
wb.close False
Set wb = Nothing
Wow, I can't believe this is ten weeks old! I am still in the 2003 version of my project... Still haven't gotten to the point of using the VBA code you so graciously provided. I've disconnected my PC from the internet since the end of support for XP and office 2003, so I haven't been back here for a while. Posting this on my wife's mac. Trying to get to a certain point before I upgrade my PC and start working with Office 360... At any rate, I am sorry that you did all that work and then I dropped off the face of the earth. I assure you I will get to the point of implementing your code eventually. It's just that every time I think I am "close" I find more ways to improve my project that just MUST be implemented. Nothing is ever "good enough"! Cheers
I am finally at the point of trying this out on my project. Unfortunately, your "Hoja" example is not working in excel 2003. When I try to open the .xlsm file, it says it canot be found with XLM converter. When I save it as .xls, it says "Unitialized Active X controls will not be opened in this version of excel". I continued, enabled macros, and opened the file. The Merge All button didn't work, so I opened the macro and ran it. I got a Runtime Error '57121': Application-defined or object-defined error. I hit debug and it highlighted the row of code: Set rngS=Worksheets(KsSummaryWS.Range(KsSummaryRange). Any thoughts?Hi, Cruiser!
Thought I forgot about this? Ok, you guessed. Give a look at this link:
https://dl.dropboxusercontent.com/u/60558749/Merge_Stack Multiple Named Ranges (Across Multiple Worksheets) in a Master Sheet (for juzzvinay at chandoo.org).xlsm
This is the code behind the cyan button, clear, neat, simple and fast (so says the author! ):
Code:Option Explicit Sub MergeAll() ' constants Const ksSummaryWS = "Hoja0" Const ksSummaryRange = "Table0" ' declarations Dim rngS As Range, rngW As Range Dim I As Long, J As Integer ' start Set rngS = Worksheets(ksSummaryWS).Range(ksSummaryRange) With rngS If .Rows.Count > 1 Then .Range(.Rows(2), .Rows(.Rows.Count)).ClearContents End With I = 1 ' process For J = 1 To Worksheets.Count If Worksheets(J).Name <> ksSummaryWS Then Set rngW = Worksheets(J).Range(rngS.Address).EntireColumn.CurrentRegion With rngW If .Rows.Count > 1 Then .Range(.Rows(2), .Rows(.Rows.Count)).Copy rngS.Cells(I + 1, 1) I = I + .Rows.Count - 1 End If End With End If Next J ' end Set rngW = Nothing Set rngS = Nothing Beep End Sub
Just advise if any issue.
Regarsd!
Just ran across this again. CORRECTION: Using the TEXT function wil cause a MINIMUM number of characters in your return. A higher number will NOT be truncated.Sometimes the "General" format retains leading zeros and sometimes it doesn't... on the same sheet! Frustrating. I have dealt with it two ways. 1) If you are simply entering the data, you can select "Custom" format and choose a format or create one. 2) If you are referencing another cell using the "General" format, use the TEXT function. Say you have the number 009 in cell A1. The formula:
=TEXT(A1,"000") forces a 3 digit result which includes the leading zeros. I use this with a +1 to generate a list of sequential numbers starting at 001. Of course, this requires a uniformed length for all of your strings of numbers. If you had 4 characters in A1, this would return only the first 3. On the other hand, if your number strings have varying lengths, but the number of leading zeros is consistant, say two, you could cut out the original zeros and add them back with:
TEXT("00","00")&MID(A1,3,255)
The format of the TEXT function using numbers is (Source or "Number","text_format").
If the text strings are of varying length AND the number of leading zeros are of varying length, well, that's a different story. I guess your formula would have to search for the first non zero in the string and figure the LEN from there subtracted from the original LEN to determine the number of zeros to add back in... I've never tried it.
Hope something here helps. The Ninjas can probably offer some even better ways to accomplish your goal.
Somehow I missed this post, so a very belated thanks to you Sir! Just wanted to let you know that I opened the Hoja worksheet on another computer in excel 2010 and it works like a charm. Pretty darned elegant piece of work. Thanks again.Hi, Cruiser!
My .xlsm file version is intended to be used with the new Excel workbook format that yet goes by its 3rd edition (2007, 2010, 2013), if we skip Mac version 2011. I haven't tested it under the older .xls 2003 version, but maybe you have a workaround.
There's a patch that MS released years ago for handling new file versions of all Office products (.xlsx, .xlsm, .docx, ...) with prior versions. Here's the link:
http://www.microsoft.com/en-us/download/details.aspx?id=3
Regards!
Hey SirJB7,Hi, Cruiser!
Thought I forgot about this? Ok, you guessed. Give a look at this link:
https://dl.dropboxusercontent.com/u/60558749/Merge_Stack Multiple Named Ranges (Across Multiple Worksheets) in a Master Sheet (for juzzvinay at chandoo.org).xlsm
This is the code behind the cyan button, clear, neat, simple and fast (so says the author! ):
Code:Option Explicit Sub MergeAll() ' constants Const ksSummaryWS = "Hoja0" Const ksSummaryRange = "Table0" ' declarations Dim rngS As Range, rngW As Range Dim I As Long, J As Integer ' start Set rngS = Worksheets(ksSummaryWS).Range(ksSummaryRange) With rngS If .Rows.Count > 1 Then .Range(.Rows(2), .Rows(.Rows.Count)).ClearContents End With I = 1 ' process For J = 1 To Worksheets.Count If Worksheets(J).Name <> ksSummaryWS Then Set rngW = Worksheets(J).Range(rngS.Address).EntireColumn.CurrentRegion With rngW If .Rows.Count > 1 Then .Range(.Rows(2), .Rows(.Rows.Count)).Copy rngS.Cells(I + 1, 1) I = I + .Rows.Count - 1 End If End With End If Next J ' end Set rngW = Nothing Set rngS = Nothing Beep End Sub
Just advise if any issue.
Regarsd!
I got it ! Nevermind.With the VBA script provided is there a way to have a specific worksheet ignored and still copy the rest of the remaining worksheets?