每当某些文件保存在文件夹中时,自动触发宏

时间:2018-08-03 02:38:28

标签: excel vba excel-vba

我正在尝试找出什么可能导致每次我的同事将文件保存到共享驱动器时运行宏。

“ xxx Report.xlsx”每月保存到共享文件夹中,并且内部数据自动与其他电子表格一起重新组织。我发现进行了重新组织/修改的宏,但我真的不了解是什么触发了宏的运行。

我不认为它在任务计划程序下,因为写宏的人离开了,宏仍在运行...

有什么建议吗?谢谢!

Sub OrganiseTimesheetReport()

Application.ScreenUpdating = False

   'Renaming Original Report
    ActiveSheet.Name = "Original Report"

   'Safekeeping the original report - Duplicating the worksheet for further working
    ActiveSheet.Copy ActiveWorkbook.Sheets(1)

   'Renaming Organised Report
    ActiveSheet.Name = "Organised Report"

   'Deleting Top Information & Empty Row Between Header and Content & Last Grand Total Row
    Dim HeaderInfoDelete As Range

    Set HeaderInfoDelete = Range("B1", "B" & Rows.Count).Find(What:="Calculated total records", LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=False)

    If Not HeaderInfoDelete Is Nothing Then
      If HeaderInfoDelete.Row > 1 Then Range("B1:B" & HeaderInfoDelete.Row + 1).EntireRow.Delete
    End If

   'Deleting Unnecessary Columns & Rows
    Dim LastRow As Long, LastCol As Long, col As Long, LRow As Long

    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

    For LRow = LastRow To 1 Step -1
        If Trim(Cells(LRow, 5)) = "" Then
        Rows(LRow).Delete
        End If
    Next LRow

    Range("B1").Copy
    Range("C1").PasteSpecial xlPasteValues
    Range("B1").ClearContents

    For col = LastCol To 1 Step -1
        If Trim(Cells(1, col)) = "" Then
        Columns(col).Delete
        End If
    Next col

    Dim ReLastRow As Long, ReLRow As Long

    ReLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    For ReLRow = ReLastRow To 1 Step -1
        If Trim(Cells(ReLRow, 1)) = "" Then
        Rows(ReLRow).Delete
        End If
    Next ReLRow

 ' Pasting the Header

    Set Cwb = ActiveWorkbook
    Set Mwb = ThisWorkbook

        Mwb.Sheets("Report Templates").Rows("1:1").Copy
        Cwb.Sheets("Organised Report").Rows("1:1").Insert
        Cells.EntireColumn.AutoFit

 ' Converting DateTime Format
    Dim col As Integer
    For col = 6 To 9

    With Sheets("Organised Report").UsedRange.Columns(col).Cells
        .TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, FieldInfo:=Array(0, xlDMYFormat)
        .NumberFormat = "dd/mmm/yyyy"
    End With

    Next col

Application.ScreenUpdating = True

结束子

1 个答案:

答案 0 :(得分:0)

如果您正在任务计划程序中查找,则可以:

  • 我真的不明白你要做什么
  • 您正在尝试做以前从未做过的事情。

如果是前者-我一直都在发生

如果是后者-我一直都在发生……第一次做某事很有趣!


您遇到的行为是VBA自动化的典型用例,通常是由特定的条件触发的。一个典型的例子是资产负债表和损益表的生成和分配。如果当前日期在特定窗口内并且某些字段已更新,则可能触发VBA运行。例如,如果它在该月的1号之后但在7号之前,则存在前一个月的数据,并且该程序尚未运行...则可以将其设置为自动运行。此外,它可能是“礼貌”的,请等到您保存或尝试关闭工作簿并将该事件用作触发器。

如果您不明白这一点,请不要担心,找出来很容易!

为避免造成混淆,请确保没有打开其他电子表格或Excel窗口。然后继续打开电子表格:

  • 同时按Alt和f11打开VBA编辑器

  • 应该会打开一个新窗口,您应该在屏幕左侧看到一个带有可扩展/可折叠树(通常称为“ myproject”)的项目浏览器

  • 展开项目树,然后双击“工作簿”。发现不同版本的excel略有不同,您可能必须先展开一个或两个文件夹,然后才能看到它。也有可能它已被重命名,但可能性很小。

  • 双击后,屏幕的中心应显示由工作簿事件触发的任何VBA代码。如果文本很多,请ctrl+f打开查找框并搜索单词“保存”。

  • 寻找类似的东西

    private sub workbook_BeforeSave()
    

如果找不到任何相关的内容,则在类模块中可能会有一个自定义事件处理程序-在这种情况下,我的建议是制作电子表格的备份副本,以防万一,然后环顾四周并尝试弄清楚触及率程序的作用。您可能已经找到了不错的资源,可以帮助解决常见问题或使您的工作流程自动化。

我可能应该首先提到这一点...如果此文件是启用了宏的工作簿,那么这些步骤很可能会使您到达我认为想要的位置。