VBA Excel如何基于部分名称设置工作簿,并根据部分名称检查工作簿是否打开

时间:2013-11-11 20:41:20

标签: vba excel-vba excel

下午好, 我之前从未使用过VBA,所以我真的需要你的帮助! 我有以下宏(我的第一个),它工作正常,但经过我们的区经理测试后,这个文件(“SalesOrderRMTOOL.xlsx”)在他们的计算机上打开了不同的名称。 如何更改我的宏以只读取部分名称?它永远是SalesOrderRMTOOL,但它可能是什么...... ??感谢您的帮助

Private Sub CommandButton1_Click()
    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    Dim wsTool As Worksheet
    Dim wBook As Workbook
On Error Resume Next
    Set wBook = Workbooks("SalesOrderRMTOOL.xlsx")
    If wBook Is Nothing Then
        MsgBox "Please open SaleOrderRMTOOL file"
        Set wBook = Nothing
        Exit Sub
    End If        
    Set wsSource = Workbooks("SalesOrderRMTOOL.xlsx").Sheets("Salesorder")    
    Set wsTarget = Workbooks("RMORDERTOOL.xlsm").Sheets("Sales Order")        
    Application.ScreenUpdating = False    
    Workbooks("RMORDERTOOL.xlsm").Sheets("Tool").Range("i7:i1003").Value = ""
    Workbooks("RMORDERTOOL.xlsm").Sheets("Tool").Range("l7:l1003").Value = ""
    Workbooks("RMORDERTOOL.xlsm").Sheets("Tool").Range("o7:o1003").Value = ""
    wsTarget.Cells.Clear    
    ' Copy header row to Target sheet if target is empty
    If IsEmpty(wsTarget.Range("A1")) Then wsSource.Rows(1).Copy Destination:=wsTarget.Range("A1")    
        ' Define visible filterd cells on source worksheet and copy
        With wsSource
            .Range("A2", .Cells(.Cells(Rows.Count, "A").End(xlUp).Row, .Cells(1, Columns.Count).End(xlToLeft).Column)).SpecialCells(xlCellTypeVisible).Copy
        End With    
        ' Paste to target sheet
        wsTarget.Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False

        Application.CutCopyMode = True
        Application.ScreenUpdating = True

        Workbooks("SalesOrderRMTOOL*.xlsx").Close 0

End Sub

2 个答案:

答案 0 :(得分:2)

我会创建一个简短的函数来返回销售订单工作簿(如果存在)。在带有该函数的模块的顶部,我将使用常量(Const)来保存工作簿名称的开头,以防它发生变化:

'Constant at top of module    
Const WORKBOOK_NAME As String = "SalesOrderRMTOOL"

'Anywhere else in same module    
Function GetSalesOrderWb() As Excel.Workbook
Dim wb As Excel.Workbook

For Each wb In Application.Workbooks
    If Left(wb.Name, Len(WORKBOOK_NAME)) = WORKBOOK_NAME Then
        Set GetSalesOrderWb = wb
        Exit Function
    End If
Next
End Function

然后这样称呼:

Set wBook = GetSalesOrderWb
If wBook Is Nothing Then
    MsgBox "Please open SaleOrderRMTOOL file"
    Exit Sub
End If        

答案 1 :(得分:0)

您可以让使用此宏的人选择他将使用的工作簿,显示如下对话框:

Sub BrowseWorkbooks()
Const nPerColumn  As Long = 38          'number of items per column
Const nWidth As Long = 13                'width of each letter
Const nHeight As Long = 18              'height of each row
Const sID As String = "___SheetGoto"    'name of dialog sheet
Const kCaption As String = " Select Workbook"
                                        'dialog caption
Dim i As Long
Dim TopPos As Long
Dim iBooks As Long
Dim cCols As Long
Dim cLetters As Long
Dim cMaxLetters As Long
Dim cLeft As Long
Dim thisDlg As DialogSheet
Dim CurrentSheet As Worksheet
Dim cb As OptionButton
    Application.ScreenUpdating = False
    If ActiveWorkbook.ProtectStructure Then
        MsgBox "Workbook is protected.", vbCritical
        Exit Sub
    End If
    On Error Resume Next
        Application.DisplayAlerts = False
        ActiveWorkbook.DialogSheets(sID).Delete
        Application.DisplayAlerts = True
    On Error GoTo 0
    Set CurrentSheet = ActiveSheet
    Set thisDlg = ActiveWorkbook.DialogSheets.Add
    With thisDlg
        .Name = sID
        .Visible = xlSheetHidden
        'sets variables for positioning on dialog
        iBooks = 0
        cCols = 0
        cMaxLetters = 0
        cLeft = 78
        TopPos = 40
        For i = 1 To Workbooks.Count
            If i Mod nPerColumn = 1 Then
                cCols = cCols + 1
                TopPos = 40
                cLeft = cLeft + (cMaxLetters * nWidth)
                cMaxLetters = 0
            End If
            Set CurrentWorkbook = Workbooks(i)
            cLetters = Len(CurrentWorkbook.Name)
            If cLetters > cMaxLetters Then
                cMaxLetters = cLetters
            End If
            iBooks = iBooks + 1
            .OptionButtons.Add cLeft, TopPos, cLetters * nWidth, 16.5
            .OptionButtons(iBooks).Text = _
                Workbooks(iBooks).Name
            TopPos = TopPos + 13
        Next i
        .Buttons.Left = cLeft + (cMaxLetters * nWidth) + 24
        CurrentWorkbook.Activate
        With .DialogFrame
            .Height = Application.Max(68, _
                Application.Min(iBooks, nPerColumn) * nHeight + 10)
            .Width = cLeft + (cMaxLetters * nWidth) + 24
            .Caption = kCaption
        End With
        .Buttons("Button 2").BringToFront
        .Buttons("Button 3").BringToFront
        Application.ScreenUpdating = True
        If .Show Then
            For Each cb In thisDlg.OptionButtons
                If cb.Value = xlOn Then
                    'Store the name of the Woorkbook to use it later
                    SelectedWorkBookName = cb.Caption
                    Exit For
                End If
            Next cb
        Else
            MsgBox "Nothing selected"
        End If
        Application.DisplayAlerts = False
        .Delete
    End With
End Sub

然后使用SelectedWorkBookName变量来调用工作簿,如下所示:

Set wBook = Workbooks(SelectedWorkBookName)