INSTR() not returning the correct number of occurrences.

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
439
Office Version
  1. 2016
Platform
  1. Windows
I'm attempting to import comma delimited data into an array but need to replace the 187th element of each line if the total number of commas in the string is equal to 192. The problem being that I can't determine the number of commas in the string.

Sample .txt file : https://www.dropbox.com/s/12oi5njdexstt74/Sample.txt?dl=0

When I use the code below and the sample file above, N_OC returns 37 when there are a total of 192 commas in the string.
Code:
    MyData = Input(LOF(fileNumber), [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNumber]#fileNumber[/URL] )


    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNumber]#fileNumber[/URL] 
    
    strData = Split(MyData, vbCrLf) 
    
For Each Item In strData    
        N_OC = InStr(1, Item, ",")
    
 Next Item

However, when I further decide to delimit the string into an array using the following code, the correct number of elements are created.

Code:
Str_Array = Split(strData(i), ",")

Thanks in advance
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I haven't looked at your text file, but you can count the number of commas as follows...

Code:
N_OC = Len(Item) - Len(Replace(Item, ",", ""))
 
Upvote 0
Thank you, that worked perfectly.
Just so you know for future coding, the InStr function returns the location of the item searched for, not the count of that item. So for this line of code...

N_OC = InStr(1, Item, ",")

the 37 you reported that it assigned to N_OC meant the first comma it came to starting at the 1st character in the text (which is what the 1 in the first argument told it) occurred at character position 37 within the text string.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top