Excel 2003 to 2007 Problem

dantheram

Board Regular
Joined
Aug 27, 2010
Messages
192
Office Version
  1. 365
Platform
  1. Windows
Can someone help me understand why this doesn't work in excel 2007. It fails to find the file i specify. I have a REF sheet where the file path and workbook name are held in seperate columns and the VBA joins them together before locating the workbook - in 2003 it works fine, in 2007 it fails


Code:
Public Sub ImportWorkbookData()
Dim wkbData As Workbook
Dim wksOut As Worksheet
Dim wksData As Worksheet
Dim rngPeriodOut As Range
Dim rngOut As Range
Dim rngData As Range
Dim rngWkbList As Range
Dim rngPeriod As Range
Dim lngRow As Long
Dim lngNumProcessed As Long
Dim strWkbPath As String
Dim strOutputSheet As String
Dim strDataSheet As String
Dim strWkbName As String
Dim strProcess As String
Dim strOutputRange As String
Dim strDataRange As String
Dim dtmStart As Date
'Handle errors
On Error GoTo Error_Label
'Check the number of workbooks open before proceeding
If Application.Workbooks.Count > 2 Then
'More than one workbook open in Excel
Call MsgBox("Please close all other workbooks before running importing data.", vbOKOnly + vbInformation)
Exit Sub

'Check user wants to proceed
ElseIf MsgBox("Import data from other workbooks?", vbYesNo + vbQuestion) = vbNo Then
'User does not want to proceed
Exit Sub
End If
Call Show_Sheets
'Initialise counter
lngNumProcessed = 0

'Activate REF DATA worksheet in this workbook
ThisWorkbook.Activate
ThisWorkbook.Worksheets("REF DATA").Select
'Override application settings
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Application.DisplayStatusBar = True
Application.ScreenUpdating = False
'Set reference to named range in this workbook
Set rngWkbList = ThisWorkbook.Worksheets("REF DATA").Range("WORKBOOK_LIST")

'Run queries listed
For lngRow = 1 To rngWkbList.Rows.Count Step 1

'Get workbook details, data and output ranges
strWkbPath = Trim$(rngWkbList.Cells(lngRow, 1).Value)
strWkbName = Trim$(rngWkbList.Cells(lngRow, 2).Value)
strDataSheet = Trim$(rngWkbList.Cells(lngRow, 3).Value)
strDataRange = Trim$(rngWkbList.Cells(lngRow, 4).Value)
strOutputSheet = Trim$(rngWkbList.Cells(lngRow, 5).Value)
strOutputRange = Trim$(rngWkbList.Cells(lngRow, 6).Value)
strProcess = Trim$(UCase$(rngWkbList.Cells(lngRow, 9).Value))

'If details not blank
If strWkbPath <> vbNullString And _
strDataSheet <> vbNullString And strDataRange <> vbNullString And _
strOutputSheet <> vbNullString And strOutputRange <> vbNullString And _
strProcess = "Y" Then

'Amend path to include backslash character
If Right$(strWkbPath, 1) <> "\" Then strWkbPath = strWkbPath & "\"

'Show user progress text
ThisWorkbook.Activate
rngWkbList.Parent.Select
dtmStart = Now()
rngWkbList.Cells(lngRow, 7).Value = "Started: " & Format(dtmStart, "DD-MM-YYYY HH:MM:SS") & vbLf & "Finished: NOT FINISHED"
rngWkbList.Cells(lngRow, 8).Value = Application.UserName
Application.StatusBar = "Processing workbook " & strWkbPath & strWkbName

'Don't handle errors in this next section of code
On Error Resume Next

'Set worksheet reference (this workbook)
Err.Clear
Set wksOut = ThisWorkbook.Worksheets(strOutputSheet)
If Err.Number <> 0 Then
On Error GoTo Error_Label
Err.Raise vbObjectError + 84, "ImportWorkbookData", "Invalid output worksheet!"
Else
wksOut.Select
wksOut.Unprotect
End If

'Set range reference (this workbook)
Err.Clear
Set rngOut = wksOut.Range(strOutputRange)
If Err.Number <> 0 Then
On Error GoTo Error_Label
Err.Raise vbObjectError + 85, "ImportWorkbookData", "Invalid output range!"
Else
rngOut.Cells.ClearContents
rngOut.Cells.ClearComments
End If

'Close all other workbooks
For Each wkbData In Application.Workbooks
If UCase(wkbData.FullName) = UCase(strWkbPath & strWkbName) Then
' Do nothing, gonna use that wkbdata
Exit For
ElseIf wkbData.Name <> ThisWorkbook.Name Then
' Don't close this workbook but close any others
wkbData.Close savechanges:=False
End If
Next wkbData

'Open the data workbook
Err.Clear
If wkbData Is Nothing Then Set wkbData = Workbooks.Open(Filename:=strWkbPath & strWkbName, updatelinks:=False, ReadOnly:=True)
If Err.Number <> 0 Then
On Error GoTo Error_Label
Err.Raise vbObjectError + 85, "ImportWorkbookData", "The workbook '" & strWkbPath & "' does not exist!"
End If

wkbData.Activate

'Set reference to data sheet in data workbook
Err.Clear
Set wksData = wkbData.Worksheets(strDataSheet)
If Err.Number <> 0 Then
On Error GoTo Error_Label
Err.Raise vbObjectError + 85, "ImportWorkbookData", "The worksheet '" & strDataSheet & "' does not exist in '" & strWkbPath & "'"
Else
wksData.Select
End If

'Set reference to data range on data worksheet
Err.Clear
Set rngData = wksData.Range(strDataRange)
Set rngPeriod = wksData.Range("A1")
rngPeriod.Select


'We can use this to copy the A1 Cell from the sheet in a variable string
If Err.Number <> 0 Then
On Error GoTo Error_Label
Err.Raise vbObjectError + 85, "ImportWorkbookData", "The range '" & strDataRange & "' does not exist on the '" & strDataSheet & "' worksheet in '" & strWkbPath & "'"
End If

'Handle errors again
On Error GoTo Error_Label

'Copy the data over (and add comment for audit purposes)
rngData.Copy
ThisWorkbook.Activate
wksOut.Select
rngOut.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False

'rngPeriod.Copy
'ThisWorkbook.Activate
'Range("A1").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False


'rngOut.Cells(1, 1).AddComment "Updated on " & Format(Now, "DD/MM/YYYY HH:MM:SS") & vbLf & " from " & strWkbPath & strWkbName & vbLf & strDataSheet & vbLf & "by " & Application.UserName

'Update progress text
'rngWkbList.Cells(lngRow, 7).Value = "Started: " & Format(dtmStart, "DD-MM-YYYY HH:MM:SS") & vbLf & "Finished: " & Format(Now(), "DD-MM-YYYY HH:MM:SS")

'Release resources
Set rngOut = Nothing
Set wksOut = Nothing
Set rngData = Nothing
Set wksData = Nothing
Set rngPeriod = Nothing

'Keep track of the number of workbooks processed
lngNumProcessed = lngNumProcessed + 1

End If

Next lngRow
Exit_Label:
'Don't handle errors in this section of code
On Error Resume Next

'Restore application settings
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
Application.StatusBar = False
Application.ScreenUpdating = True

'Release resources
If Not wkbData Is Nothing Then
wkbData.Close savechanges:=False
Set wkbData = Nothing
End If
Set wksOut = Nothing
Set wksData = Nothing
Set rngOut = Nothing
Set rngData = Nothing
Set rngWkbList = Nothing
Set rngPeriod = Nothing


'Prompt user with confirmation
ThisWorkbook.Activate
' Call method to refresh Pivot Table
'Call Refresh_Pivot_Tables
'Call Hide_Sheets
ThisWorkbook.Worksheets("REF DATA").Select
Call MsgBox(lngNumProcessed & " worksheets updated.", vbOKOnly + vbInformation)
Exit Sub


'Error handler
Error_Label:
Call MsgBox(Err.Description, vbOKOnly + vbCritical, "Error detected in " & Err.Source)
Resume Exit_Label
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Can you elaborate (a lot) on what "fails" means?

simply it can't find the workbook. It is given the below -

C:\Documents and Settings\user\Desktop\New Folder\New Folder3

and

Book01.xls

then it joins these together to locate the file before opening it and retrieving data. Below is the part which it fails on, returning - "does not exist"

'Open the data workbook
Err.Clear
If wkbData Is Nothing Then Set wkbData = Workbooks.Open(Filename:=strWkbPath & strWkbName, updatelinks:=False, ReadOnly:=True)
If Err.Number <> 0 Then
On Error GoTo Error_Label
Err.Raise vbObjectError + 85, "ImportWorkbookData", "The workbook '" & strWkbPath & "' does not exist!"
End If

As i state it works fine, as it is, on 2003.

The locals window displays the correct data too, so i can see the path and workbook listed.
 
Last edited:
Upvote 0
simply it can't find the workbook. It is given the below -

C:\Documents and Settings\user\Desktop\New Folder\New Folder3

and

Book01.xls

then it joins these together to locate the file before opening it and retrieving data. Below is the part which it fails on, returning - "does not exist"

'Open the data workbook
Err.Clear
If wkbData Is Nothing Then Set wkbData = Workbooks.Open(Filename:=strWkbPath & strWkbName, updatelinks:=False, ReadOnly:=True)
If Err.Number <> 0 Then
On Error GoTo Error_Label
Err.Raise vbObjectError + 85, "ImportWorkbookData", "The workbook '" & strWkbPath & "' does not exist!"
End If

As i state it works fine, as it is, on 2003.

The locals window displays the correct data too, so i can see the path and workbook listed.


simply - is there anything in 2003 that wont work in 2007 in the code above??

Thanks for any info
 
Upvote 0
The file extensions for Excel 2007 and later are different than Excel 2003 e.g.; .xlsx or .xlsm

Is the strWkbName file name extension correct?
 
Upvote 0
The file extensions for Excel 2007 and later are different than Excel 2003 e.g.; .xlsx or .xlsm

Is the strWkbName file name extension correct?

I thought i'd fixed it when i remebered this last night - changed xls to xlsm but, alas, no luck, still couldn't find the file.

i'm going to test it on the desktop as i think it may be an issue with the network location.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top