vba only works on one file, one computer - import of text file data

Sayre

Board Regular
Joined
Apr 12, 2005
Messages
180
I can't wrap my head around this. I have 2 slightly different versions of a macro: GetDataArray and GetDataArray2.

One works fine. The other does not, behaves oddly, hoping someone can help. In both cases the code is importing data from text files to excel using keywords, using binary translation etc… its all a little over my head so I’m hoping there’s an obvious reason that I just don’t understand.

Both macros are in the same workbook running excel 2010. Both of them refer to text files saved in the same folder/directory, I'm just trying to do same thing with 2 different text files.

I think it has something to do with this line:
Code:
Line = Split(Text, vbCrLf) 'this line gives me subscript out of range error
because something different happens if I change to
Code:
Line = Split(Text, vbLf) 'If I switch to this version, no error but it does not find the keyword in the text even though it is there.
or to
Code:
Line = Split(Text) ' same thing here. Runs through all lines without finding the keyword.
. But in any case its not transferring the text from the .txt to the .xlsm


It looks to me like the keyword the macro is searching for is clearly there in both instances. Perhaps there's something different about the 2 text files?
I’ve attached them both here in hopes that someone will see something I am not seeing.

Oh, and to make it worse, the macro that does work (GetDataArray) only works on my work PC. Neither GetDataArray or GetDataArray2 works on my home laptop saving to local disk) or through citrix remote connect to my work (saving to shared network drive). I’m on excel 2010 on all 3 platforms. Any ideas on that one would be helpful but if I can get it to work on one place that's probably good enough for now.

Any direction or help is appreciated!

Here's the code that works.
Code:
Option Compare Text
 '''''''''''''''''''''''''this code works on text file 326647_ThisFileWorks.txt''''''''''''''''''''''''''''''''''''''''''''''
Sub GetDataArray()
    Dim WS As Worksheet, NextRow As Long
    Dim Data() As Byte
    Dim FolderPath As Variant, Lines As Variant, oFile As Variant
    Dim oFiles As Object, oFolder As Object, oShell As Object
    Dim Text As String
    Dim Temp As Variant
    Dim Symbols As String
    Dim Param As Range
    Dim SearchParam As Variant
    Dim A As Long

    'Symbols = "Si28,Si29"
   'SearchParam = Split(Symbols, ".")

    Set WS = Worksheets("Sheet1")

    Set oShell = CreateObject("Shell.Application")

    ' Add fixed path to the folder.
    FolderPath = "\\dfs\fs\users\asayre\ASG internal\A1 - new macros"

    ' Let User choose the Folder.
    'Set oFolder = oShell.BrowseForFolder(0&, "Please Select the Folder with the Note Files.", 17)
    'If oFolder Is Nothing Then Exit Sub Else FolderPath = oFolder.Self.Path

    Set oFolder = oShell.Namespace(FolderPath)

    Set oFiles = oFolder.Items

    oFiles.Filter 64, "326647_ThisFileWorks.txt"
    'oFiles.Filter 64, "*.csv;*.txt"

    For Each oFile In oFiles
        Open FolderPath & "\" & oFile For Binary Access Read As #1
        ReDim Data(LOF(1))
        Get #1, , Data
        Close #1

        Text = StrConv(Data, vbUnicode)

        With WS
            NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            If NextRow < 3 Then NextRow = 3
        End With

        Line = Split(Text, vbCrLf)
        Do
            A = A + 1
            If InStr(Line(A), "Time [sec]") > 0 Then
                A = A + 2
                Do
                    Temp = Split(Line(A), vbTab)
                    If Temp(0) = "" Then Exit Do
                    WS.Range("A" & NextRow) = Temp(0)
                    WS.Range("B" & NextRow) = oFile
                    WS.Range("C" & NextRow) = Temp(7)
                    WS.Range("D" & NextRow) = Temp(8)
                    A = A + 1
                    NextRow = NextRow + 1
                Loop Until A = UBound(Line)
            End If
        Loop Until A = UBound(Line)
    Next oFile
End Sub




And here is the version that does not work:
Code:
Option Compare Text
 ''''''''''''''''''''''''''''''this code does not work on text file zfs_ThisFile_does_not_work.txt'''''''''''''''''''''''
Sub GetDataArray2()
    Dim WS As Worksheet, NextRow As Long
    Dim Data() As Byte
    Dim FolderPath As Variant, Lines As Variant, oFile As Variant
    Dim oFiles As Object, oFolder As Object, oShell As Object
    Dim Text As String
    Dim Temp As Variant
    Dim Symbols As String
    Dim Param As Range
    Dim SearchParam As Variant
    Dim A As Long

    Symbols = "Si28,Si29"
    SearchParam = Split(Symbols, ".")

    Set WS = Worksheets("Sheet1")

    Set oShell = CreateObject("Shell.Application")

    ' Add fixed path to the folder.
    FolderPath = "\\dfs\fs\users\asayre\ASG internal\A1 - new macros"

    ' Let User choose the Folder.
    'Set oFolder = oShell.BrowseForFolder(0&, "Please Select the Folder with the Note Files.", 17)
    'If oFolder Is Nothing Then Exit Sub Else FolderPath = oFolder.Self.Path

    Set oFolder = oShell.Namespace(FolderPath)

    Set oFiles = oFolder.Items

    oFiles.Filter 64, "zfs_ThisFile_does_not_work.txt"
    'oFiles.Filter 64, "zfs_ThisFile_does_not_work.txt"
    'oFiles.Filter 64, "*.csv;*.txt"

    For Each oFile In oFiles
        Open FolderPath & "\" & oFile For Binary Access Read As #1
        ReDim Data(LOF(1))
        Get #1, , Data
        Close #1

        Text = StrConv(Data, vbUnicode)

        With WS
            NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            If NextRow < 3 Then NextRow = 3
        End With

        Line = Split(Text, vbCrLf) 'this line gives me subscript out of range error
        'Line = Split(Text, vbLf) 'If I switch to this version, no error but it does not find the keyword in the text even though it is there.
        'Line = Split(Text) ' same thing here. Runs through all lines without finding the keyword.

        Do
            A = A + 1
            If InStr(Line(A), "NAME") > 0 Then
                A = A + 2
                Do
                    Temp = Split(Line(A), vbTab)
                    If Temp(0) = "" Then Exit Do
                    WS.Range("A" & NextRow) = Temp(0)
                    WS.Range("B" & NextRow) = oFile
                    WS.Range("C" & NextRow) = Temp(7)
                    WS.Range("D" & NextRow) = Temp(8)
                    A = A + 1
                    NextRow = NextRow + 1
                Loop Until A = UBound(Line)
            End If
        Loop Until A = UBound(Line)
    Next oFile
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
OK looks like I can't add attachments, I'll add the text for each .txt file here:

contents of 326647_ThisFileWorks.txt:
Code:
Acquisition Parameters									
									
Data File :		03_1ppm_play.txt							
Error :									
Analysis Date :									
Sample Name :									
Tune Parameters :								
Method File :									
Runs/Passes (Meas.) :		0 * 0 + 10 * 1 + 0 * 0							
Res. Switch Delay [s] :		2							
Washtime [min] :		0.0							
Take-up Time [min] :		0.0							
Deadtime [ns] :		2							
Evaluation Parameters									
									
Evaluation Date :									
User :									
Analysis Type :		SMP							
Standard File :									
Int. Standard File :									
Blank File :									
Runs/Passes (Eval.) :		0 * 0 + 10 * 1 + 0 * 0							
Quantification Type :		Intensities							
Calibration File :									
Response File :									
Dilution Factor :									
Sample Amount :									
Spike Amount :									
Final Volume :									
Int. Std. active :		No							
IS before BS :	Li6(MR)	Li7(MR)	Mg24(MR)	Mg25(MR)	Mg26(MR)	Al27(MR)	Si28(MR)	Si29(MR)	
									
	Intensity AVG	Intensity AVG	Intensity AVG	Intensity AVG	Intensity AVG	Intensity AVG	Intensity AVG	Intensity AVG	
	[cps]	[cps]	[cps]	[cps]	[cps]	[cps]	[cps]	[cps]	
									
Average	1094.6	16914.5	26313.6	3550.8	4173.9	129178.6	81635106.1	4310595.1	
Std.Dev.	178.8	1501.7	738.7	259.0	270.0	2336.9	3944082.0	57479.9	
RSD [%]	16.33	8.88	2.81	7.29	6.47	1.81	4.83	1.33	
									
Time [sec]									
									
20.7	1151.0	17204.8	26352.5	3528.3	4466.7	133345.0	77873489.5	4370659.9	
24.1	1151.0	16691.8	27503.7	3916.2	4304.0	126296.6	77374470.7	4387111.2	
27.5	1013.3	20057.8	26227.3	4028.8	3628.4	129526.2	73864982.9	4276272.3	
31.0	975.8	16591.6	26640.3	3540.8	3928.7	127223.3	83330316.9	4332128.5	
34.4	1288.5	18306.0	25764.2	3490.7	4241.5	127185.9	83853834.0	4289792.1	
37.9	1038.3	16228.8	24838.3	3365.6	4379.1	131567.4	85625819.4	4232303.1	
41.3	700.5	14764.7	26903.0	3115.4	4091.3	130841.3	85955638.4	4359829.6	
44.8	1150.9	16704.3	26677.8	3515.7	4491.8	128976.0	83176113.1	4358104.3	
48.2	1338.5	17380.0	25701.7	3565.8	3978.7	126697.1	83152061.3	4247249.8	
51.7	1138.5	15215.2	26527.7	3440.7	4229.0	130127.6	82144334.7	4252500.0	
									
									
									
Errorcodes :   S=Amplifier Skipped    D=Intensity Defocussed   O=Overflow

contents of zfs_ThisFile_does_not_work.txt:
Code:
NAME                         USED  AVAIL  REFER  MOUNTPOINT
prd_pool                     122G   152G  37.3G  /prd_pool
prd_pool@v2v                58.5G      -  71.7G  -
prd_pool/oracle             26.0G   152G  26.0G  /prd_pool/prod_zones/couldbe/root/oracle
rpool                        119G   155G  62.7G  /rpool
rpool/ROOT                  15.3G   155G    31K  legacy
rpool/ROOT/s10s_u10wos_17b  15.3G   155G  15.3G  /
rpool/dump                  1.50G   155G  1.50G  -
rpool/export                  63K   155G    32K  /export
rpool/export/home             31K   155G    31K  /export/home
rpool/oracle                31.1G   155G  31.1G  /rpool/zones/soso/root/oracle
rpool/swap                  2.06G   155G  2.00G  -
rpool/swap2                 6.19G   155G  6.00G  -
 
Upvote 0
I got it to work on the other text doc by switching this:
Code:
Line = Split(Text, vbCrLf)
to this:
Code:
Line = Split(Text, vbLf)
and then adding in a function I found here: Split Function That Ignores Delimiters Located Inside Quote Marks. Its form Rick Rothstein whom I see in my thread searches quite a bit ;)

To treat consecutive delimiters as one. Its really great for importing arrays from space-separated text files that have inconsistent spacing.

My only problem now is that I still can't get this to work anywhere besides my office PC.

Observations:
This line:
Code:
If NextRow < 3 Then NextRow = 3
does not move onto the 'Then' part of the statement when I'm working on this remotely on citrix server. But does so just fine in the office. Perplexing.

The value of Text in this line:
Code:
Text = StrConv(Data, vbUnicode)
shows " " after execution when on citrix. At my office PC it contains the data in the text file at this point.

Both the citrix instance and my office PC are on excel 2010.

Any tips or threads that address this specifically would be greatly appreciated that's what I'll be searching on now.

It's been really fun doing this kind of project again and this is a great resource.
 
Upvote 0
Wow there really does not seem to be much out there on this. Although I continue to try. I did notice that the version that works is on windows 7 enterprise version whereas the 2 places where the macro fails are on 1) Windows 2008 R2 (citrix) and 2) Windows 8 (my laptop). But in all cases its running excel 2010. Same library references selected.

Anyone heard of issues like this? I'd really like to be able to work on this code somewhere else besides at the office ;)

Once again any help appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,836
Messages
6,127,180
Members
449,368
Latest member
JayHo

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