VBA import of an IQY file without prompt

Sidrat

New Member
Joined
Feb 22, 2024
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
This is my first post on here so please be gentle with me if I have explained this wrong, I have looked at other posts on here about this but could not find the right answer and know there are some clever VBA 'Jedi's' out there who will most probably know the answer.

I have a spreadsheet that loads in .IQY data, manipulates it, then saves as a standalone spreadsheet with all macros etc stripped out. The data manipulation part and save all work how I need, it is the data input I need help with.

I have got part of the .IQY automated in a VBA macro but I am trying to automate the prompt part of the data import, due to the nature of the place I work the initial locating the IQY file will need to stay as is. It is once the user has selected the file I am trying to automate at the current Import Data prompt, this is currently displayed -

1708598486249.png


At this point the user selects Existing worksheet (leaves the cell a =$A$1) and clicks OK, it is this prompt I am trying to automate. I have tried recording a macro doing the whole IQY data import and it records successfully, for some reason when I run it again it errors. I believe it is to do with the way our LAN is configured.

The current part of the VBA that gets this far is -

FileToOpen = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.iqy*),*.iqy*", Title:="Open Database File")
If FileToOpen <> False Then

Workbooks.OpenDatabase FileName:=FileToOpen, _
CommandText:=Array( _
"<LIST><VIEWGUID>???????-?????-??????-??????</VIEWGUID><LISTNAME>?????????-????-?????????-???????</LIST" _
, _
"NAME><LISTWEB>https://fredfred.sharepoint.com/teams/????/??/?/_vti_bin</LISTWEB><LISTSUBWEB></LISTSUBWEB><ROOTFOLDE" _
, "R></ROOTFOLDER></LIST>"), CommandType:=5, ImportDataAs:=xlTable

End If

Please note the ???? in the above does have letters and or numbers in, they have been removed at the request of the administrator.

This works perfectly, I just need to automate the prompt part that will import the data to the Existing worksheet to cell $A$1, it will stop errors people are making by selecting the wrong options in the prompt, even though they have a work guide on how to run it.

Thanks in advance
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this macro, however I'm not sure if it will work for your scenario.

VBA Code:
Public Sub Import_IQY_Data()

    Dim FileToOpen As Variant
    Dim qt As QueryTable
    Dim qtResultAddress As String
    
    FileToOpen = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.iqy*),*.iqy*", Title:="Open Database File")
    
    If FileToOpen <> False Then
    
        With ActiveSheet
        
            Set qt = .QueryTables.Add(Connection:="FINDER;" & FileToOpen, Destination:=.Range("A1"))
            With qt
                .CommandType = xlCmdList
                .CommandText = Array("<LIST><VIEWGUID>???????-?????-??????-??????</VIEWGUID><LISTNAME>?????????-????-?????????-???????</LIST", _
                                     "NAME><LISTWEB>https://fredfred.sharepoint.com/teams/????/??/?/_vti_bin</LISTWEB><LISTSUBWEB></LISTSUBWEB><ROOTFOLDER>", _
                                     "</ROOTFOLDER></LIST>")
                .Refresh
                'Save the result range address
                qtResultAddress = .ResultRange.Address
                'Must delete the QueryTable to convert its result range to a table
                .Delete
            End With
           
            'Convert the result range to a table named "DataTable"
            .ListObjects.Add(xlSrcRange, .Range(qtResultAddress), , xlYes).Name = "DataTable"
            
        End With
   
    End If
        
End Sub
 
Upvote 0
Try this macro, however I'm not sure if it will work for your scenario.

VBA Code:
Public Sub Import_IQY_Data()

    Dim FileToOpen As Variant
    Dim qt As QueryTable
    Dim qtResultAddress As String
   
    FileToOpen = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.iqy*),*.iqy*", Title:="Open Database File")
   
    If FileToOpen <> False Then
   
        With ActiveSheet
       
            Set qt = .QueryTables.Add(Connection:="FINDER;" & FileToOpen, Destination:=.Range("A1"))
            With qt
                .CommandType = xlCmdList
                .CommandText = Array("<LIST><VIEWGUID>???????-?????-??????-??????</VIEWGUID><LISTNAME>?????????-????-?????????-???????</LIST", _
                                     "NAME><LISTWEB>https://fredfred.sharepoint.com/teams/????/??/?/_vti_bin</LISTWEB><LISTSUBWEB></LISTSUBWEB><ROOTFOLDER>", _
                                     "</ROOTFOLDER></LIST>")
                .Refresh
                'Save the result range address
                qtResultAddress = .ResultRange.Address
                'Must delete the QueryTable to convert its result range to a table
                .Delete
            End With
          
            'Convert the result range to a table named "DataTable"
            .ListObjects.Add(xlSrcRange, .Range(qtResultAddress), , xlYes).Name = "DataTable"
           
        End With
  
    End If
       
End Sub
John_w, thanks for the help, I tried your solution and got the following when I ran it, this appeared after selecting the IQY file -

Run-time error '1004':
This cannot be used or changed while a table is using this external data range.

Any ideas?
 
Upvote 0
John_w, thanks for the help, I tried your solution and got the following when I ran it, this appeared after selecting the IQY file -

Run-time error '1004':
This cannot be used or changed while a table is using this external data range.

Any ideas?
Forgot to add, when I click on Debug it highlights this line - .CommandType = xlCmdList
 
Upvote 0
I thought it wouldn't work, but worth trying.

xlCmdList has the value 5, which is the same as your CommandType:=5. Try omitting .CommandType = xlCmdList.

Can you post the contents of the .iqy file? The code could read the file and somehow do the equivalent of the Workbooks.OpenDatabase line.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,951
Members
449,095
Latest member
nmaske

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