Compile error from Office 2010 to Office 365: Variable not defined

mrsun

New Member
Joined
Apr 20, 2022
Messages
4
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello,

I have to migrate a very old excel file with many macros from Office 2010 to Office 365 (Office 2010 being no longer supported). The Excel file works perfectly on Office 2010.

When I run the VBA compiler, I get the following error message: Compile error Variable not defined

Here is the code: The issue highlighted by the VBA editor is:
VBA Code:
 If strRec = SP_KUGIRI Then

How can the code be amended to be supported by O365?

VBA Code:
Sub GetStorePlanFile(ByVal strFilePath As String, ByRef arrDataHeader As Variant, ByRef arrDataDetail As Variant)

    Dim fso As Object
    Dim ffile As Object
    Dim csvDataLine As Variant
    Dim lngHeaderCntC As Long
    Dim lngDetailCntC As Long
    Dim i As Long, j As Long
    Dim bDetailFlg As Boolean
    Dim intFree As Long
    Dim strRec As String
    
    Dim colHeader As Collection
    Dim colDetail As Collection
    
    Set colHeader = New Collection
    Set colDetail = New Collection
    
    bDetailFlg = False
    
    intFree = FreeFile
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Open strFilePath For Input As #intFree

    Do Until EOF(intFree)
        Line Input #intFree, strRec
        
      
        If strRec = SP_KUGIRI Then
            bDetailFlg = True
            i = 0
        Else
            If bDetailFlg Then
                colDetail.Add (strRec)
            Else
                colHeader.Add (strRec)
            End If
        End If
    Loop
    
    Close #intFree
    
    Dim oneLineH As Variant
    Dim oneLineD As Variant
    
    oneLineH = Split(colHeader.Item(1), ",")
    oneLineD = Split(colDetail.Item(1), ",")
    
 
    lngHeaderCntC = UBound(oneLineH) + 1
    lngDetailCntC = UBound(oneLineD) + 1
    
    ReDim arrDataHeader(colHeader.Count, lngHeaderCntC)
    ReDim arrDataDetail(colDetail.Count, lngDetailCntC)
    
    Dim ch As Variant
    Dim cd As Variant
    
    j = 1
    For Each ch In colHeader
        csvDataLine = Split(ch, ",")
        For i = 1 To lngHeaderCntC
            arrDataHeader(j, i - 1) = csvDataLine(i - 1)
        Next
        j = j + 1
    Next
    
    j = 1
    For Each cd In colDetail
        csvDataLine = Split(cd, ",")
        For i = 1 To lngDetailCntC
            arrDataDetail(j, i - 1) = csvDataLine(i - 1)
        Next
        j = j + 1
    Next
    
    Set colHeader = Nothing
    Set colDetail = Nothing
    
End Sub

Thank you!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The problem does not have anything to do with Excel 2010, because when I test your code using that version I get the same error. It is more likely that SP_KUGIRI is a global/public variable defined at the module level and was not carried over when you migrated your code.
 
Upvote 0
The problem does not have anything to do with Excel 2010, because when I test your code using that version I get the same error. It is more likely that SP_KUGIRI is a global/public variable defined at the module level and was not carried over when you migrated your code.
Thank you for checking.

I am using exactly the same package as the one running with Office 2010. the only difference is that one system uses 32 bits and the other one 64 bits. I managed to solve some issues by adding PtrSafe so I am wondering it might be the case also here.
 
Upvote 0
How did you "migrate" the code ?
Did you by any chance not have the Option Explicit line in 2010 and do have in it MS365 ?
Does your text file contain the text "SP_KUGIRI" ? in which case the word should be in quotes in your if statement ie

VBA Code:
If strRec = "SP_KUGIRI" Then
 
Upvote 0
Did you check to see if you have any missing libraries/references?
This commonly happens when running the same code on different systems.

If you go to the system where it is working, go to the VB Editor, and under the Tools menu select References, and note all the selected libraries.
Now do the same on your new system, and see if there are any references that were selected from your old system that are missing from this new one.
If there are, find them in the list and select them (note that the version number may be slightly different, but the rest of text verbiage should be the same).
 
Upvote 0
How did you "migrate" the code ?
Did you by any chance not have the Option Explicit line in 2010 and do have in it MS365 ?
Does your text file contain the text "SP_KUGIRI" ? in which case the word should be in quotes in your if statement ie

VBA Code:
If strRec = "SP_KUGIRI" Then
After checking further, the same issue happens in Office 2010.

Thank you for your help!

The issue is somewhere else...
 
Upvote 0
Thank you for checking.

I am using exactly the same package as the one running with Office 2010. the only difference is that one system uses 32 bits and the other one 64 bits. I managed to solve some issues by adding PtrSafe so I am wondering it might be the case also here.
SP_KUGIRI is a variable or constant from somewhere else. Somewhere outside of 'Sub GetStorePlanFile' . You must find it in order to know how to migrate it. It is not any kind of special difference between Excel O365 and 2010. @Joe4's suggestion to look at your library references is a good one. You might also look to see if the old setup had any add-ins installed that could have defined it.

In fact, if you still have the old 2010 setup available, then the quickest way might be to use the 2010 VBE debugger to set a breakpoint and inspect the value of SP_KUGIRI.
 
Upvote 0
Solution
Did you check to see if you have any missing libraries/references?
This commonly happens when running the same code on different systems.

If you go to the system where it is working, go to the VB Editor, and under the Tools menu select References, and note all the selected libraries.
Now do the same on your new system, and see if there are any references that were selected from your old system that are missing from this new one.
If there are, find them in the list and select them (note that the version number may be slightly different, but the rest of text verbiage should be the same).
Thank you for checking,

No missing references. The librairies are same on both systems.

I will keep checking
 
Upvote 0
Thank you for checking,

No missing references. The librairies are same on both systems.

I will keep checking
Then the most likely culprit is the SP_KUGIRI variable that rlv01 mentions.
Your code is not recognizing it, so you need to debug and figure out what it is, where it comes from, and and where it is being set.
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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