Excel VBA:未find.Range方法或数据成员

我有以下代码的问题,但是当我尝试Set FoundCell = .Range("A:A").Find(what:=StartDate1, lookat:=xlWhole)在不同的窗体中Set FoundCell = .Range("A:A").Find(what:=StartDate1, lookat:=xlWhole)不pipe用。

以下是有问题的完整代码:

我在那里比较Startdate来find范围吗?

 Private Sub CommandButton1_Click() Dim wsReport As Sheets Dim objEachSheet As Sheets Dim FoundCell As Range Dim iRow As Long Dim xRow As Long Dim i As Integer Dim Rw As Long Dim ERW As Long Dim x As Integer Dim y As Integer Dim FoundName As String Dim StartDate1 As String Dim LastDate1 As String Dim StartArray As Variant Dim EndArray As Variant Dim FoundArray As Variant Dim iColumn As Long StartDate1 = StartDate.Value LastDate1 = LastDate.Value StartArray() = Split(StartDate1, "-") EndArray() = Split(LastDate1, "-") x = 1 y = 20 Set wsReport = Sheets("MonthlyReport") 'changed For Each objEachSheet In Sheets With objEachSheet Set FoundCell = .Range("A:A").Find(what:=StartDate1, lookat:=xlWhole) If Not FoundCell Is Nothing And objEachSheet.Name <> "Stock" And objEachSheet.Name <> "MachineSales" And objEachSheet.Name <> "AddStock" And objEachSheet.Name <> "Balance" Then iRow = objEachSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1 Rw = FoundCell.Row FoundName = objEachSheet.Name FoundArray() = Split(objEachSheet.Cells(Rw, 1), "-") End If For x = 1 To 21 If objEachSheet.Name = wsReport.Cells(1, x) Then iColumn = x End If Next x For xRow = 0 To iRow If FoundArray(0) >= StartArray(0) And FoundArray(0) <= EndArray(0) And FoundArray(1) = StartArray(1) And FoundArray(2) = StartArray(2) Then Select Case FoundArray(0) Case "01" wsReport.Cells(2, iColumn) = objEachSheet.Cells(Rw, 5) Case "02" wsReport.Cells(3, iColumn) = objEachSheet.Cells(Rw, 5) Case "03" wsReport.Cells(4, iColumn) = objEachSheet.Cells(Rw, 5) Case "04" wsReport.Cells(5, iColumn) = objEachSheet.Cells(Rw, 5) Case "05" wsReport.Cells(6, iColumn) = objEachSheet.Cells(Rw, 5) Case "06" wsReport.Cells(7, iColumn) = objEachSheet.Cells(Rw, 5) Case "07" wsReport.Cells(8, iColumn) = objEachSheet.Cells(Rw, 5) Case "08" wsReport.Cells(9, iColumn) = objEachSheet.Cells(Rw, 5) Case "09" wsReport.Cells(10, iColumn) = objEachSheet.Cells(Rw, 5) Case "10" wsReport.Cells(11, iColumn) = objEachSheet.Cells(Rw, 5) Case "11" wsReport.Cells(12, iColumn) = objEachSheet.Cells(Rw, 5) Case "12" wsReport.Cells(13, iColumn) = objEachSheet.Cells(Rw, 5) Case "13" wsReport.Cells(14, iColumn) = objEachSheet.Cells(Rw, 5) Case "14" wsReport.Cells(15, iColumn) = objEachSheet.Cells(Rw, 5) Case "15" wsReport.Cells(16, iColumn) = objEachSheet.Cells(Rw, 5) Case "16" wsReport.Cells(17, iColumn) = objEachSheet.Cells(Rw, 5) Case "17" wsReport.Cells(18, iColumn) = objEachSheet.Cells(Rw, 5) Case "18" wsReport.Cells(19, iColumn) = objEachSheet.Cells(Rw, 5) Case "19" wsReport.Cells(20, iColumn) = objEachSheet.Cells(Rw, 5) Case "20" wsReport.Cells(21, iColumn) = objEachSheet.Cells(Rw, 5) Case "21" wsReport.Cells(22, iColumn) = objEachSheet.Cells(Rw, 5) Case "22" wsReport.Cells(23, iColumn) = objEachSheet.Cells(Rw, 5) Case "23" wsReport.Cells(24, iColumn) = objEachSheet.Cells(Rw, 5) Case "24" wsReport.Cells(25, iColumn) = objEachSheet.Cells(Rw, 5) Case "25" wsReport.Cells(26, iColumn) = objEachSheet.Cells(Rw, 5) Case "26" wsReport.Cells(27, iColumn) = objEachSheet.Cells(Rw, 5) Case "27" wsReport.Cells(28, iColumn) = objEachSheet.Cells(Rw, 5) Case "28" wsReport.Cells(29, iColumn) = objEachSheet.Cells(Rw, 5) Case "29" wsReport.Cells(30, iColumn) = objEachSheet.Cells(Rw, 5) Case "30" wsReport.Cells(31, iColumn) = objEachSheet.Cells(Rw, 5) Case "31" wsReport.Cells(32, iColumn) = objEachSheet.Cells(Rw, 5) End Select Next xRow End With Next objEachSheet End Sub 

button代码

  Private Sub CommandButton2_Click() Unload Me End Sub 

改变这个:

 Dim wsReport As Sheets 

对此:

 Dim wsReport As Worksheet 

objEachSheet做同样的objEachSheet

您需要将它们声明为单个工作表对象。 床单是一个集合。

另外,要明确在哪个工作簿中循环使用类似以下内容的工作簿:

 For Each objEachSheet In ActiveWorkbook.Worksheets 

你可以使用Activeworkbook.Sheets但是包括图表,没有范围可以search。