简体   繁体   中英

Excel Run macro from button issue

I made a macro that processes multiple files in a directory and generated CSV files to take them a database.

When I run the macro from the Macros menu or "step Into" I have no problem. it Gos through all the files and all rows (in the files).

But when I assign the macro to a button, it goes through all the files but fails in some rows And the result in CSV is different.

Works

在此处输入图片说明

Does Not work

在此处输入图片说明

GOOD CSV :D

NAME , GROUP1, 25,13
NAME1, GROUP1, 25,17
NAME2, GROUP2, 27,14
NAME3, GROUP2, 28,16
NAME2, GROUP2, 23,12

WRONG CSV D:

NAME , GROUP1, 25,13
NAME1, GROUP1, 25,17
, GROUP2, 27,14
, GROUP2, 23,12

fewer rows and the wrong format

I don't think it should go a part of the macro, it works perfectly without the button, but I need it.

I tested with

 Application.ScreenUpdating = False

And

 Application.ScreenUpdating = True

PD: sorry for my english.

EDIT I set the button to another macro, to run the other, but the result is the same. Start sheet, it does not matter. (tested) THE CODE: (NO EDIT)

Public Sub ProcesarTodo()

    Application.ScreenUpdating = False
    Dim Filename, Pathname As String
    Dim wb As Workbook

    Pathname = ActiveWorkbook.path & "\Inscripciones\"
    Exportpath = ActiveWorkbook.path & "\CSV\"
    ExportpathE = ActiveWorkbook.path & "\CSV_E\"
    Filename = Dir(Pathname & "*.xls")

    answer = MsgBox("Eliminar archivos de carpeta CSV?", vbYesNo + vbQuestion, "VACIAR CSV")
    If answer = vbYes Then
        On Error Resume Next
        Kill Exportpath & "*.csv"
        Kill ExportpathE & "*.csv"
        On Error GoTo 0
    End If

    a = 0
    rows = 0
    rowsE = 0
    Dim Data(1 To 1) As String
    Dim Hojas(1 To 2) As String
    Data(1) = "Z"
    Hojas(1) = "A"
    Hojas(2) = "B"
    etapa = 3

    Do While Filename <> ""
        Set wb = Workbooks.Open(Pathname & Filename)
        '   CREAR SI NO EXISTE HOJA PARA ATLETAS
        Dim mySheetName As String, mySheetNameTest As String
        mySheetName = "Procesar"
        On Error Resume Next
        mySheetNameTest = Worksheets(mySheetName).name
        If Err.Number = 0 Then
            Worksheets(mySheetName).Cells.Clear
        Else
            Err.Clear
            Worksheets.Add.name = mySheetName
        End If
        '   This function get data from the file's name.
        get_data
        n = 1
        For Each Hoja In Hojas
            Sheets(Hoja).Select
            For i = Cells(9, 7).Value To Cells(9, 9).Value Step 2
                For j = Cells(10, 3).Value To Cells(10, 5).Value
                    If Cells(j, i).Value = "T" Or Cells(j, i).Value = "t" Or Cells(j, i).Value = "R" Or Cells(j, i).Value = "r" Then
                        Sheets("Procesar").Cells(n, 1).Value = Sheets(Hoja).Cells(j, 2).Value
                        Sheets("Procesar").Cells(n, 2).Value = equipo                               
                        Sheets("Procesar").Cells(n, 3).Value = Sheets(Hoja).Cells(11, i).Value
                        Worksheets(Hoja).Cells(j, i + 1).copy                                       
                        Worksheets("Procesar").Cells(n, 4).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False                                             
                        Sheets("Procesar").Cells(n, 5).Value = Sheets(Hoja).Cells(j, i).Value
                        Sheets("Procesar").Cells(n, 6).Value = Sheets(Hoja).Cells(12, i).Value
                        n = n + 1
                    End If
                Next j
            Next i
        Next Hoja
        n = n - 1

        Sheets("Procesar").Select
        Range("H1").Select
        Let x = 1
        Do While x <= n
        ActiveCell.FormulaR1C1 = "=PROPER(RC[-7])"
        Selection.Offset(1, 0).Select
        x = x + 1
        Loop
        Range("H1:H" & n).Select
        Selection.copy
        Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("H1:H" & n).Select
        Selection.ClearContents
        '   Exportar Atletas
        Call Exportar(Exportpath, wb.name, n)

        wb.Close SaveChanges:=False
        Filename = Dir()
        a = a + 1
    Loop ' Next file
    Application.ScreenUpdating = True
    bat
    mensaje = MsgBox("Se procesaron " & a & " archivos" & vbNewLine & "El cual son " & rows & " atletas" & vbNewLine & "Y " & rowsE & " Entrenadores." & vbNewLine & "Programa realizado por Tomas Prado", , "Listo")

End Sub

Function Exportar(path, name, n)
    equipo = Replace(name, ".xlsx", "")
    equipo2 = Replace(equipo, ".xls", "")
    Let Rango = "A1:" & "F" & n
    ActiveSheet.Range(Rango).Select
    Selection.copy
    Worksheets.Add.name = "Exportar"
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ' Formato antiguo xlCSV
    ActiveWorkbook.SaveAs Filename:= _
        path & equipo2 & ".csv", FileFormat:=xlCSV, _
        CreateBackup:=False
    rows = rows + n
End Function

There is only one way which cannot work. That is wrong place of your method .

One thing to know is you are using Active X control . So, click method for that button must have inside the sheet's module . I means, you putted your button in Sheet1 , you need to also put the event methods of that button in Sheet1's module .

And also method name must be Private Sub buttonName_Click() . In that, buttonName should not be button label (eg. in your "ProcesarTodo"). It should be like "CommandButton1". You can also change that name in VBA Editor's Properties tag. If you change the name, you method name should be Private Sub ProcesarTodo_Click() .

So, check your code and module again. You hide names of module in your evidence, so, I can say by hint. If you show clearly, I can say exactly.

If you don't know, how to do, just following steps:

  1. Right Click on your button.
  2. From the list, choose "View Code".
  3. So, you can see the right method for your button, copy and paste all method body from your old to that new one.
  4. And then, test about it. It will work perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM