Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

Macro corrupts my Excel file if executed when open file

$
0
0

I made a macro that when executed, create 2 arrays of strings with all the files in specific folders, then creates a drop down data validation in a cell, if i manually execute the macro everything works perfect, if i save the file and reopen it, well, all works perfect, the problem is if i add the macro into the Open event of ThisWorkbook and save... heres the wierd part, ill describe then as option 1 and 2:

Option 1

Reopen the file, macro runs perfect and shows me my drop down list without issues... BUT... if i save the document with that data validation in place, when i open the file again it says is corrupted and has to recover the file, so my sheet formating goes to crap and i have to do it all over again.

Option 2 Reopen the file, macro runs, i get my data validation, THEN i execute a .Clear on the cell and then save the file, if i do it that way when i open the file its not corrupted.

I dont know what to do to have this fixed, it was working fine until i started adding more code to that macro, now its corrupting my file again everytime i save it with the data validation in place :S, heres the code of the macro:

Private Sub Workbook_Open()
    Dim ocName As String
    Dim neName As String
    Dim arNE(8000) As String
    Dim arOC(8000) As String
    Dim mon As Variant
    Dim mn As Variant
    Dim myList As String
    Dim val As Integer

    Range("O5").Select

    mon = Array("ENERO", "FEBRERO", "MARZO", "ABRIL", "MAYO", "JUNIO", "JULIO", "AGOSTO", "SEPTIEMBRE", "OCTUBRE", "NOVIEMBRE", "DICIEMBRE")
    mn = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")

    i = 1
    For m = 0 To 11
        neName = Dir("\\Server-PC\E$\Grupos\Empaque\Hortalizas\Notas de Entrada\"& m + 1 & ".- "& mon(m) & " (N)\")

        Do While neName <> ""
            If Mid(neName, InStr(neName, "") + 1, InStr(InStr(neName, "") + 1, neName, "-", vbBinaryCompare) - InStr(neName, "") - 1) = "OCF" Then
                arNE(i) = Mid(neName, InStr(neName, "") + 1, InStr(InStr(neName, "") + 1, neName, "", vbBinaryCompare) - InStr(neName, "") - 1)
                i = i + 1
            End If
            neName = Dir()
        Loop
    Next m

    j = 1
    For n = 0 To 11
        ocName = Dir("\\Server-PC\E$\Grupos\Grupo Manga Menor\Ordenes de Compra\2019\ORDENES DE COMPRA\"& mn(n) & ".-"& mon(n) & "\04.-HORTALIZAS\")

        Do While ocName <> ""
            If Left(ocName, InStr(ocName, "-") - 1) = "OCF" Then
                arOC(j) = Left(ocName, InStr(ocName, "") - 1)
                j = j + 1
            End If
            ocName = Dir()
        Loop
    Next n

    val = 0
    For ocj = j - 1 To 1 Step -1
        For nei = 1 To i - 1
            If arOC(ocj) = arNE(nei) Then
                val = 1
                Exit For
            End If
        Next nei

        If val = 0 Then myList = myList & arOC(ocj) & ","

        val = 0
    Next ocj

    With Range("O5").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=myList
    End With

    Range("O5").Value = ""
End Sub

Viewing all articles
Browse latest Browse all 88854

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>