Update Sheet From CSV Problem In Excel 2007

___

New Member
Joined
Nov 16, 2004
Messages
14
Morning / Evening All,

I'm having a small problem while trying to update my 'products' sheet from an external .csv file in Excel 2007. I'm using the following code which works fine in Excel 2000...

Code:
Sub openProductcsv()
On Error Resume Next

If ping("***.***.***.***") Then

    Application.DisplayAlerts = False
          
        If Not ThisWorkbook.ProtectStructure Then
          
              Sheets("products").Delete
 
        End If
   
    Workbooks.Open Filename:="http://mydomain.co.uk/orders/data/products.csv"

    ActiveSheet.Move Before:=Workbooks("myworkbook.xls").Sheets(1)
    
Else
    
    MsgBox "Cannot connect to server. Please make sure you have internet access or use the 'Save Order For later Upload' button."
    
End If

    Application.DisplayAlerts = True

End Sub

The error message I'm getting is...
Run-time error '1004'

Excel cannot insert the sheets into the destination workbook because it contains fewer rows and columns than the source workbook, you can select the data and then use Copy and Paste commands to insert it into the sheets of another workbook.

I'm guessing that this is because Excel 2007 has many more columns and rows than 2000 but I'm not sure of the best way to resolve the problem.

Any help much appreciated.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi

Rather than physically move the sheet you could just copy the values:

Code:
Sub openProductcsv()
On Error Resume Next
 
If ping("***.***.***.***") Then
 
    Application.DisplayAlerts = False
 
        If Not ThisWorkbook.ProtectStructure Then
 
              Sheets("products").Delete
 
        End If
 
    Workbooks.Open Filename:="http://mydomain.co.uk/orders/data/products.csv"
 
    ActiveSheet.UsedRange.Copy
    Workbooks("myworkbook.xls").Worksheets.Add.Paste
 
Else
 
    MsgBox "Cannot connect to server. Please make sure you have internet access or use the 'Save Order For later Upload' button."
 
End If
 
    Application.DisplayAlerts = True
 
End Sub
 
Upvote 0
Hi Richard and thanks for your quick reply.

I have just tried your solution which works fine in both 2000 and 2007, the only problem is it doesn't close the .csv file after pasting and pastes to sheet1 which it inserts next to the last active sheet. Is there any way it could insert a sheet called 'products' in position one of the tabs then paste the data and close the .csv?

Once again, many thanks for your help.
 
Upvote 0
Yep - try this:

Rich (BB code):
Sub openProductcsv()
Dim wb As Workbook, ws As Worksheet
On Error Resume Next
 
If ping("***.***.***.***") Then
 
    Application.DisplayAlerts = False
 
        If Not ThisWorkbook.ProtectStructure Then
 
              Sheets("products").Delete
 
        End If
 
    Set wb = Workbooks.Open(Filename:=http://mydomain.co.uk/orders/data/products.csv)
 
    wb.Sheets(1).UsedRange.Copy
    With Workbooks("myworkbook.xls")
        Set ws =  .Worksheets.Add(Before:=.Sheets(1))
     End With
     ws.Paste
     wb.Close SaveChanges:=False
   
 
Else
 
    MsgBox "Cannot connect to server. Please make sure you have internet access or use the 'Save Order For later Upload' button."
 
End If
 
    Application.DisplayAlerts = True
 
End Sub
 
Upvote 0
Great! thank you!

I was getting a syntax error on line

Code:
Set wb = Workbooks.Open(Filename:=http://mydomain.co.uk/orders/data/products.csv)

but changed it to

Code:
Set wb = Workbooks.Open(Filename:="http://lucylocketmail.co.uk/orders/data/products.csv")

and all works!

Still a slight issuse though, I rename the sheet as follows...

Code:
    wb.Sheets(1).UsedRange.Copy
    With Workbooks("myworkbook.xls")
        Set ws = .Worksheets.Add(Before:=.Sheets(1))
     End With
     ws.Paste
     wb.Close SaveChanges:=False
     Sheets("Sheet1").Name = "products"
     Range("A1").Select

which works fine unless I fire the code a second time where it inserts the sheet as Sheet2, and doesn't rename it to 'products'.

Any ideas?

Thanks again for your time and help.
 
Upvote 0
In the adjusted code, I set an object reference (ws) to be the newly added sheet, so you can use this to rename it:

Code:
 wb.Sheets(1).UsedRange.Copy
    With Workbooks("myworkbook.xls")
        Set ws = .Worksheets.Add(Before:=.Sheets(1))
     End With
     ws.Paste
     wb.Close SaveChanges:=False
     ws.Name = "products"
     ws.Range("A1").Select
 
Upvote 0

Forum statistics

Threads
1,203,733
Messages
6,057,051
Members
444,902
Latest member
ExerciseInFutility

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