VBA搜索工作表的值并将其写入单元格

时间:2017-06-29 13:38:57

标签: vba

我尝试了很多,但我找不到错误。我很确定它只是一个小的......

我想搜索除“Fileupload”表之外的所有工作表的特定列,以获取Fileupload工作表中的值。如果它在特定范围内,则应将值写入单元格中。 它抛出对象变量或with-block变量未确定的错误。或者它崩溃了......

Sub forEachWs()
        Set FileUpload = Worksheets("FileUpload")
        Dim ws As Worksheet
        Dim FileUpload As Worksheet

        cntsheet = 0

        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> "FileUpload" Then
                For i = 1 To ws.Range("A3").End(xlDown).Row
                    For j = 1 To worksheets("FileUpload").Range("E9").End(xlDown).Row
                        If ws.Cells(1, 2 + i).Value < ((FileUpload.Cells(5, j + 8).Value) + (FileUpload.Cells(3, 5).Value) * 0.001) And ws.Cells(1, 2 + i).Value > ((FileUpload.Cells(5, j + 8).Value) - ((FileUpload.Cells(3, 5).Value) * 0.001)) Then
                            Worksheets("FileUpload").Cells(7 + cntsheet, j + 8) = ws.Cells(i, 1).Value
                        End If
                    Next j
                Next i
                cntsheet = cntsheet + 1
            End If
        Next ws
End Sub

1 个答案:

答案 0 :(得分:1)

你需要添加......

Set FileUpload = Worksheets("FileUpload")

......第一行之后?即:

Sub forEachWs()
    Dim FileUpload As Worksheet
    Set FileUpload = Worksheets("FileUpload")
    Dim ws As Worksheet

    cntsheet = 0

    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "FileUpload" Then
            For i = 1 To ws.Range("A3").End(xlDown).Row
                For j = 1 To Worksheets("FileUpload").Range("E9").End(xlDown).Row
                    If ws.Cells(1, 2 + i).Value < ((FileUpload.Cells(5, j + 8).Value) + (FileUpload.Cells(3, 5).Value) * 0.001) And ws.Cells(1, 2 + i).Value > ((FileUpload.Cells(5, j + 8).Value) - ((FileUpload.Cells(3, 5).Value) * 0.001)) Then
                        Worksheets("FileUpload").Cells(7 + cntsheet, j + 8) = ws.Cells(i, 1).Value
                    End If
                Next j
            Next i
            cntsheet = cntsheet + 1
        End If
    Next ws
End Sub