Values not saved in a 2D array

mtenorio

New Member
Joined
Sep 5, 2014
Messages
18
Hi guys!

I have a text file that I want to extract information from. I'd like to get the quantity and the product name information and save it on a 2D array, here's my code:
Code:
Dim temp_current() As String
ReDim temp_current(1 To number_lines, 1 To 2) As String


Open FileName(a, c, b) For Input As #1               'opens the sales file for the current month
    row_number = 0
    number_lines = 0
    Do Until EOF(1)                                  'reads the text line per line until the end of file
        Line Input #1, LineFromFile
        quantity_current = Mid(LineFromFile, 237, 10) 'gets the quantity
        product_current = Mid(LineFromFile, 104, 80)  'gets the product name
        quantity_current_all = quantity_current_all & quantity_current & ";"  'stores the values of the quantity
        product_current_all = product_current_all & product_current & ";"     'stores the values of the produc names
        number_lines = number_lines + 1
        row_number = row_number + 1
   
    Loop
    Close #1
        
        
    '[B]Save values on 1 2D array[/B]
    For I = 1 To number_lines
        temp_current(I, 1) = CLng(Split(quantity_current_all, ";")(I))
        temp_current(I, 2) = CStr(Split(product_current_all, ";")(I))
        Debug.Print CLng(Split(quantity_current_all, ";")(I)), CStr(Split(product_current_all, ";")(I))
    Next I

Below are values displayed for the CLng(Split(quantity_current_all, ";")(I)) and CStr(Split(product_current_all, ";")(I)).
100000030 DISCOTRINE5mg/24h Disp tr B/30
18 HEMIGOXINE NATIV0,125mg Cpr B/30
12 PREVISCAN20mg Cpr B/30
8 DIGOXINE0,25mg-Cpr-B/30
9 PERMIXON160mg Gél B/60
7 SOLIAN200mg Cpr séc B/30
16 VOGALENE LYOC7,5mg Lyoph or B/16
24 AMLOR=AMLOPIDINE10mg Gél B/30
10 BETADINE TULLEPans B/10/10x10
20 DISCOTRINE10mg/24h Disp tr B/30
7 CHIBRO-PROSCAR5mg Cpr pell B/28
5 KARDEGIC160mg Pdr or 30sach
1 ELISOR20mg Cpr séc B/28


But when I try to see the values for temp_current(I, 1) and temp_current(I, 2), no values are displayed.
Do you guys have an idea why I was not able to save the values on the 2D array? I've used redim to declare the length of the array as variable..

Thanks a lot!
 
Last edited:

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.
Your ReDim occurs when number_lines is zero. Move it down after the first loop.


Changed the code to:

Code:
For I = 1 To number_lines
        R[B]eDim temp_current(1 To number_lines, 1 To 2) As String[/B]
        temp_current(I, 1) = CLng(Split(quantity_current_all, ";")(I))
        temp_current(I, 2) = CStr(Split(product_current_all, ";")(I))
        'Debug.Print CLng(Split(quantity_current_all, ";")(I)), CStr(Split(product_current_all, ";")(I))
        Debug.Print temp_current(I, 1), temp_current(I, 2)
    Next I

It worked! Thank you so much Andrew! :)
 
Upvote 0
I think the ReDim should be before that loop. You only need to do it once, when number_lines is at its maximum.

Like this one? Result is okay too.

Code:
[B]ReDim temp_current(1 To number_lines, 1 To 2) As String[/B]
    'Save values on 1 2D array
    For I = 1 To number_lines
        temp_current(I, 1) = CLng(Split(quantity_current_all, ";")(I))
        temp_current(I, 2) = CStr(Split(product_current_all, ";")(I))
        'Debug.Print CLng(Split(quantity_current_all, ";")(I)), CStr(Split(product_current_all, ";")(I))
        Debug.Print temp_current(I, 1), temp_current(I, 2)
    Next I
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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