Option Compare Database
Function fImportPhoenix() As String
On Error GoTo Err_fImportPhoenix
[COLOR="Red"][B] Dim i As Long
Dim Msg As String[/B][/COLOR]
Dim fld As Field
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
' ====== OPEN WORKBOOK AND SHEET ================================================ '
' Notes: WORKING
Dim strDialogTitle As String
Dim strPath As String
strDialogTitle = "Select a file for import"
strPath = GetOpenFile_CLT(".\", strDialogTitle) ' just a generic file dialog
'If no file was selected then the PathStrg variable will be empty.
'If there was a file selected then.....
[COLOR="red"][B] Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim Range As Object[/B][/COLOR]
If strPath <> "" Then
[COLOR="red"][B]
Set ApXL = CreateObject("Excel.Application") 'invoke excel application
Set xlWBk = ApXL.Workbooks.Open(strPath) 'open the selected file
Set xlWSh = xlWBk.Worksheets("Sheet1")[/B][/COLOR]
ApXL.Visible = True ' don't show the file to the user
' ====== ADD NEW WORKSHEETS ===================================================== '
' Notes: DEVELOPING, but all WORKING unless otherwise stated
Dim xlWShExtract As Object
Dim xlWShMarkers As Object
Dim xlWShExRules As Object
' check if additional worksheets exists, if not create them
On Error Resume Next
Set xlWShExtract = xlWBk.Sheets("Extract")
Set xlWShMarkers = xlWBk.Sheets("Markers") ' this one yet to code
Set xlWShExRules = xlWBk.Sheets("ExRules") ' this one yet to code
On Error GoTo Err_fImportPhoenix
' ------ "EXTRACT" Results Extraction Worksheet ------------------------- '
' this worksheet will be used for importing into a temporary Access table
If Not xlWShExtract Is Nothing Then
'sheet has already been generated
'(and thus imported, once i finish the code!, so exit for this file)
'maybe check that the record exists...?
'and/or ask if user wants to re-import? overwrite? etc...
'End Function
Else
'does not exist, so add it
ApXL.Worksheets.Add.Move After:=ApXL.Worksheets(ApXL.Worksheets.Count)
ApXL.Sheets(ApXL.Worksheets.Count).Name = "Extract"
Set xlWShExtract = xlWBk.Sheets("Extract")
'and add appropariate headers
With xlWShExtract
.Range("A1") = "Antimicrobial"
.Range("B1") = "Qualifier"
.Range("C1") = "Value"
.Range("D1") = "SIR"
.Range("E1") = "Reading"
End With
'copy over information from Sheet1.
With xlWSh
.Activate
For i = 1 To .UsedRange.Rows.Count
If .Cells(i, 1) = "Isolate AST Results" Then
.Cells(i + 2, 1).Activate
'' NUT OUT FROM HERE
'' -----------------
'' Error 424 on next line - Object Required
.Range(i + 2, 1).CurrentRegion
End If
Next
End With
' With xlWShExtract
' .Activate
' .ActiveSheet.Paste
' .Sheets(1).Select
' .Range(Selection + 2, Selection.End(xlDown)).Copy
' .Sheets("Extract").Activate
' .Sheets("Extract").Paste
' End With
'' -----------------
'' TO HERE
'add formulae to get the desired data
With xlWShExtract
' -------- for qualifier
' Must use RC[3] instead of "E2" for range source in formula (relative reference)
.Range("B2").FormulaR1C1 = "=IF(NOT(ISERROR(FIND(""="",RC[3]))),LEFT(RC[3],2),IF(OR(LEFT(RC[3],1)="">"",LEFT(RC[3],1)=""<""),LEFT(RC[3],1),""=""))"
'now copy down the same number of rows as exists in column A
' (yet to do)
' -------- for value
' chagned B2 to RC[-1] and E2 to RC[2] (relative reference)
.Range("C2").FormulaR1C1 = "=IF(RC[-1]<>""="",RIGHT(RC[2],LEN(RC[2])-LEN(RC[-1])),RC[2])"
'now copy down the same number of rows as exists in column A
' (yet to do)
End With
GoTo Clean_up
End If
' ------ "MARKERS" Marker Rules Worksheet ------------------------------- '
' this worksheet will be used for importing into a temporary Access table
' copy and adpat from 'EXTRACT' once it's working
' have to try to 'skip' the blank rows... maybe a loop to find cells containing 'rule'?
' could be tricky... :-/
' ------ "RULES" Expert Triggered Rules Worksheet ----------------------- '
' this worksheet will be used for importing into a temporary Access table
' copy and adpat from 'MARKERS' once it's working
' ====== FIND PRELIMINARY DATA ================================================== '
' Notes: WORKING
Dim strSample As String
Dim strOrganism As String
Dim strTestName As String
[COLOR="Red"][B] ' find the sample name and copy to string
With xlWSh
.Activate
For i = 1 To .UsedRange.Rows.Count
If .Cells(i, 1) = "Accession #:" Then
.Cells(i, 2).Activate
strSample = .Cells(i, 2).Value
End If
Next
End With
[/B][/COLOR]
' find the organism name and copy to string
With xlWSh
For i = 1 To .UsedRange.Rows.Count
If .Cells(i, 1) = "Organism Name:" Then
.Cells(i, 2).Activate
strOrganism = .Cells(i, 2).Value
End If
Next
End With
' find the test name and copy to string
With xlWSh
For i = 1 To .UsedRange.Rows.Count
If .Cells(i, 1) = "Test Name:" Then
.Cells(i, 2).Activate
strTestName = .Cells(i, 2).Value
End If
Next
End With
'copy all three strings to temporary table
' (yet to do) = INSERT statement?
' ====== CLEAN UP =============================================================== '
' Notes: WORKING
Clean_up:
' delete sheet while testing (code works!)
ApXL.DisplayAlerts = False
xlWBk.Sheets("Extract").Delete
'xlWBk.Sheets("Markers").Delete
'xlWBk.Sheets("ExRules").Delete
ApXL.DisplayAlerts = True
[COLOR="Red"][B] ' close instance of Excel
xlWBk.Save
xlWBk.Close
ApXL.Quit
Set xlWShExtract = Nothing
Set xlWSh = Nothing
Set xlWBk = Nothing
Set ApXL = Nothing[/B][/COLOR]
MsgBox "I've left everything neat and tidy for you"
End If
Exit_fImportPhoenix:
Exit Function
' ====== ERROR HANDLER ========================================================== '
Err_fImportPhoenix:
' delete sheet while testing (code works!)
ApXL.DisplayAlerts = False
xlWBk.Sheets("Extract").Delete
'xlWBk.Sheets("Markers").Delete
'xlWBk.Sheets("ExRules").Delete
ApXL.DisplayAlerts = True
' close instance of Excel
xlWBk.Save
xlWBk.Close
ApXL.Quit
Set xlWShExtract = Nothing
Set xlWSh = Nothing
Set xlWBk = Nothing
Set ApXL = Nothing
' "modFeatures" class module required for strings presented below
Msg = "Error # " & Str(Err.Number) & Chr(13) & " (" & Err.Description & ")"
Msg = Msg & Chr(13) & "in modImportPhoenix | fImportPhoenix"
MsgBox Msg, vbOKOnly, fstrDBname & ": Error", Err.HelpFile, Err.HelpContext
Resume Exit_fImportPhoenix
End Function