很多这可以在 Excel 公式中完成,但如果你想检查另一个工作簿的每个工作表中的每个单元格,那么你可能想在 VBA 中做一些事情。这是一个例子:
Dim i as integer, k as integer, j as integer 'We declare our variables.
Dim b as Workbook, temp as Worksheet
set b = Workbooks("Book2.xlt") 'We set our workbook variable (makes things easier)
Dim mySheet As Worksheet
Set mySheet = Excel.ActiveSheet 'We set our variable for the current worksheet (the one that contains the original cell).
dim myCell as range
set myCell = mySheet.Range(rCell.Address) 'We set our original cell variable. This is the cell that has the value we want to check for.
'I used the cell you specified so this will not work if there is an error there.
for i = 1 to b.Worksheets.Count 'Loop through each worksheet in the workbook we specified. The worksheet are on a 1-based array so we start with 1 and end at the count.
set temp = b.Worksheets(i) 'Set the worksheet variable to make things easier.
for k = 1 to temp.UsedRange.Columns.Count 'Loop through each column in the current worksheet's used range.
for j = 1 to temp.UsedRange.Rows.Count 'Loop through each row in that column.
if temp.Cells(j,k).value = myCell.Value then 'Check the cell for that row+column address for a match.
MsgBox "valid" 'If it matches, do something here.
MsgBox Worksheets.Count 'Counts the sheets in the current book? Don't know why though.
else
'If it doesn't match, do something else here.
end if
next j 'Complete the loops.
next k
next i
适合初学者的一些很棒的 VBA 参考资料
这篇文章有一些关于 VBA 的很好的注释,可能会为您提供更多帮助。
这个页面有一些关于如何使用 VBA 引用其他工作簿的优秀提示,包括检查给定文件夹中每个工作簿的方法。