Update a range within a macro using an input box or similar
VooDoo (or anyone with some experience with this)
After working for me for the longest time, I am now getting a Visual basic
Error when running the code detailed below.
ERROR:
Run-time error '-2147217904 (80040e10)'
[Microsoft][ODBC Excel Driver] Too few parameters. Expected 1.
Clicking on DEBUG brings me to the following line of code:
rst.Open sSQL, sConn
Are there any ideas on solutions? Thanks in advance.
/////ENTIRE CODE BEGINS HERE/////
Sub VooDoo()
Dim SourceFile$, SourceRange$, GetSet%
Dim rst As ADODB.Recordset, sConn$, sSQL$
Dim RandomNumberArray As Variant
Dim rng As Range
' requires a reference to the Microsoft ActiveX Data Objects library
' in VBA module, click Tools/References
'this is the source file name - read it from a cell or input box, stash it
in a custom doc property etc.
'google for getopenfilename if you want to make it dynamic
SourceFile = "C:\Documents and Settings\user2\My
Documents\RandomTool\random.xls"
'this is the range name - you could hard code, store or get form input box
SourceRange = "sets"
'this is the set # you want. i suggest you stash this in the workbook. I
like custom document properties
'add one BEFORE you run the code thru clicking file/properties/custom
With ActiveWorkbook.CustomDocumentProperties("setnumber ")
GetSet = .Value
..Value = .Value + 1 'increment the set by one
'good idea to put error handling here so you dont go over the # of sets in
your random.xls file
End With
'Create a new recordset object
Set rst = New ADODB.Recordset
'Connection details string
sConn = "DRIVER={Microsoft Excel Driver (*.xls)};" & "ReadOnly=1;DBQ=" &
SourceFile
'[TRIED]sConn = "DRIVER={Microsoft Excel Driver (*.xls)};" &
"ReadOnly=1;DBQ=" & "C:\Documents and Settings\user2\My
Documents\RandomTool\random.xls"
'[TRIED]sConn = "Provider=Microsoft.Jet.OLEDB.4.0; & "ReadOnly=1;DBQ=" &
SourceFile
' SQL statement to retrieve list of numbers
sSQL = "SELECT Set" & GetSet & " FROM " & SourceRange
' Open the recordset
rst.Open sSQL, sConn
'put the recordset into zero based array with all your random numbers in it
'to read array is RandomNumberArray(0,0), RandomNumberArray(0,1) etc.
RandomNumberArray = rst.GetRows
'Clean up
rst.Close
Set rst = Nothing
'NOW EVERYTHING YOU WERE DOING BEFORE IS BOILED DOWN TO 5 LINES!
Set rng = Sheet1.Rows(RandomNumberArray(0, 0))
For i = 1 To UBound(RandomNumberArray, 2)
Set rng = Application.Union(rng, Sheet1.Rows(RandomNumberArray(0, i)))
Next i
rng.Copy Sheet3.Cells(1, 1) 'copy paste
'clean up
Set rng = Nothing
End Sub
///////CODE ENDS HERE//////
|