Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default help with merging worksheets


Having some problems with the code below. If I uncomment the lines I need to
change it will work correctly. but if I run it like it is I only get some on
the stuff copied.

Option Explicit

Sub Consolidate()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim FirstCell As String

'Fill in the path\folder where the files are
MyPath = "D:\Documents and Settings\newuser\Desktop\testing"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) < "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath < ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet
'Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Set BaseWks = ActiveWorkbook.Worksheets("Form1")
rnum = 1

'Loop through all files in the array(myFiles)
If FNum 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0

If Not mybook Is Nothing Then

On Error Resume Next

With mybook.Worksheets(1)
FirstCell = "A7"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3,
..Cells))

'Test if the row of the last cell = then the row of the
FirstCell
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With

If Err.Number 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count = BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

SourceRcount = sourceRange.Rows.Count

If rnum + SourceRcount = BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

'Copy the file name in column A
With sourceRange
'BaseWks.Cells(rnum, "A"). _
' Resize(.Rows.Count).Value = MyFiles(FNum)
End With

'Set the destrange
'Set destrange = BaseWks.Range("B" & rnum)
Set destrange = BaseWks.Range("A7")

'we copy the values from the sourceRange to the
destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
..Columns.Count)
End With
destrange.Value = sourceRange.Value

rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If

Next FNum
BaseWks.Columns.AutoFit
End If

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default help with merging worksheets


You will need to give a better explanation of the problem. However, one
thing that I see is that you are assigning a value to Range("A7") on each
iteration of a loop, which means you will only get the value of the last
iteration displayed in the cell.
See if you can describe what you want to get as a result, and what you are
getting.

Don't make us have to run your code to figure out the problem, many of us
have other things to do.



"Bobbo" wrote in message
...
Having some problems with the code below. If I uncomment the lines I need
to
change it will work correctly. but if I run it like it is I only get some
on
the stuff copied.

Option Explicit

Sub Consolidate()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim FirstCell As String

'Fill in the path\folder where the files are
MyPath = "D:\Documents and Settings\newuser\Desktop\testing"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) < "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath < ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet
'Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Set BaseWks = ActiveWorkbook.Worksheets("Form1")
rnum = 1

'Loop through all files in the array(myFiles)
If FNum 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0

If Not mybook Is Nothing Then

On Error Resume Next

With mybook.Worksheets(1)
FirstCell = "A7"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3,
.Cells))

'Test if the row of the last cell = then the row of
the
FirstCell
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With

If Err.Number 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count = BaseWks.Columns.Count
Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

SourceRcount = sourceRange.Rows.Count

If rnum + SourceRcount = BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the
sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

'Copy the file name in column A
With sourceRange
'BaseWks.Cells(rnum, "A"). _
' Resize(.Rows.Count).Value =
MyFiles(FNum)
End With

'Set the destrange
'Set destrange = BaseWks.Range("B" & rnum)
Set destrange = BaseWks.Range("A7")

'we copy the values from the sourceRange to the
destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = sourceRange.Value

rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If

Next FNum
BaseWks.Columns.AutoFit
End If

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default help with merging worksheets


What I am trying to do is search through a folder and get all of the data
from the workbooks. I need to grab the data starting at row 7 down from the
first worksheet only of each xls. Then put the data in the currently open
workbook on sheet 1 starting at row 7.

I hope this helps

Thanks
Bob

"JLGWhiz" wrote:

You will need to give a better explanation of the problem. However, one
thing that I see is that you are assigning a value to Range("A7") on each
iteration of a loop, which means you will only get the value of the last
iteration displayed in the cell.
See if you can describe what you want to get as a result, and what you are
getting.

Don't make us have to run your code to figure out the problem, many of us
have other things to do.



"Bobbo" wrote in message
...
Having some problems with the code below. If I uncomment the lines I need
to
change it will work correctly. but if I run it like it is I only get some
on
the stuff copied.

Option Explicit

Sub Consolidate()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim FirstCell As String

'Fill in the path\folder where the files are
MyPath = "D:\Documents and Settings\newuser\Desktop\testing"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) < "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath < ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet
'Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Set BaseWks = ActiveWorkbook.Worksheets("Form1")
rnum = 1

'Loop through all files in the array(myFiles)
If FNum 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0

If Not mybook Is Nothing Then

On Error Resume Next

With mybook.Worksheets(1)
FirstCell = "A7"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3,
.Cells))

'Test if the row of the last cell = then the row of
the
FirstCell
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With

If Err.Number 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count = BaseWks.Columns.Count
Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

SourceRcount = sourceRange.Rows.Count

If rnum + SourceRcount = BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the
sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

'Copy the file name in column A
With sourceRange
'BaseWks.Cells(rnum, "A"). _
' Resize(.Rows.Count).Value =
MyFiles(FNum)
End With

'Set the destrange
'Set destrange = BaseWks.Range("B" & rnum)
Set destrange = BaseWks.Range("A7")

'we copy the values from the sourceRange to the
destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = sourceRange.Value

rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If

Next FNum
BaseWks.Columns.AutoFit
End If

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default help with merging worksheets


Hi Bobbo

If you use the example here
http://www.rondebruin.nl/copy3.htm

You can chnage

'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1


To

Set BaseWks = ActiveWorkbook.Worksheets("Sheet1")
rnum = 7



--

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




"Bobbo" wrote in message ...
What I am trying to do is search through a folder and get all of the data
from the workbooks. I need to grab the data starting at row 7 down from the
first worksheet only of each xls. Then put the data in the currently open
workbook on sheet 1 starting at row 7.

I hope this helps

Thanks
Bob

"JLGWhiz" wrote:

You will need to give a better explanation of the problem. However, one
thing that I see is that you are assigning a value to Range("A7") on each
iteration of a loop, which means you will only get the value of the last
iteration displayed in the cell.
See if you can describe what you want to get as a result, and what you are
getting.

Don't make us have to run your code to figure out the problem, many of us
have other things to do.



"Bobbo" wrote in message
...
Having some problems with the code below. If I uncomment the lines I need
to
change it will work correctly. but if I run it like it is I only get some
on
the stuff copied.

Option Explicit

Sub Consolidate()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim FirstCell As String

'Fill in the path\folder where the files are
MyPath = "D:\Documents and Settings\newuser\Desktop\testing"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) < "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath < ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet
'Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Set BaseWks = ActiveWorkbook.Worksheets("Form1")
rnum = 1

'Loop through all files in the array(myFiles)
If FNum 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0

If Not mybook Is Nothing Then

On Error Resume Next

With mybook.Worksheets(1)
FirstCell = "A7"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3,
.Cells))

'Test if the row of the last cell = then the row of
the
FirstCell
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With

If Err.Number 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count = BaseWks.Columns.Count
Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

SourceRcount = sourceRange.Rows.Count

If rnum + SourceRcount = BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the
sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

'Copy the file name in column A
With sourceRange
'BaseWks.Cells(rnum, "A"). _
' Resize(.Rows.Count).Value =
MyFiles(FNum)
End With

'Set the destrange
'Set destrange = BaseWks.Range("B" & rnum)
Set destrange = BaseWks.Range("A7")

'we copy the values from the sourceRange to the
destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = sourceRange.Value

rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If

Next FNum
BaseWks.Columns.AutoFit
End If

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging worksheets [email protected] Excel Programming 0 September 24th 06 05:57 AM
Merging worksheets into one Edward Owen Excel Programming 2 May 17th 06 06:50 PM
Merging Different worksheets John Excel Discussion (Misc queries) 0 February 10th 05 03:15 PM
merging worksheets onto one Doug Excel Programming 2 December 9th 04 07:49 PM
Merging Worksheets Kulwinder Oberoi Excel Programming 1 October 5th 03 03:17 AM


All times are GMT +1. The time now is 05:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"