如何使用UserForm从所选工作簿中选择工作表

时间:2017-06-19 21:34:29

标签: excel-vba vba excel

我正在尝试创建一个宏,允许用户从特定的工作簿和工作表更新主工作簿(宏所在的位置)。工作簿和工作表名称可能会有所不同,因此我从the ozgrid forum改编的代码中调用此UserForm:

Option Explicit

Private Sub CommandButton1_Click()
    MyFile = Me.ComboBox1.Value
    MySheet = Me.ComboBox2.Value
    Unload Me
End Sub

Private Sub CommandButton2_Click()
    Stopped = True
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Dim wkb As Workbook
    Dim sht As Worksheet
    Me.Label1.Caption = "Please select one of the following files and Worksheets..."
    With Me.ComboBox1
        For Each wkb In Application.Workbooks
            .AddItem wkb.Name
        Next wkb
    End With
    MyFile = Me.ComboBox1.Value
    With Me.ComboBox2
        For Each sht In Application.Worksheets
            .AddItem sht.Name
        Next sht
    End With
    MySheet = Me.ComboBox2.Value
End Sub

现在问题出在主工作簿中调用用户表单的模块。我用这个位得到一个类型不匹配错误:设置wb1 = MyFile

以下是该模块的其余部分:

Option Explicit

Public MyFile As String
Public Stopped As Boolean

Sub Update_Master()
    Stopped = False
    UserForm1.Show
    If Stopped Then Exit Sub
    MsgBox MyFile

' Update_Master Macro
'
' Keyboard Shortcut: Ctrl+m
'
    Dim wb1 As Workbook, wb2 As Workbook
    Dim TargetFile As Variant
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim ws1LRow As Long, ws2LRow As Long
    Dim i As Long, j As Long
    Dim ws1LCol As Long, ws2LCol As Long
    Dim aCell As Range, bCell As Range
    Dim SearchString As String
    Dim ExitLoop As Boolean, matchFound As Boolean

    '~~> Set EOD Workbook
    Set wb1 = MyFile
    Set ws1 = MySheet
    '~~> Get the last Row and Last Column
    With wb1
        ws1LRow = .Range("E" & .Rows.Count).End(xlUp).Row
        ws1LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With

    '~~> Set Master Workbook
    Set wb2 = Workbooks("MoO - Master List - TEST.xlsm")
    Set ws2 = wb2.Sheets("CM List")
    '~~> Get the last Row and Last Column
    With ws2
        ws2LRow = .Range("E" & .Rows.Count).End(xlUp).Row
        ws2LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With

    '~~> Loop Through Cells of Col E in workbook A and try and find it
    '~~> in Col E of workbook B
    For i = 2 To ws1LRow
        SearchString = ws1.Range("E" & i).Value

        Set aCell = ws2.Columns(5).Find(What:=SearchString, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)

        ExitLoop = False

        '~~> If match found
        If Not aCell Is Nothing Then
            Set bCell = aCell

            matchFound = True

            '~~> Then compare all columns
            For j = 6 To ws1LCol
                If ws1.Cells(i, j).Value <> ws2.Cells(aCell.Row, j).Value Then
                    matchFound = False
                    Exit For
                End If
            Next

            '~~> If all columns matched then write to Col A/B
            If matchFound = False Then
                ws2.Cells(aCell.Row, 12).Value = ws1.Cells(i, 12).Value
                ws2.Cells(aCell.Row, 13).Value = ws1.Cells(i, 13).Value
            End If

            '~~> Find Next Match
            Do While ExitLoop = False
                Set aCell = ws2.Columns(5).FindNext(After:=aCell)

                '~~> If match found
                If Not aCell Is Nothing Then
                    If aCell.Address = bCell.Address Then Exit Do

                    matchFound = True

                    '~~> Then compare all columns
                    For j = 6 To ws1LCol
                        If ws1.Cells(i, j).Value <> ws2.Cells(aCell.Row, j).Value Then
                            matchFound = False
                            Exit For
                        End If
                    Next

                    '~~> If all columns matched then write to Col A/B
                    If matchFound = False Then
                        ws2.Cells(aCell.Row, 12).Value = ws1.Cells(i, 12).Value
                        ws2.Cells(aCell.Row, 13).Value = ws1.Cells(i, 13).Value
                    End If
                Else
                    ExitLoop = True
                End If
            Loop
        End If
    Next
End Sub

非常感谢任何帮助。我是一个业余爱好者,并一直试图从我在这里和其他地方找到的代码片段中拼凑出来。

提前致谢!

1 个答案:

答案 0 :(得分:0)

Set wb1 = MyFile替换为Set wb1=Workbooks(myFile)