help with merging worksheets

B

Bobbo

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
 
J

JLGWhiz

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.
 
B

Bobbo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro Help!!!!!! 2
Copy non-contiguous cells from all workbooks in folder 8
Import Data from Multiple Sources 8
Windows XP vba code help 10
Copy from fixed range does not work 6
Open CSV File 4
Open files in order 11
Copy part of text file 4

Top