LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default 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//////
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop Down--How do I copy and update cell link/input range MaryinCT Excel Discussion (Misc queries) 1 November 2nd 07 01:13 AM
Combo Box input range automatic update John M Excel Discussion (Misc queries) 1 May 11th 06 08:05 PM
Macro to update pivot table data range Mike_M Excel Programming 2 May 25th 05 04:02 PM
Macro to change list box input range based on selection made in another cell Sue[_6_] Excel Programming 3 October 7th 04 06:45 PM
Macro to input formula in range based on another range Peter Atherton Excel Programming 0 October 9th 03 12:47 AM


All times are GMT +1. The time now is 04:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"