Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
EXCEL VBA调试:搜索整个工作簿_Excel_Vba - Fatal编程技术网

EXCEL VBA调试:搜索整个工作簿

EXCEL VBA调试:搜索整个工作簿,excel,vba,Excel,Vba,我正在为Excel中的数据库编写VBA宏。我有一个工作表,它存储了诸如姓名、电子邮件等信息(遗憾的是,这些信息在所有工作表中并不一致地放在相同的列中,但电子邮件地址从“B:F”开始),这个数据库被拆分为多个工作表。除了所有这些工作表之外,我还有一个工作表(“下面代码中的Sheet2”),它存储了分配给我的新闻稿的所有电子邮件地址。(本表中唯一的信息是“A”列中的电子邮件地址) 我正在使用的VBA应该循环浏览订阅新闻稿(“Sheet2”)的所有电子邮件地址,并检查它们是否存储在“数据库”中,以及其

我正在为Excel中的数据库编写VBA宏。我有一个工作表,它存储了诸如姓名、电子邮件等信息(遗憾的是,这些信息在所有工作表中并不一致地放在相同的列中,但电子邮件地址从“B:F”开始),这个数据库被拆分为多个工作表。除了所有这些工作表之外,我还有一个工作表(“下面代码中的Sheet2”),它存储了分配给我的新闻稿的所有电子邮件地址。(本表中唯一的信息是“A”列中的电子邮件地址)

我正在使用的VBA应该循环浏览订阅新闻稿(“Sheet2”)的所有电子邮件地址,并检查它们是否存储在“数据库”中,以及其他页面中。如果没有,则发出警告-在电子邮件旁边的单元格中填写“NOTFOUND”

由于某些原因,VBA在行上给了我一个运行时错误“对象不支持此属性或方法”:

带有纸张(纸张索引)。范围(“B:F”)

最初我认为原因是我没有激活工作表,但我仍然得到错误

到目前为止,我提出的代码是:

Sub Search_for_emails()

Dim scanstring As String
Dim foundscan As Range
Dim lastRowIndex As Long
Dim ASheet As Worksheet

Set ASheet = Sheets("Sheet2")

lastRowInteger = ASheet.Range("A1", ASheet.Range("A1").End(xlDown)).Rows.Count

For rowNum = 1 To lastRowInteger
    scanstring = Sheets("Sheet2").Cells(rowNum, 1).Value
    For sheetIndex = 1 To ThisWorkbook.Sheets.Count
        Sheets(sheetIndex).Activate
        If Sheets(sheetIndex).Name <> "Sheet2" Then
            With Sheets(sheetIndex).Range("B:F")
                Set foundscan = .Find(What:=scanstring, LookIn:=xlValues, LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
            End With
            If foundscan Is Nothing Then
                ASheet.Cells(rowNum, 2).Value = "NOTFOUND"

            Else

                ' ASheet.Cells(rowNum, 2).Value = foundscan.Rows.Count

            End If
        End If
    Next
Next rowNum
子搜索电子邮件()
作为字符串的字符串
Dim foundscan As范围
将lastRowIndex变长
将ASheet设置为工作表
设置为图纸=图纸(“图纸2”)
lastRowInteger=ASheet.Range(“A1”,ASheet.Range(“A1”).End(xlDown)).Rows.Count
对于rowNum=1到lastRowInteger
扫描字符串=工作表(“Sheet2”).单元格(行数,1).值
对于sheetIndex=1,将其添加到ThisWorkbook.Sheets.Count
工作表(工作表索引)。激活
如果是图纸(sheetIndex)。则命名为“Sheet2”
带图纸(图纸索引)。范围(“B:F”)
Set foundscan=.Find(What:=scanstring,LookIn:=xlValues,LookAt:=xlWhole_
SearchOrder:=xlByRows,SearchDirection:=xlNext_
MatchCase:=False,SearchFormat:=False)
以
如果foundscan什么都不是,那么
ASheet.Cells(rowNum,2).Value=“未找到”
其他的
'ASheet.Cells(rowNum,2).Value=foundscan.Rows.Count
如果结束
如果结束
下一个
下一行
结束子部分

一些要点:

  • -没必要
  • 你应该总是限定像这样的事情
    工作表
    范围
    ,否则Excel将使用活动工作簿/ 床单,这并不总是你想要的
  • 工作表
    工作表
    集合之间存在差异。例如,
    图表
    -工作表没有单元格,因此没有
    范围
  • 您正在声明变量
    lastRowIndex
    ,但使用
    lastRowInteger
    。为避免此类错误,请始终将
    Option Explicit
    放在代码顶部
把你的潜水艇换成

Sub Search_for_emails()

    Dim scanstring As String
    Dim foundscan As Range
    Dim lastRowIndex As Long, rowNum As Long
    Dim ASheet As Worksheet

    Set ASheet = ThisWorkbook.Worksheets("Sheet2")
    lastRowIndex = ASheet.Range("A1", ASheet.Range("A1").End(xlDown)).Rows.Count

    For rowNum = 1 To lastRowIndex
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name <> "Sheet2" Then
                With ws.Range("B:F")
                    Set foundscan = .Find(What:=scanstring, LookIn:=xlValues, LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False)
                End With
                If foundscan Is Nothing Then
                    ASheet.Cells(rowNum, 2).Value = "NOTFOUND"
                Else
                    ' ASheet.Cells(rowNum, 2).Value = foundscan.Rows.Count

                End If
            End If
        Next
    Next rowNum
End Sub
子搜索电子邮件()
作为字符串的字符串
Dim foundscan As范围
Dim lastRowIndex为长,rowNum为长
将ASheet设置为工作表
设置ASheet=ThisWorkbook.Worksheets(“Sheet2”)
lastRowIndex=ASheet.Range(“A1”,ASheet.Range(“A1”).End(xlDown)).Rows.Count
对于rowNum=1到lastRowIndex
将ws设置为工作表
对于此工作簿中的每个ws。工作表
如果ws.Name为“Sheet2”,则
具有ws.范围(“B:F”)
Set foundscan=.Find(What:=scanstring,LookIn:=xlValues,LookAt:=xlWhole_
SearchOrder:=xlByRows,SearchDirection:=xlNext_
MatchCase:=False,SearchFormat:=False)
以
如果foundscan什么都不是,那么
ASheet.Cells(rowNum,2).Value=“未找到”
其他的
'ASheet.Cells(rowNum,2).Value=foundscan.Rows.Count
如果结束
如果结束
下一个
下一行
端接头

+1,很好的解释!如果我能提出一个建议,当
notfoundscan is nothing
时应该有一个逃逸,以避免出现不必要的“NOTFOUND”。搜索应转到
下一个rowNum
。我只是假设OP忽略了这一点。