2017-06-19 6 views
0

ユーザーが特定のブックやワークシートから(マクロがある)マスターブックを更新できるようにするマクロを作成しようとしています。ワークブックとワークシートの名前は変えることができるので、私はthe ozgrid forumから適応コードからこのユーザーフォームを呼び出す:ユーザーフォームを使用して選択したブックからワークシートを選択する方法

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 

任意の助けをいただければ幸い私は、このビットを持つ型の不一致エラーが得ます。私はアマチュアであり、ここと他の場所で見つけたコードスニペットからまとめようとしています。

ありがとうございます!そのが既に開いている場合

答えて

0

Set wb1=Workbooks(myFile)

+0

Setwb1 = MyFileを交換しては依存しています。しかし、ええと、ワークブックのタイプに文字列を割り当てることはできません。タイプの不一致がエラーを伝えたばかりで、エラー行もエラーになります。最初に行うことは、=の両辺が同じタイプであるかどうかをチェックすることです。 –

+0

Gordonさんにお返事ありがとうございますが、それでも残念ながらうまくいきません。私はこれで道を見つけました – JBond

+0

... 'Set wb1 = Workbooks(UserForm1.ComboBox1.Value) 'を設定しましたが、別のワークブックを選択してもマスターワークブックとして設定されます。コードをステップ実行すると、ユーザーフォームコードに戻りますが、フォームが表示されないため、ComboBoxごとに値が再び決定されます。私は近くにいるが、まだ困っている。 UserFormで選択された値から変数を宣言してモジュールに渡す方法はありませんか?パブリックまたはグローバルに設定することは可能でしょうか? – JBond

関連する問題