Jump to content

VBA code will not work as VBScript


bilwebore

Recommended Posts

I am having a problem getting this VBA code to run as VBScript. In Excel, the following code works perfectly:Sub DynamicCollection()Dim sampleVisualBasicColl As New CollectionDim i, RanNum(5), x As LongFor i = 1 To 5RandomNumber = Int(100 * Rnd()) x = RandomNumber RanNum(i) = x 'The code below adds the first random number to the collection "sampleVisualbasicColl"sampleVisualBasicColl.Add RanNum(i), CStr("Pos" & i) '"Cstr("Pos" & i)" is the KEY 'for this collection object. RanNumber = sampleVisualBasicColl.Item(CStr("Pos" & i)) Next is = sampleVisualBasicColl.Counts1 = sampleVisualBasicColl("Pos1")s2 = sampleVisualBasicColl("Pos2")s3 = sampleVisualBasicColl("Pos3")s4 = sampleVisualBasicColl("Pos4")s5 = sampleVisualBasicColl("Pos5")'document.writeln.sampleVisualBasicColl.Item (CStr("Pos" & i)) RandomNumber = InputBox("Pick a Position from 1 to 5", "Random Number")MsgBox (sampleVisualBasicColl.Item(CStr("Pos" & RandomNumber)))Set sampleVisualBasicColl = NothingEnd SubWhen I write it into a VBScript code as follows, it fails to work. I can’t figure out why it will not run. Any help:<html><body><Pre><script language="vbscript">Sub DynamicCollection()Dim sampleVisualBasicColl As New CollectionDim i, RanNum(5), x As LongFor i = 1 To 5RandomNumber = Int(100 * Rnd()) x = RandomNumber RanNum(i) = x 'The code below adds the first random number to the collection "sampleVisualbasicColl"sampleVisualBasicColl.Add RanNum(i), CStr("Pos" & i) '"Cstr("Pos" & i)" is the KEY 'for this collection object. RanNumber = sampleVisualBasicColl.Item(CStr("Pos" & i)) Next is = sampleVisualBasicColl.Counts1 = sampleVisualBasicColl("Pos1")s2 = sampleVisualBasicColl("Pos2")s3 = sampleVisualBasicColl("Pos3")s4 = sampleVisualBasicColl("Pos4")s5 = sampleVisualBasicColl("Pos5")RandomNumber = InputBox("Pick a Position from 1 to 5", "Random Number")MsgBox (sampleVisualBasicColl.Item(CStr("Pos" & RandomNumber)))Set sampleVisualBasicColl = NothingEnd Sub </script><script language="vbscript">document.writeln.sampleVisualBasicColl.Item (CStr("Pos" & i))document.writeln(" " & "Collection Count =: " & cstr(sampleVisualBasicColl.count))document.writeln("Pos1")</script></Pre></body></html>

Link to comment
Share on other sites

I am having a problem getting this VBA code to run as VBScript. In Excel, the following code works perfectly [...]
If you want to become familiar with VBScript, the tutorial is probably a good place to start. You can learn there about syntax, Sub and Function procedures, the scope of variables, object creation and so forth in VBScript.Just out of interest, why do you want to use VBScript?
Link to comment
Share on other sites

If you want to become familiar with VBScript, the tutorial is probably a good place to start. You can learn there about syntax, Sub and Function procedures, the scope of variables, object creation and so forth in VBScript.Just out of interest, why do you want to use VBScript?
All the code development I have done has been with VBA for use in Excel files. VBA is the only code I have real experience with. I thought going from VBA to vbscript would be easy. But visual bascic does not work the same in vbscript. Can visual basic be used directly in web applications? What alternative code would you recommend?Bill
Link to comment
Share on other sites

All the code development I have done has been with VBA for use in Excel files. VBA is the only code I have real experience with. I thought going from VBA to vbscript would be easy. But visual bascic does not work the same in vbscript. Can visual basic be used directly in web applications? What alternative code would you recommend?Bill
It depends what you're trying to achieve, how much effort you want to invest in learning, what resources you have, etc etc. But if you want to build on your VB experience and use VB in web applications, have you looked at ASP.NET?
Link to comment
Share on other sites

If you ant to develop in ASP.NET ( as I recently learnt) get the msdn 2005 Express Edition WEb Developer.http://msdn2.microsoft.com/en-us/express/aa975050.aspxYou will also want the VB.NET package.Good Luck!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...