1

As you can see below I have used fileName as string variable,which is used in the URL. When I run the code I get an error stating 'Run time error '-2147217376(80041020) The system cannot locate object specified.' Actually, I want to copy data from a .cg file to Excel. Any alternative method will also do. Please help.

'My Code

Sub ImportCG()

Dim directory As String, fileName As String

   directory = "D:\CG FILE"
  fileName = Dir(directory & "*.cg??")

ActiveWorkbook.Worksheets.Add
   ActiveWorkbook.XmlImport Url:= _
        "D:\CG FILE\filename" _
        , ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$1")

     'Add CG_List
    Set NewSheet = Sheets.Add(Type:=xlWorksheet)
    ActiveSheet.Name = "CG_List"

    'Copy contents to Component Type List
    Columns("D:D").Select
    Selection.Copy
    Sheets("ComponentTypeList").Select
    Columns("A:A").Select
    ActiveSheet.Paste
    Sheets("CG_List").Select
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    Columns("G:G").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("ComponentTypeList").Select
    Columns("B:B").Select
    ActiveSheet.Paste
    Range("B15").Select

End Sub

2 Answers 2

2

your directory variable and url need some corrections

Sub ImportCG()

Dim directory As String, fileName As String

   directory = "D:\CG FILE\"
  fileName = Dir(directory & "*.cg??")

ActiveWorkbook.Worksheets.Add
   ActiveWorkbook.XmlImport Url:= _
        directory & filename _
        , ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$1")

     'Add CG_List
    Set NewSheet = Sheets.Add(Type:=xlWorksheet)
    ActiveSheet.Name = "CG_List"

    'Copy contents to Component Type List
    Columns("D:D").Select
    Selection.Copy
    Sheets("ComponentTypeList").Select
    Columns("A:A").Select
    ActiveSheet.Paste
    Sheets("CG_List").Select
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    Columns("G:G").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("ComponentTypeList").Select
    Columns("B:B").Select
    ActiveSheet.Paste
    Range("B15").Select

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Hello Buddy thanks for the solution ,its working as wanted it.
1

"D:\CG FILE\" & filename you need to concatenate the 2

1 Comment

Hello Buddy thanks for the reply ,but the concatenation is done at this step fileName = Dir(directory & "*.cg??")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.