【问题标题】:Call one subroutine from another从另一个子程序调用一个子程序
【发布时间】:2019-06-20 03:59:12
【问题描述】:

我正在尝试取消合并和复制 xlsx 文件文件夹的数据。

另外,两个宏都按预期工作。当我组合宏(通过“调用”)时,它会执行但随后将我带回宏屏幕。它没有给我任何错误,但我需要关闭 excel 重新开始。

我猜“UnMergeFill”宏不适合自动打开?

我尝试过使用“呼叫”以及仅使用子名称。我也尝试将潜艇分成不同的模块。

Sub AllWorkbooks()

   Dim MyFolder As String
   Dim MyFile As String
   Dim wbk As Workbook

On Error Resume Next

Application.ScreenUpdating = False

With Application.FileDialog(msoFileDialogFolderPicker)

.Title = "Please select a folder"

.Show

.AllowMultiSelect = False

   If .SelectedItems.Count = 0 Then

MsgBox "You did not select a folder"

      Exit Sub

   End If

MyFolder = .SelectedItems(1) & "\"

End With

MyFile = Dir(MyFolder)

Do While MyFile <> “”

   Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)


UnMergeFill


wbk.Close savechanges:=True

MyFile = Dir

Loop

Application.ScreenUpdating = True

End Sub


Call Sub UnMergeFill()

Dim cell As Range, joinedCells As Range

For Each cell In ThisWorkbook.ActiveSheet.UsedRange
    If cell.MergeCells Then
        Set joinedCells = cell.MergeArea
        cell.MergeCells = False
        joinedCells.Value = cell.Value
    End If
Next

End Sub

'''

【问题讨论】:

  • 首先,注释掉或者去掉On Error Resume Next。代码会抛出任何错误吗?其次,您不能在一行中有Call Sub UnMergeFill()Call 似乎是一个错字。第三,您打算让UnMergeFill 处理ThisWorkbook 还是您正在打开的工作簿?
  • 感谢您的反馈。我希望它可以在打开的工作簿上工作。 Mikku 的解决方案奏效了。

标签: excel vba


【解决方案1】:

试试这个:

Sub AllWorkbooks()

   Dim MyFolder As String
   Dim MyFile As String
   Dim wbk As Workbook

On Error Resume Next

Application.ScreenUpdating = False

With Application.FileDialog(msoFileDialogFolderPicker)

.Title = "Please select a folder"

.Show

.AllowMultiSelect = False

   If .SelectedItems.Count = 0 Then

MsgBox "You did not select a folder"

      Exit Sub

   End If

MyFolder = .SelectedItems(1) & "\"

End With

MyFile = Dir(MyFolder)

Do While MyFile <> “”

   Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)


Call UnMergeFill(wbk)


wbk.Close savechanges:=True

MyFile = Dir

Loop

Application.ScreenUpdating = True

End Sub


Sub UnMergeFill(wb As Workbook)

Dim cell As Range, joinedCells As Range

For Each cell In wb.ActiveSheet.UsedRange
    If cell.mergeCells Then
        Set joinedCells = cell.MergeArea
        cell.mergeCells = False
        joinedCells.Value = cell.Value
    End If
Next

End Sub

【讨论】:

  • 成功了,非常感谢。对此,我真的非常感激! :)
  • @BigBen .. 我没有添加那行...我所做的只是改变了调用宏的方式。
  • @deetseeker ...很高兴它起作用了。接受答案:)
  • @BigBen ...没错。 :)
  • 接受了答案。根据反馈,我也删除了“On error resume next”行。感谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多