如何使用VBA查看工作表是否包含任何先例?

时间:2019-07-18 13:46:50

标签: excel vba

我正在整理一个宏,以识别工作簿中的浪费空间。我要识别的一件事是工作簿中其他任何地方未引用的选项卡。

我尝试了一个dependents.count循环,以及在我的代码中看到的“查找”。不受任何限制,只需要在VBA中完成。

Sub Test3()

Dim a As Integer
Dim i As Integer
Dim j As Integer
Dim r As Range


a = 1
i = 1
j = Application.Sheets.Count
j = j + 1

Do While i < j
a = a + 1
With Worksheets(i).UsedRange
Set r = .Find(Worksheets("Summary").Cells(a, 1), LookIn:=xlFormulas, LookAt:=xlPart)


If Not r Is Nothing Then
Worksheets("Summary").Cells(a, 4).Value = "Yes"
Else: Worksheets("Summary").Cells(a, 4).Value = "No"
End If

End With
i = i + 1
Loop

End Sub

在“摘要”选项卡的D列中,查找在该选项卡中引用的工作表,使其具有“是”。未引用的人应带有“否”。

2 个答案:

答案 0 :(得分:0)

也许尝试一下,它可能会起作用

Sub Test3()

Dim a As Integer
Dim i As Integer
Dim j As Integer
Dim r As Range


a = 1
i = 1
j = Application.Sheets.Count

If Sheets(1).Name = "Summary" Then
Worksheets("Summary").Cells(a, 4).Value = "Yes"
Else: Worksheets("Summary").Cells(a, 4).Value = "No"
End If


Do While i < j
a = a + 1
With Worksheets(i).UsedRange
Set r = .Find(Worksheets("Summary").Cells(a, 1), LookIn:=xlFormulas, LookAt:=xlPart)


If Not r Is Nothing Then
Worksheets("Summary").Cells(a, 4).Value = "Yes"
Else: Worksheets("Summary").Cells(a, 4).Value = "No"
End If

End With
i = i + 1
Loop

End Sub

答案 1 :(得分:0)

这就是我终于要工作的地方。其他答案可能就足够了,而我只是没有正确地应用它们。感谢所有提供帮助的人。

Sub Dependents()

Dim a As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim r As Range


a = 2
i = 1
j = Application.Sheets.Count
k = Application.Sheets.Count
k = k + 1


Do While i < j

a = 2

Do While a < k

With Worksheets(i).UsedRange
Set r = .Find(Worksheets("Summary").Cells(a, 1), LookIn:=xlFormulas, LookAt:=xlPart)

If Worksheets("Summary").Cells(a, 4).Value <> "Yes" Then

If Not r Is Nothing Then
Worksheets("Summary").Cells(a, 4).Value = "Yes"
Else: Worksheets("Summary").Cells(a, 4).Value = "No"
End If

End If

End With

a = a + 1

Loop

i = i + 1

Loop

End Sub