PC Review


Reply
Thread Tools Rate Thread

consolidate Sheets

 
 
SangelNet
Guest
Posts: n/a
 
      23rd Oct 2007
Hi
Im using the code from the the following link:

http://www.rodenbruin.nl/copy2.htm

it goes like this
Sub merge()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

On Error Resume Next
If Len(ThisWorkbook.Worksheets.Item("merge").Name) = 0 Then
On Error GoTo 0
Application.ScreenUpdating = False
Set DestSh = ThisWorkbook.Worksheets.Add
DestSh.Name = "merge"
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
Last = LastRow(DestSh)
shLast = LastRow(sh)

sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
DestSh.Cells(Last + 1, "A")
'Instead of this line you can use the code below to
copy only the values
'or use the PasteSpecial option to paste the format
also.

'With sh.Range(sh.Rows(3), sh.Rows(shLast))
'DestSh.Cells(Last + 1, "A").Resize(.Rows.Count, _
'.Columns.Count).Value = .Value
'End With

'sh.Range(sh.Rows(3), sh.Rows(shLast)).Copy
'With DestSh.Cells(Last + 1, "A")
'.PasteSpecial xlPasteValues, , False, False
'.PasteSpecial xlPasteFormats, , False, False
'Application.CutCopyMode = False
'End With

End If
Next
DestSh.Cells(1).Select
Application.ScreenUpdating = True
Else
MsgBox "The sheet Merge already exist"
End If
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

What can i add to the code if i want it to run thru all the sheets
except one in specific, lets say its called "maindata".

thnx

 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      23rd Oct 2007
Hi SangelNet

There are a few examples on the site

But you can do this

If sh.Name <> DestSh.Name And sh.Name <> "maindata" Then


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"SangelNet" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Hi
> Im using the code from the the following link:
>
> http://www.rodenbruin.nl/copy2.htm
>
> it goes like this
> Sub merge()
> Dim sh As Worksheet
> Dim DestSh As Worksheet
> Dim shLast As Long
> Dim Last As Long
>
> On Error Resume Next
> If Len(ThisWorkbook.Worksheets.Item("merge").Name) = 0 Then
> On Error GoTo 0
> Application.ScreenUpdating = False
> Set DestSh = ThisWorkbook.Worksheets.Add
> DestSh.Name = "merge"
> For Each sh In ThisWorkbook.Worksheets
> If sh.Name <> DestSh.Name Then
> Last = LastRow(DestSh)
> shLast = LastRow(sh)
>
> sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
> DestSh.Cells(Last + 1, "A")
> 'Instead of this line you can use the code below to
> copy only the values
> 'or use the PasteSpecial option to paste the format
> also.
>
> 'With sh.Range(sh.Rows(3), sh.Rows(shLast))
> 'DestSh.Cells(Last + 1, "A").Resize(.Rows.Count, _
> '.Columns.Count).Value = .Value
> 'End With
>
> 'sh.Range(sh.Rows(3), sh.Rows(shLast)).Copy
> 'With DestSh.Cells(Last + 1, "A")
> '.PasteSpecial xlPasteValues, , False, False
> '.PasteSpecial xlPasteFormats, , False, False
> 'Application.CutCopyMode = False
> 'End With
>
> End If
> Next
> DestSh.Cells(1).Select
> Application.ScreenUpdating = True
> Else
> MsgBox "The sheet Merge already exist"
> End If
> End Sub
>
> Function LastRow(sh As Worksheet)
> On Error Resume Next
> LastRow = sh.Cells.Find(What:="*", _
> After:=sh.Range("A1"), _
> Lookat:=xlPart, _
> LookIn:=xlFormulas, _
> SearchOrder:=xlByRows, _
> SearchDirection:=xlPrevious, _
> MatchCase:=False).Row
> On Error GoTo 0
> End Function
>
> What can i add to the code if i want it to run thru all the sheets
> except one in specific, lets say its called "maindata".
>
> thnx
>

 
Reply With Quote
 
SangelNet
Guest
Posts: n/a
 
      24th Oct 2007
Hi Ron

Did the change.
It starts doing the merge, then im getting an error on this line

sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy DestSh.Cells(Last + 1, "A")

cant seem to pint out whats wrong!


 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      24th Oct 2007
Have you also copy the LastRow function in the module ?

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"SangelNet" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Hi Ron
>
> Did the change.
> It starts doing the merge, then im getting an error on this line
>
> sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy DestSh.Cells(Last + 1, "A")
>
> cant seem to pint out whats wrong!
>
>

 
Reply With Quote
 
SangelNet
Guest
Posts: n/a
 
      24th Oct 2007
Yes Sir, I added the lastrow function. The code im using at this point
and getting error is this:

Sub merge()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Delete the sheet "MergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Worksheets("MergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True

'Add a worksheet with the name "MergeSheet"
Set DestSh = ThisWorkbook.Worksheets.Add
DestSh.Name = "MergeSheet"

'loop through all worksheets and copy the data to the DestSh
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> DestSh.Name And sh.Name <> "maindata" Then
Last = LastRow(DestSh)
shLast = LastRow(sh)

'This example copies everything, if you only want to copy
'values/formats look at the example below the first
example
sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
DestSh.Cells(Last + 1, "A")

End If
Next

Application.Goto DestSh.Cells(1)

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
___________________________________________________________________________________________
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      24th Oct 2007
Then I think that there is a empty sheet in your workbook

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"SangelNet" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Yes Sir, I added the lastrow function. The code im using at this point
> and getting error is this:
>
> Sub merge()
> Dim sh As Worksheet
> Dim DestSh As Worksheet
> Dim Last As Long
> Dim shLast As Long
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> 'Delete the sheet "MergeSheet" if it exist
> Application.DisplayAlerts = False
> On Error Resume Next
> ThisWorkbook.Worksheets("MergeSheet").Delete
> On Error GoTo 0
> Application.DisplayAlerts = True
>
> 'Add a worksheet with the name "MergeSheet"
> Set DestSh = ThisWorkbook.Worksheets.Add
> DestSh.Name = "MergeSheet"
>
> 'loop through all worksheets and copy the data to the DestSh
> For Each sh In ThisWorkbook.Worksheets
> If sh.Name <> DestSh.Name And sh.Name <> "maindata" Then
> Last = LastRow(DestSh)
> shLast = LastRow(sh)
>
> 'This example copies everything, if you only want to copy
> 'values/formats look at the example below the first
> example
> sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
> DestSh.Cells(Last + 1, "A")
>
> End If
> Next
>
> Application.Goto DestSh.Cells(1)
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
> End Sub
> ___________________________________________________________________________________________
> Function LastRow(sh As Worksheet)
> On Error Resume Next
> LastRow = sh.Cells.Find(What:="*", _
> After:=sh.Range("A1"), _
> Lookat:=xlPart, _
> LookIn:=xlFormulas, _
> SearchOrder:=xlByRows, _
> SearchDirection:=xlPrevious, _
> MatchCase:=False).Row
> On Error GoTo 0
> End Function
>

 
Reply With Quote
 
SangelNet
Guest
Posts: n/a
 
      24th Oct 2007
On Oct 24, 11:37 am, "Ron de Bruin" <rondebr...@kabelfoon.nl> wrote:
> Then I think that there is a empty sheet in your workbook
>
> --
>
> Regards Ron de Bruinhttp://www.rondebruin.nl/tips.htm
>
> "SangelNet" <upslavazq...@gmail.com> wrote in messagenews:(E-Mail Removed)...
> > Yes Sir, I added the lastrow function. The code im using at this point
> > and getting error is this:

>
> > Sub merge()
> > Dim sh As Worksheet
> > Dim DestSh As Worksheet
> > Dim Last As Long
> > Dim shLast As Long

>
> > With Application
> > .ScreenUpdating = False
> > .EnableEvents = False
> > End With

>
> > 'Delete the sheet "MergeSheet" if it exist
> > Application.DisplayAlerts = False
> > On Error Resume Next
> > ThisWorkbook.Worksheets("MergeSheet").Delete
> > On Error GoTo 0
> > Application.DisplayAlerts = True

>
> > 'Add a worksheet with the name "MergeSheet"
> > Set DestSh = ThisWorkbook.Worksheets.Add
> > DestSh.Name = "MergeSheet"

>
> > 'loop through all worksheets and copy the data to the DestSh
> > For Each sh In ThisWorkbook.Worksheets
> > If sh.Name <> DestSh.Name And sh.Name <> "maindata" Then
> > Last = LastRow(DestSh)
> > shLast = LastRow(sh)

>
> > 'This example copies everything, if you only want to copy
> > 'values/formats look at the example below the first
> > example
> > sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
> > DestSh.Cells(Last + 1, "A")

>
> > End If
> > Next

>
> > Application.Goto DestSh.Cells(1)

>
> > With Application
> > .ScreenUpdating = True
> > .EnableEvents = True
> > End With
> > End Sub
> > ___________________________________________________________________________________________
> > Function LastRow(sh As Worksheet)
> > On Error Resume Next
> > LastRow = sh.Cells.Find(What:="*", _
> > After:=sh.Range("A1"), _
> > Lookat:=xlPart, _
> > LookIn:=xlFormulas, _
> > SearchOrder:=xlByRows, _
> > SearchDirection:=xlPrevious, _
> > MatchCase:=False).Row
> > On Error GoTo 0
> > End Function


No, No blanks. Actually now its merging just 2 of the sheets and then
giving the error. tried doing it with new clean sheets, still.
I will keep trying.


 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      24th Oct 2007
Send me the workbook private then i take a look

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"SangelNet" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> On Oct 24, 11:37 am, "Ron de Bruin" <rondebr...@kabelfoon.nl> wrote:
>> Then I think that there is a empty sheet in your workbook
>>
>> --
>>
>> Regards Ron de Bruinhttp://www.rondebruin.nl/tips.htm
>>
>> "SangelNet" <upslavazq...@gmail.com> wrote in messagenews:(E-Mail Removed)...
>> > Yes Sir, I added the lastrow function. The code im using at this point
>> > and getting error is this:

>>
>> > Sub merge()
>> > Dim sh As Worksheet
>> > Dim DestSh As Worksheet
>> > Dim Last As Long
>> > Dim shLast As Long

>>
>> > With Application
>> > .ScreenUpdating = False
>> > .EnableEvents = False
>> > End With

>>
>> > 'Delete the sheet "MergeSheet" if it exist
>> > Application.DisplayAlerts = False
>> > On Error Resume Next
>> > ThisWorkbook.Worksheets("MergeSheet").Delete
>> > On Error GoTo 0
>> > Application.DisplayAlerts = True

>>
>> > 'Add a worksheet with the name "MergeSheet"
>> > Set DestSh = ThisWorkbook.Worksheets.Add
>> > DestSh.Name = "MergeSheet"

>>
>> > 'loop through all worksheets and copy the data to the DestSh
>> > For Each sh In ThisWorkbook.Worksheets
>> > If sh.Name <> DestSh.Name And sh.Name <> "maindata" Then
>> > Last = LastRow(DestSh)
>> > shLast = LastRow(sh)

>>
>> > 'This example copies everything, if you only want to copy
>> > 'values/formats look at the example below the first
>> > example
>> > sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
>> > DestSh.Cells(Last + 1, "A")

>>
>> > End If
>> > Next

>>
>> > Application.Goto DestSh.Cells(1)

>>
>> > With Application
>> > .ScreenUpdating = True
>> > .EnableEvents = True
>> > End With
>> > End Sub
>> > ___________________________________________________________________________________________
>> > Function LastRow(sh As Worksheet)
>> > On Error Resume Next
>> > LastRow = sh.Cells.Find(What:="*", _
>> > After:=sh.Range("A1"), _
>> > Lookat:=xlPart, _
>> > LookIn:=xlFormulas, _
>> > SearchOrder:=xlByRows, _
>> > SearchDirection:=xlPrevious, _
>> > MatchCase:=False).Row
>> > On Error GoTo 0
>> > End Function

>
> No, No blanks. Actually now its merging just 2 of the sheets and then
> giving the error. tried doing it with new clean sheets, still.
> I will keep trying.
>
>

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      24th Oct 2007
Hi SangelNet

There are 3 sheets with data(V) in one cell in row
65338
65246
65399

So your range is to big to copy to one sheet
Use Ctrl-end on each sheet and you will find your last cell



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Ron de Bruin" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Send me the workbook private then i take a look
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "SangelNet" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> On Oct 24, 11:37 am, "Ron de Bruin" <rondebr...@kabelfoon.nl> wrote:
>>> Then I think that there is a empty sheet in your workbook
>>>
>>> --
>>>
>>> Regards Ron de Bruinhttp://www.rondebruin.nl/tips.htm
>>>
>>> "SangelNet" <upslavazq...@gmail.com> wrote in messagenews:(E-Mail Removed)...
>>> > Yes Sir, I added the lastrow function. The code im using at this point
>>> > and getting error is this:
>>>
>>> > Sub merge()
>>> > Dim sh As Worksheet
>>> > Dim DestSh As Worksheet
>>> > Dim Last As Long
>>> > Dim shLast As Long
>>>
>>> > With Application
>>> > .ScreenUpdating = False
>>> > .EnableEvents = False
>>> > End With
>>>
>>> > 'Delete the sheet "MergeSheet" if it exist
>>> > Application.DisplayAlerts = False
>>> > On Error Resume Next
>>> > ThisWorkbook.Worksheets("MergeSheet").Delete
>>> > On Error GoTo 0
>>> > Application.DisplayAlerts = True
>>>
>>> > 'Add a worksheet with the name "MergeSheet"
>>> > Set DestSh = ThisWorkbook.Worksheets.Add
>>> > DestSh.Name = "MergeSheet"
>>>
>>> > 'loop through all worksheets and copy the data to the DestSh
>>> > For Each sh In ThisWorkbook.Worksheets
>>> > If sh.Name <> DestSh.Name And sh.Name <> "maindata" Then
>>> > Last = LastRow(DestSh)
>>> > shLast = LastRow(sh)
>>>
>>> > 'This example copies everything, if you only want to copy
>>> > 'values/formats look at the example below the first
>>> > example
>>> > sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
>>> > DestSh.Cells(Last + 1, "A")
>>>
>>> > End If
>>> > Next
>>>
>>> > Application.Goto DestSh.Cells(1)
>>>
>>> > With Application
>>> > .ScreenUpdating = True
>>> > .EnableEvents = True
>>> > End With
>>> > End Sub
>>> > ___________________________________________________________________________________________
>>> > Function LastRow(sh As Worksheet)
>>> > On Error Resume Next
>>> > LastRow = sh.Cells.Find(What:="*", _
>>> > After:=sh.Range("A1"), _
>>> > Lookat:=xlPart, _
>>> > LookIn:=xlFormulas, _
>>> > SearchOrder:=xlByRows, _
>>> > SearchDirection:=xlPrevious, _
>>> > MatchCase:=False).Row
>>> > On Error GoTo 0
>>> > End Function

>>
>> No, No blanks. Actually now its merging just 2 of the sheets and then
>> giving the error. tried doing it with new clean sheets, still.
>> I will keep trying.
>>
>>

 
Reply With Quote
 
Sangel
Guest
Posts: n/a
 
      24th Oct 2007
On Oct 24, 2:28 pm, "Ron de Bruin" <rondebr...@kabelfoon.nl> wrote:
> Hi SangelNet
>
> There are 3 sheets with data(V) in one cell in row
> 65338
> 65246
> 65399
>
> So your range is to big to copy to one sheet
> Use Ctrl-end on each sheet and you will find your last cell
>
> --
>
> Regards Ron de Bruinhttp://www.rondebruin.nl/tips.htm
>
> "Ron de Bruin" <rondebr...@kabelfoon.nl> wrote in messagenews:(E-Mail Removed)...
>
> > Send me the workbook private then i take a look

>
> > --

>
> > Regards Ron de Bruin
> >http://www.rondebruin.nl/tips.htm

>
> > "SangelNet" <upslavazq...@gmail.com> wrote in messagenews:(E-Mail Removed)...
> >> On Oct 24, 11:37 am, "Ron de Bruin" <rondebr...@kabelfoon.nl> wrote:
> >>> Then I think that there is a empty sheet in your workbook

>
> >>> --

>
> >>> Regards Ron de Bruinhttp://www.rondebruin.nl/tips.htm

>
> >>> "SangelNet" <upslavazq...@gmail.com> wrote in messagenews:(E-Mail Removed)...
> >>> > Yes Sir, I added the lastrow function. The code im using at this point
> >>> > and getting error is this:

>
> >>> > Sub merge()
> >>> > Dim sh As Worksheet
> >>> > Dim DestSh As Worksheet
> >>> > Dim Last As Long
> >>> > Dim shLast As Long

>
> >>> > With Application
> >>> > .ScreenUpdating = False
> >>> > .EnableEvents = False
> >>> > End With

>
> >>> > 'Delete the sheet "MergeSheet" if it exist
> >>> > Application.DisplayAlerts = False
> >>> > On Error Resume Next
> >>> > ThisWorkbook.Worksheets("MergeSheet").Delete
> >>> > On Error GoTo 0
> >>> > Application.DisplayAlerts = True

>
> >>> > 'Add a worksheet with the name "MergeSheet"
> >>> > Set DestSh = ThisWorkbook.Worksheets.Add
> >>> > DestSh.Name = "MergeSheet"

>
> >>> > 'loop through all worksheets and copy the data to the DestSh
> >>> > For Each sh In ThisWorkbook.Worksheets
> >>> > If sh.Name <> DestSh.Name And sh.Name <> "maindata" Then
> >>> > Last = LastRow(DestSh)
> >>> > shLast = LastRow(sh)

>
> >>> > 'This example copies everything, if you only want to copy
> >>> > 'values/formats look at the example below the first
> >>> > example
> >>> > sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy
> >>> > DestSh.Cells(Last + 1, "A")

>
> >>> > End If
> >>> > Next

>
> >>> > Application.Goto DestSh.Cells(1)

>
> >>> > With Application
> >>> > .ScreenUpdating = True
> >>> > .EnableEvents = True
> >>> > End With
> >>> > End Sub
> >>> > ___________________________________________________________________________________________
> >>> > Function LastRow(sh As Worksheet)
> >>> > On Error Resume Next
> >>> > LastRow = sh.Cells.Find(What:="*", _
> >>> > After:=sh.Range("A1"), _
> >>> > Lookat:=xlPart, _
> >>> > LookIn:=xlFormulas, _
> >>> > SearchOrder:=xlByRows, _
> >>> > SearchDirection:=xlPrevious, _
> >>> > MatchCase:=False).Row
> >>> > On Error GoTo 0
> >>> > End Function

>
> >> No, No blanks. Actually now its merging just 2 of the sheets and then
> >> giving the error. tried doing it with new clean sheets, still.
> >> I will keep trying.


Ron

That definitely was it.
Thnx so much, you've been very kind.

Thnx also for the great info on your page.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Consolidate work sheets 240 Microsoft Excel Misc 1 8th Feb 2010 08:44 PM
CONSOLIDATE SHEETS Roger Microsoft Excel Worksheet Functions 1 28th Oct 2009 01:23 AM
Consolidate sheets automatically =?Utf-8?B?SW52b2ljZQ==?= Microsoft Excel Programming 4 4th Nov 2006 11:53 AM
how to consolidate sheets tommasopalazzot Microsoft Excel Programming 5 6th Nov 2005 04:09 PM
Consolidate sheets =?Utf-8?B?TWFub3M=?= Microsoft Excel Worksheet Functions 2 14th Feb 2005 03:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:36 AM.