excel 将指定名称的工作表复制到另一个工作簿

kt06eoxx  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(139)

此宏将具有数字名称的Excel工作表复制到另一个Excel工作簿。
例如,只有标题为6位数的工作表。例如“140655”。
我还想复制那些有标准英语名称(如“预算”)的工作表。

Const CalcDelay = 0.00000578704

Dim CopyRange As String
Dim PasteRange As String
Dim ScanFileOpen As Byte
Dim ScanCount As Byte
Dim ScanSaveSpec As String
Dim ScanSaveFile As String
Dim ReturnWindow As String
Dim ReportFile As String
Dim ExcelVersion As String

Sub OpenReportFile()
ReturnWindow = [ProcessWinSpec].Value

If [ReportFileFlag].Value = True Then
    Application.ScreenUpdating = False
    Workbooks.Open Filename:=[ReportFileSpec].Value
    Windows(ReturnWindow).Activate
    Application.ScreenUpdating = True
Else
    MsgBox ("Error: File not found")
End If

End Sub

Sub DoScan()
Dim Work As Variant
Dim X As Interger

ReturnWindow = [ProcessWinSpec].Value
ReportFile = [ReportFileName].Value

ExcelVersion = IIf([FileNameExt].Value = ".xls", 2003, 2013)

For Each Work In [ScanFlags]

    ScanFileOpen = 0
    ScanCount = 0

    If Work.Value = 1 Then

        [ScanName].Value = Work.Offset(0, 1).Value
        [ScanCalcRange].Calculate
        ScanSaveFile = [ScanFile].Value
        ScanSaveSpec = [ScanSpec].Value

        For X = Work.Offset(0, 2).Value To 1 Step -1
            ScanTabName = Work.Offset(0, X + 2).Value
            [ScanTab].Value = ScanTabName
            [ScanCalcRange].Calculate
            If [ReadFlag].Value = 1 Then DoCopyTab
        Next
    End If
    If ScanFileOpen = 1 Then
        ActiveWorkbook.Save
        ActiveWindow.Close
    End If

Next

End Sub
jgwigjjp

jgwigjjp1#

关于你的问题,如果文件有一个特定的名字,如何执行宏,可能最好的方法是创建一个单词数组,然后循环通过它们寻找匹配。

Sub OpenReportFile()
    Const yourWords = "budget,actual,accept" '<--- fill these in separated by comman
    ReturnWindow = [ProcessWinSpec].Value
    Dim foundMatch As Boolean
    
    If [ReportFileFlag].Value = True Then
        foundMatch = True
    Else
        
        Dim wordArray() As String, i As Long
        wordArray = Split(yourWords, ",")
        
        'loopS through words
        For i = LBound(wordArray) To UBound(wordArray)
            If UCase(wordArray(i)) = UCase([ReportFileFlag].Value) Then
                foundMatch = True
                Exit For 'exits loop after match
            End If
        Next i
    End If
    
    If foundMatch Then
      Application.ScreenUpdating = False
      Workbooks.Open Filename:=[ReportFileSpec].Value
      Windows(ReturnWindow).Activate
      Application.ScreenUpdating = True
    Else
        MsgBox ("Error: File not found")
    End If

End Sub

正如你在评论中所看到的,你的问题没有得到最好的反馈,如果这不起作用,你可能需要考虑删除你的问题,并在更仔细地阅读How to ask a question后重新发布

相关问题