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
任何幫助將不勝感激。我是一個業餘愛好者,一直試圖從我在這裏和其他地方找到的代碼片斷中拼湊出來。
在此先感謝!
取決於其是否已經打開。但是,無法將字符串分配給工作簿類型。類型不匹配只是告訴你這個錯誤,你甚至會得到錯誤的錯誤行。首先要做的是檢查=的兩邊是否是相同的類型。 –
感謝Gordon的回覆,但遺憾的是仍然無法正常工作。我確實找到了一種方法,用這個 – JBond
...'Set wb1 = Workbooks(UserForm1.ComboBox1.Value)',但現在即使在我選擇不同的工作簿時,它也被設置爲主工作簿。當我遍歷代碼時,它會返回到用戶表單代碼,但不會顯示錶單 - 所以我想它會爲每個ComboBox再次確定值。我更接近,但仍然難倒。沒有辦法從UserForm中選定的值聲明變量,然後將它們傳遞給模塊?將它們設置爲公共或全局可能? – JBond