Data available for plotting vs already plotted data
"L Mehl" wrote ...
If I could use SQL in this app, the source for the listbox would look
something like:
SELECT name_Imported FROM tblImported
WHERE name_Imported NOT IN (SELECT name_Plotted FROM tblPlotted)
Example:
Imported Plotted
------------ ----------
series1 series1
series2 series4
series3
series4
series5
The listpox would contain the values:
series2
series3
series5
The more usual SQL construct would be to use an OUTER JOIN:
SELECT T2.name_Plotted
FROM [tblImported] T1
LEFT JOIN [tblPlotted] T2
ON T1.name_Imported = T2.name_Plotted
WHERE T2.name_Plotted IS NOT NULL
You can access the data in a workbook using ADO but due to the dreaded
memory leak bug, you can't query an open workbook. Happily, the
workaround is easy enough: save a copy of the relevant worksheet(s) to
a temporary (closed) workbook and query the temp workbook.
Here's one I made earlier (you'll need to save *two* worksheets to the
temp workbook):
Option Explicit
Sub Test()
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim Con As Object
Dim rs As Object
Dim strCon As String
Dim strPath As String
Dim strSql1 As String
' Amend the following constants to suit
Const FILENAME_XL_TEMP As String = "" & _
"delete_me.xls"
Const TABLE_NAME_CURRENT As String = "" & _
"XXX"
Const TABLE_NAME_NEW As String = "" & _
"MyNewTable"
' Do NOT amend the following constants
Const CONN_STRING_1 As String = "" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=<PATH<FILENAME;" & _
"Extended Properties='Excel 8.0;HDR=YES'"
' Build connection strings
strPath = ThisWorkbook.Path & _
Application.PathSeparator
strCon = CONN_STRING_1
strCon = Replace(strCon, _
"<PATH", strPath)
strCon = Replace(strCon, _
"<FILENAME", FILENAME_XL_TEMP)
' Build sql statement
strSql1 = ""
strSql1 = strSql1 & "SELECT Col1 FROM "
strSql1 = strSql1 & " [" & TABLE_NAME_CURRENT & "$]"
' strSql1 = strSql1 & " WHERE Co2=1 OR Col2=3"
' Delete old instance of temp workbook
On Error Resume Next
Kill strPath & FILENAME_XL_TEMP
On Error GoTo 0
' Save copy of worksheet to temp workbook
Set wb = Excel.Application.Workbooks.Add()
With wb
ThisWorkbook.Worksheets(TABLE_NAME_CURRENT). _
Copy .Worksheets(1)
.SaveAs strPath & FILENAME_XL_TEMP
.Close
End With
' Open connection to temp workbook
Set Con = CreateObject("ADODB.Connection")
With Con
.ConnectionString = strCon
.CursorLocation = 3
.Open
Set rs = .Execute(strSql1)
End With
Sheet1.ListBox1.List = _
Application.Transpose(rs.GetRows())
rs.Close
Con.Close
End Sub
--
|