query another excel file?

bdee1

Board Regular
Joined
Feb 17, 2003
Messages
105
i am working on an excel project which needs to import data from another excel file.

is there any way that i can use VBA from my original sheet to import the records from another excel file where a specific feild is within a specified date range?

:rolleyes:
 
ok - at long last - i think we have it - woo hoo! thank you everyone for all your help!!!


for reference here is the code i used:

for the import from a closed workbook:

Code:
Private Sub CommandButton1_Click()
  
  UserForm4.Hide
  Dim cn As Object
  Set cn = CreateObject("ADODB.Connection")
  cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & UserForm4.TextBox1.Value & ";Extended Properties=Excel 8.0;"
  cn.Execute "Insert Into [SV_Download$] In '' [Excel 8.0;Database=" & ThisWorkbook.FullName & ";HDR=YES] Select " & _
    "PATIENT_NUM, CLASSN, ADMIT_DATE, DISCH_DATE, Last_Name, First_Name, CMG, PAYTS From [A$a4:h65536] Where DISCH_DATE >" & UserForm4.DTPicker1.Value

  cn.Close: Set cn = Nothing

END Sub

and here is the code i used to clear the sheet:

Code:
'Delete the SV_Download Sheet
    Sheets("SV_Download").Select
    ActiveWindow.SelectedSheets.Delete
    
    
    'Recreate Sheet
    Sheets("Erehab_NoMatch").Select
    Sheets.Add
    ActiveSheet.Name = "SV_Download"
    Sheets("SV_Download").Select
    ActiveWorkbook.Sheets("SV_Download").Tab.ColorIndex = 41
    
    'Set the headers for the sheet
    Sheets("SV_Download").Range("A1").Value = "PATIENT_NUM"
    Sheets("SV_Download").Range("B1").Value = "CLASSN"
    Sheets("SV_Download").Range("C1").Value = "ADMIT_DATE"
    Sheets("SV_Download").Range("D1").Value = "DISCH_DATE"
    Sheets("SV_Download").Range("E1").Value = "LAST_NAME"
    Sheets("SV_Download").Range("F1").Value = "FIRST_NAME"
    Sheets("SV_Download").Range("G1").Value = "CMG"
    Sheets("SV_Download").Range("H1").Value = "PAYTs"
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You are welcome. :)

I might tweak the 2nd procedure to look like:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> BlahBlahBlah()
<SPAN style="color:darkblue">Dim</SPAN> y <SPAN style="color:darkblue">As</SPAN> Worksheet
<SPAN style="color:darkblue">With</SPAN> Application
    .ScreenUpdating = <SPAN style="color:darkblue">False</SPAN>
    .DisplayAlerts = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">On</SPAN> <SPAN style="color:darkblue">Error</SPAN> <SPAN style="color:darkblue">Resume</SPAN> <SPAN style="color:darkblue">Next</SPAN>
Sheets("SV_Download").Delete
<SPAN style="color:darkblue">On</SPAN> <SPAN style="color:darkblue">Error</SPAN> <SPAN style="color:darkblue">GoTo</SPAN> 0
<SPAN style="color:darkblue">Set</SPAN> y = Sheets.Add(Sheets(1))
<SPAN style="color:darkblue">With</SPAN> y
   .Name = "SV_Download"
   .Tab.ColorIndex = 41
   .[a1:h1] = Array("PATIENT_NUM", "CLASSN", "ADMIT_DATE", "DISCH_DATE", _
       "LAST_NAME", "FIRST_NAME", "CMG", "PAYTs")
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">Set</SPAN> y = <SPAN style="color:darkblue">Nothing</SPAN>
<SPAN style="color:darkblue">With</SPAN> Application
    .ScreenUpdating = <SPAN style="color:darkblue">True</SPAN>
    .DisplayAlerts = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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