A
avardaneg
I'm trying to use your Ron de Bruin's code to copy a range from a closed
workbook stored in a FTP location (http://www.rondebruin.nl/ado.htm), it was
working fine yesterday but today all of a sudden I started to have problems
with it. I was wondering you would have some clues of what's causing the
problem:
This is my code:
Sub get_wmreport()
Dim act As Worksheet
Dim currdate As Date
Dim newdate As Date
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Set act = ThisWorkbook.Sheets("WM_Report")
currdate = act.Cells(2, 13)
GetRange "ftp://ftp_user:[email protected]/Daily-EC-Active", "Daily EC
Metric Report_-_Active on NAP.xls", "Sheet1", "M2:M2", _
act.Range("M2:M2")
newdate = act.Cells(2, 13)
If newdate = currdate Then GoTo skip1:
act.Activate
act.Cells(5, 1).AutoFilter
act.Cells(5, 1).AutoFilter
act.Range("A660000").ClearContents
GetData "ftp://ftp_user:[email protected]/Daily-EC-Active/Daily EC
Metric Report_-_Active on NAP.xls", "Sheet1", _
"A660000", act.Range("A660000"), False, False
If IsEmpty(act.Cells(6, 1)) Then
act.Rows(6).Delete
End If
skip1:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
This is Ron's "GetData" code:
Public Sub GetData(SourceFile As Variant, SourceSheet As String, _
SourceRange As String, TargetRange As Range, Header As
Boolean, UseHeaderRow As Boolean)
' 30-Dec-2007, working in Excel 2000-2007
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
Dim lCount As Long
' Create the connection string.
If Header = False Then
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=No"";"
End If
Else
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
End If
End If
If SourceSheet = "" Then
' workbook level name
szSQL = "SELECT * FROM " & SourceRange$ & ";"
Else
' worksheet level name or range
szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];"
End If
On Error GoTo SomethingWrong
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
' Check to make sure we received data and copy the data
If Not rsData.EOF Then
If Header = False Then
TargetRange.Cells(1, 1).CopyFromRecordset rsData
Else
'Add the header cell in each column if the last argument is True
If UseHeaderRow Then
For lCount = 0 To rsData.Fields.Count - 1
TargetRange.Cells(1, 1 + lCount).Value = _
rsData.Fields(lCount).Name
Next lCount
TargetRange.Cells(2, 1).CopyFromRecordset rsData
Else
TargetRange.Cells(1, 1).CopyFromRecordset rsData
End If
End If
Else
MsgBox "No records returned from : " & SourceFile, vbCritical
End If
' Clean up our Recordset object.
rsData.Close
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub
SomethingWrong:
MsgBox "The file name, Sheet name or Range is invalid of : " &
SourceFile, _
vbExclamation, "Error"
On Error GoTo 0
End Sub
It works fine the first time I run the macro, but if I run it again right
after the first time it errors out:
"The file name, Sheet name or Range is invalid of... "
If I close the workbook and Excel, re-open and run the code again it works.
I have also noticed that when I step through the "GetData" code it seems to
be hanging a long time at the following line:
rsCon.Close
Any help would be greatly appreciated.
workbook stored in a FTP location (http://www.rondebruin.nl/ado.htm), it was
working fine yesterday but today all of a sudden I started to have problems
with it. I was wondering you would have some clues of what's causing the
problem:
This is my code:
Sub get_wmreport()
Dim act As Worksheet
Dim currdate As Date
Dim newdate As Date
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Set act = ThisWorkbook.Sheets("WM_Report")
currdate = act.Cells(2, 13)
GetRange "ftp://ftp_user:[email protected]/Daily-EC-Active", "Daily EC
Metric Report_-_Active on NAP.xls", "Sheet1", "M2:M2", _
act.Range("M2:M2")
newdate = act.Cells(2, 13)
If newdate = currdate Then GoTo skip1:
act.Activate
act.Cells(5, 1).AutoFilter
act.Cells(5, 1).AutoFilter
act.Range("A660000").ClearContents
GetData "ftp://ftp_user:[email protected]/Daily-EC-Active/Daily EC
Metric Report_-_Active on NAP.xls", "Sheet1", _
"A660000", act.Range("A660000"), False, False
If IsEmpty(act.Cells(6, 1)) Then
act.Rows(6).Delete
End If
skip1:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
This is Ron's "GetData" code:
Public Sub GetData(SourceFile As Variant, SourceSheet As String, _
SourceRange As String, TargetRange As Range, Header As
Boolean, UseHeaderRow As Boolean)
' 30-Dec-2007, working in Excel 2000-2007
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
Dim lCount As Long
' Create the connection string.
If Header = False Then
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=No"";"
End If
Else
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
End If
End If
If SourceSheet = "" Then
' workbook level name
szSQL = "SELECT * FROM " & SourceRange$ & ";"
Else
' worksheet level name or range
szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];"
End If
On Error GoTo SomethingWrong
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
' Check to make sure we received data and copy the data
If Not rsData.EOF Then
If Header = False Then
TargetRange.Cells(1, 1).CopyFromRecordset rsData
Else
'Add the header cell in each column if the last argument is True
If UseHeaderRow Then
For lCount = 0 To rsData.Fields.Count - 1
TargetRange.Cells(1, 1 + lCount).Value = _
rsData.Fields(lCount).Name
Next lCount
TargetRange.Cells(2, 1).CopyFromRecordset rsData
Else
TargetRange.Cells(1, 1).CopyFromRecordset rsData
End If
End If
Else
MsgBox "No records returned from : " & SourceFile, vbCritical
End If
' Clean up our Recordset object.
rsData.Close
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub
SomethingWrong:
MsgBox "The file name, Sheet name or Range is invalid of : " &
SourceFile, _
vbExclamation, "Error"
On Error GoTo 0
End Sub
It works fine the first time I run the macro, but if I run it again right
after the first time it errors out:
"The file name, Sheet name or Range is invalid of... "
If I close the workbook and Excel, re-open and run the code again it works.
I have also noticed that when I step through the "GetData" code it seems to
be hanging a long time at the following line:
rsCon.Close
Any help would be greatly appreciated.