我正在创建一个具有验证按钮的宏。这意味着,我将上传现有的工作簿(与主宏相同的工作表)。如果有任何重复,我需要比较wb1和wb2表,并最终突出显示主宏表上的重复项。到目前为止,这就是我现在所拥有的,但它不允许我使用Set WorkRng1 = Wb1.Sharepoint.Range(“A”& Sharepoint.Rows.Count).End(xlUp).row部分。以下是我的代码:
Sub UploadandCompareSheets()
Dim Wb1 As Workbook
Dim wb2 As Workbook
Dim MainPage As Worksheet
Set MainPage = Sheets("Main")
Dim tbl As ListObject
Dim ws1 As Worksheet
Dim Sharepoint As Worksheet
Set Sharepoint = Sheets("PRP Sharepoint")
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set Wb1 = ActiveWorkbook
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a File", _
filefilter:="Excel File *.xlsx (*.xlsx),")
If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)
For Each sheet In wb2.Sheets
If sheet.Visible = True Then
Dim WorkRng1 As Range, WorkRng2 As Range, Rng1 As Range, Rng2 As Range
Set WorkRng1 = Wb1.Sharepoint.Range("A" & Sharepoint.Rows.Count).End(xlUp).row
Set WorkRng2 = wb2.Sharepoint.Range("A" & Sharepoint.Rows.Count).End(xlUp).row
For Each Rng1 In WorkRng1
rng1Value = Rng1.value
For Each Rng2 In WorkRng2
If rng1Value = Rng2.value Then
Rng1.Interior.Color = VBA.RGB(255, 0, 0)
Exit For
End If
Next
Next
End If
Next sheet
End If
End Sub
答案 0 :(得分:0)
您正在设置名为SharePoint的对象:
Set Sharepoint = Sheets("PRP Sharepoint")
然后您尝试访问2个不同的工作簿:
Set WorkRng1 = Wb1.Sharepoint.Range("A" & Sharepoint.Rows.Count).End(xlUp).row
Set WorkRng2 = wb2.Sharepoint.Range("A" & Sharepoint.Rows.Count).End(xlUp).row
您无法执行此操作,因为SharePoint不是工作簿对象的方法。最初创建SharePoint对象时,它将引用特定工作表(如果您没有指定哪一个,则指向ActiveWorkbook中的工作表)。您需要创建2个单独的Sheet对象(打开工作簿后):
Set Sharepoint1 = Wb1.Sheets("PRP Sharepoint")
Set Sharepoint2 = Wb2.Sheets("PRP Sharepoint")
或在声明范围时直接参考工作表名称:
Set WorkRng1 = Wb1.Sheets("PRP Sharepoint").Range("A" & Sharepoint.Rows.Count).End(xlUp).row
Set WorkRng2 = wb2.Sheets("PRP Sharepoint").Range("A" & Sharepoint.Rows.Count).End(xlUp).row