• 0

[vb] Split function and Arrays


Question

hey, I know most of you are Web Coders but Im wondering if anyone out there has some knowledge of vb...

anyway, Im trying to split up a string and plonk it in an array, of course, VB does this with the Split function, the only problem is that it returns an error "Cant assign to array" when I pre-define the array (Dim arry(1) As String). If the Array is assigned as a dynamic array (Dim arry() As String) then it all works fine, but I get a "Subscript out of range" error message when I check to see if arry(1) has anything in it or not.

So far the only way Ive figured to get around it is to use an "On error goto" statement, though I think thats a little shoddy and theres gotta be some way to get the Split function liking fixed arrays...

just for reference, my code looks like so...

Option Explicit
Dim p00p As String
Dim arry(1) As String


Private Sub cmdZoomIn_Click()
'Erase arry

p00p = ActiveWindow.RangeSelection.Address
arry = Split(p00p, ":", 2)
'On Error GoTo zoominerror:
If arry(1) <> "" Then
    MsgBox arry(0)
    MsgBox arry(1)
End If

'zoomcont:

'Exit Sub
'zoominerror:
'    MsgBox "You need to select another number!"
'Resume zoomcont:
End Sub

as you can see, Ive commented out my error goto code and the "erase arry" statement (cause theyre both unnecesary).. any ideas?

Link to comment
https://www.neowin.net/forum/topic/86958-vb-split-function-and-arrays/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

im not entirely sure what u are trying to do here. Are you trying to get a string and split it into to parts. If so well surely the array would need 2 elements instead of 1. As for the cannot assign to array error you would have to put one section of your sting into one variable and then the other part of ur string into another variable and then put them into the array.

  • 0

yes, Im trying to split a string into parts, its actually for a VBA project (macros) in Excel where I get the two Cell addresses that are selected, if only one is selected it will only return one string so only one array value will be filled.

The reason its declared as 1 is because all Arrays start at 0, so I have 0 and 1 in the Array which is two elements, though even if I declare it with 100 I still get the "Cannot assign to Array" message.

Segosa, what would it be looking for? Im not sure whether I should use the On Error Resume Next's because we havent really been taught them yet in the class so I dont know if they will like us using them or not. :wacko:

  • 0

Try not dimensioning the array size in the declaration. Just do:

dim arry() as string

arry = Split(p00p, ":", 2)

VB will handle the sizing. You can check the size with ubound(arry) and and empty array (when used with a split like this) will return a ubound of -1 (If I remember correctly).

  • 0
  Novex said:
Im not sure whether I should use the On Error Resume Next's because we havent really been taught them yet in the class so I dont know if they will like us using them or not. :wacko:

Well it are the most useful thing in VB, almost any error you have, can be got rid of with On Error Resume Next. All it does is go to the next line when an error occurs... but this doesnt really suit your program, i dont know wtf is wrong with the array, i mean, just try what that other person said.

  • 0
  ixsis said:
Try not dimensioning the array size in the declaration. Just do:

dim arry() as string

arry = Split(p00p, ":", 2)

VB will handle the sizing. You can check the size with ubound(arry) and and empty array (when used with a split like this) will return a ubound of -1 (If I remember correctly).

yeah, I did that, the split function worked, but then it keeled over in the if statement when it was checking if something was in key number one, if key 1 hasnt been created then it spits out an error too..

I tried pre-defining the array by setting 0 and 1 to "", but then the split statement buggered up again so Im just going to ask my Tutorial guy next Workshop about it :D

On Error Goto and all that seem to be a very good resource to use, so I figure why not use them

Thanks for the help :D

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I always turn encryption off 1st boot, crazy its on by default on new computers, it should ASK you ON or OFF on 1st boot,, So many people dont even know its on , then forget their windows login and microsoft account,, RETarDED Microsoft is now, , i also find having it on slows things down too
    • Adding AI is just an excuse to hike prices. I don't want any AI features in our Slack workspace and yet will have to pay for it.
    • Hello Could you be kind too help me fix this 3.1gig and not 256gig it says 3.1gig only .someone told me to fix it with this https://www.disk-partition.com/articles/64gb-flash-drive-only-showing-32gb-5740i.html yes my sandisk ultra usb 3.0 256gb wont show up in full .  but i don't know want options i have to use on there can someone  please guide me with step by step instructions please  guide me with step by step instructions on how to repair this .sorry for asking but I've tried but i don't know want i have to do on that programme please help me i am on windows 11 home thank you nick
    • AI is as big a scam as FTX was...run by the same 'group' of people as well.
    • Slack Business+ plan jumps to $15/month as AI features launch by Paul Hill Salesforce has announced a price increase for one of its plans, which will add a few dollars per user each month to organizations' bills. The Slack Business+ plan will see prices rise from $12.50 to $15 per user per month if you pay for a whole year up front; if you pay monthly, the bill will increase from $15 to $18. Salesforce has kept the prices of its Pro plan the same and introduced a new Enterprise+ plan with custom prices for larger organizations. The new price adjustments will take effect on August 17 if you’re planning to renew, and immediately for any new customers. So, if you are renewing before August 17, you will be able to delay this price rise for around a year. The company isn’t just pulling a fast one on customers with these price rises. It’s adding AI to all paid plans, so if you use these features, the increase somewhat justifies them. We saw a similar tactic from Google Workspace back in January. New AI features and deeper Salesforce integration Slack has outlined all the new AI features coming to its Pro, Business+, and Enterprise+ plans starting from July 17. The Pro Plan will have access to conversation and thread summaries, plus huddle notes, while Business+ will have all of those plus: AI Search, Recaps, Translations, File summaries, Workflow generation, and Summarize channels workflow step. The Enterprise+ subscription has all those AI features plus Enterprise search. Additionally, it provides enhanced security, admin controls, governance, and compliance. The company also announced that all plans, including the free plan, will get access to Salesforce Channels, which lets you use Slack and Salesforce together directly within their Customer 360 Apps. Who benefits? By embedding AI right within Slack, organizations should be able to boost efficiency more easily in their daily operations. To ensure that it keeps up with the competition, the company is investing significant resources into its research and development to deliver new innovations. Affected organizations will need to try to find effective ways to use these AI tools to make the price increase worth it, whether this is speeding up tasks to free workers up for other work or something else. The move definitely doesn’t hurt Slack because it’ll make more money whether you use these AI tools or not.
  • Recent Achievements

    • Week One Done
      vivetool earned a badge
      Week One Done
    • Reacting Well
      pnajbar earned a badge
      Reacting Well
    • Week One Done
      TBithoney earned a badge
      Week One Done
    • First Post
      xuxlix earned a badge
      First Post
    • First Post
      Tomek Święcicki earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      673
    2. 2
      ATLien_0
      288
    3. 3
      Michael Scrip
      223
    4. 4
      +FloatingFatMan
      195
    5. 5
      Steven P.
      144
  • Tell a friend

    Love Neowin? Tell a friend!