简体   繁体   English

Excel Run宏从按钮问题

[英]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. 我制作了一个宏,该宏处理目录中的多个文件,并生成CSV文件以将其作为数据库。

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. 但是,当我将宏分配给按钮时,它会遍历所有文件,但在某些行中会失败,并且CSV中的结果会有所不同。

Works 作品

在此处输入图片说明

Does Not work 不起作用

在此处输入图片说明

GOOD CSV :D 良好的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: 错误的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. PD:对不起,我的英语。

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 . 要知道的一件事是您正在使用Active X控件 So, click method for that button must have inside the sheet's module . 因此,该按钮的click方法必须在工作表的模块中 I means, you putted your button in Sheet1 , you need to also put the event methods of that button in Sheet1's module . 我的意思是,您将按钮放在Sheet1中 ,还需要将该按钮的事件方法放入Sheet1的模块中

And also method name must be Private Sub buttonName_Click() . 而且方法名称也必须是Private Sub buttonName_Click() In that, buttonName should not be button label (eg. in your "ProcesarTodo"). 那样, buttonName不应是按钮标签 (例如,在“ ProcesarTodo”中)。 It should be like "CommandButton1". 它应该像“ CommandButton1”。 You can also change that name in VBA Editor's Properties tag. 您也可以在VBA编辑器的“属性”标签中更改该名称。 If you change the name, you method name should be Private Sub ProcesarTodo_Click() . 如果更改名称,则方法名称应为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. 它将完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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