I need to include a macro in my spreadsheet that will take a directory of files and combine them into a single specific sheet in my spreadsheet. I found code here( that I could tweak for my specific purposes, and it works in most cases. After a few test runs, I'm now guessing that it's only when a cell has too long of a string that the macro throws an error and comes to a halt. A cell with 863 characters is fine, but a cell with 1,042 characters will stop the macro. Can anyone help me? Since I didn't write the original code, I don't feel I understand it enough to track down the problem. (I'm sorry this code is so long - 90% of the responses I see on this forum ask the OP to post more of his/her code, so I'm trying to think ahead)
Code:
Sub Get_Data(FileNameInA As Boolean, PasteAsValues As Boolean, SourceShName As String, _
SourceShIndex As Integer, SourceRng As String, StartCell As String)
On Error GoTo A_Error:
Dim SourceRcount As Long
Dim SourceRange As Range, destrange As Range
Dim mybook As Workbook, BaseWks As Worksheet
Dim rnum As Long, CalcMode As Long
Dim SourceSh As Variant
Dim sh As Worksheet
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet named "Combined Sheet"
Set BaseWks = ThisWorkbook.Worksheets.Add
BaseWks.Name = "Combined Sheet"
'Set start row for the Data
rnum = 1
'Check if we use a named sheet or the index
If SourceShName = "" Then
SourceSh = SourceShIndex
Else
SourceSh = SourceShName
End If
'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(MyFiles(fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
If LCase(SourceShName) <> "all" Then
'Set SourceRange and check if it is a valid range
On Error Resume Next
If StartCell <> "" Then
With mybook.Sheets(SourceSh)
Set SourceRange = .Range(StartCell & ":" & RDB_Last(3, .Cells))
End With
Else
With mybook.Sheets(SourceSh)
Set SourceRange = Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If
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
'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If
'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("B" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(fnum)
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If
'
'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.NumberFormat = "@"
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If
rnum = rnum + SourceRcount
End If
'Close the workbook without saving
On Error Resume Next
Application.DisplayAlerts = False
mybook.Close savechanges:=False
Application.DisplayAlerts = True
On Error GoTo 0
Else
'Loop through all sheets in mybook
For Each sh In mybook.Worksheets
'Set SourceRange and check if it is a valid range
On Error Resume Next
If StartCell <> "" Then
With sh
Set SourceRange = .Range(StartCell & ":" & RDB_Last(3, .Cells))
End With
Else
With sh
Set SourceRange = Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If
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
'Check if there are enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry, there are not enough rows in the sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If
'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("B" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(fnum)
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If
'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If
rnum = rnum + SourceRcount
End If
Next sh
'Close the workbook without saving
mybook.Close savechanges:=False
End If
End If
Next fnum
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
Erase MyFiles()
A_Error:
If Err.Number <> 0 Then
MsgBox "Get_Data procedure; Error number: " & Err.Number & " , Description: " & Err.Description
End If
End Sub