在范围内查找string,并将单元格地址设置为新范围

我正在尝试search表单中的string,并返回单元格地址,以便我可以使用该地址填充信息的相邻单元格。 我的问题是设置“细胞”作为细胞参考,任何人都可以看到我要去哪里错了?

Private Sub Enter_Click() Dim FindWor As Range Dim Findrng As String Dim FindRowNumber As Long Dim i As Integer Dim frq As String Dim wkday As String Dim nwday As String Dim Mnth As String Dim Daycell As Range Dim entryno As String Dim Dayno As String frq = Me.No1 'Date from userform Findrng = Me.Date1 'Reoccuring interval from userform If Not Me.Freq1 = "No" Then Do While i < Me.No1 'This part adds the title and the detail to a data sheet with 3 possible entries per day With Worksheets("Data").Range("$A:$A") Set FindWor = .Find(What:=CDate(Findrng)) FindRowNumber = FindWor.Row 'Checks for the 1st empty row and populates If .Cells(FindRowNumber, 2) = "" Then .Cells(FindRowNumber, 2).Value = Me.Event1 .Cells(FindRowNumber, 3).Value = Me.Details1 entryno = 1 ElseIf .Cells(FindRowNumber, 4) = "" Then .Cells(FindRowNumber, 4).Value = Me.Event1 .Cells(FindRowNumber, 5).Value = Me.Details1 entryno = 2 ElseIf .Cells(FindRowNumber, 6) = "" Then .Cells(FindRowNumber, 6).Value = Me.Event1 .Cells(FindRowNumber, 7).Value = Me.Details1 entryno = 3 End If 'This part is to enter the detail part into the cell within the correct monthly tab as a comment Mnth = (Month((CDate(Findrng)))) Mnth = MonthName(Mnth, True) With Worksheets(Mnth).Range("$A:$Z") Dayno = Day(CDate(Findrng)) Set Daycell = .Find(What:=Dayno, LookIn:=xlValues, Lookat:=xlWhole, _ MatchCase:=False, SearchFormat:=False) 'This is where I think my issue is as I want the comment to be in the cell below the day number (the days are displayed as day number only .Cell(Daycell).Offset (1) 'I am not sure if this will work, I want the comment to go into the corresponding cell as stated by the entryno that is created above If .Cells(Daycell) = "" Then Range(Daycell).NoteText Text:=Me.Details1.Value Else: .Cell(Daycell).Offset (entryno) End If End With 'this part then loops based upon the frq and the no1 from the userform If Me.Freq1 = "Daily" Then Findrng = Findrng + 1 ElseIf Me.Freq1 = "Weekly" Then Findrng = Findrng + 7 ElseIf Me.Freq1 = "Bi-Weekly" Then Findrng = Findrng + 14 'the parts below check for the correct working day ElseIf Me.Freq1 = "Monthly" Then wkday = Format(CDate(DateAdd("M", 1, (CDate(Findrng)) + 1)), "MM/DD/YYYY") bday = DateAdd("D", -1, ((Format(wkday, "DD/MM/YYYY")))) nwday = CDate(Application.WorksheetFunction.WorkDay(wkday, -1)) Findrng = bday ElseIf Me.Freq1 = "Quaterly" Then wkday = CDate(Format(DateAdd("M", 3, (CDate(Findrng))) + 1, "MM/DD/YYYY")) bday = DateAdd("D", -1, ((Format(wkday, "DD/MM/YYYY")))) nwday = CDate(Application.WorksheetFunction.WorkDay(wkday, -1)) Findrng = bday ElseIf Me.Freq1 = "6 Monthly" Then wkday = CDate(Format(DateAdd("M", 6, (CDate(Findrng))) + 1, "MM/DD/YYYY")) bday = DateAdd("D", -1, ((Format(wkday, "DD/MM/YYYY")))) nwday = CDate(Application.WorksheetFunction.WorkDay(wkday, -1)) Findrng = bday ' Findrng = FindRowNumber + ((CDate(bday) - CDate(Findrng))) ElseIf Me.Freq1 = "Yearly" Then wkday = CDate(Format(DateAdd("M", 12, (CDate(Findrng))) + 1, "MM/DD/YYYY")) bday = DateAdd("D", -1, ((Format(wkday, "DD/MM/YYYY")))) nwday = CDate(Application.WorksheetFunction.WorkDay(wkday, -1)) Findrng = bday ' Findrng = FindRowNumber + ((CDate(bday) - CDate(Findrng))) End If i = i + 1 End With Loop End If Unload Me End Sub 

@SiddharthRout对不起,我不想要一个消息框,我想用单元格地址来填充该单元格的评论。 我怎样才能find单元格地址/范围使用Set Daycell线? – 史蒂文·克雷格1分钟前

你说你想要的价值,因此我给了这个例子。 如果你想在该单元格中插入注释,那么你将不得不这样做( 未经testing

 Dim Daycell As Range With Range("A1:A100") Set Daycell = .Find(What:=Dayno, LookIn:=xlValues, Lookat:=xlWhole, _ MatchCase:=False, SearchFormat:=False) If Not Daycell Is Nothing Then '<~~ Check if a match was found With Daycell .AddComment .Comment.Visible = False .Comment.Text Text:="This is a sample comment" End With End If End With 

试试这个方法

  Dim Daycell As Range With Range("A1:A100") Set Daycell = .Find(What:=Dayno, LookIn:=xlValues, MatchCase:=False, SearchFormat:=False) If Not Daycell Is Nothing Then MsgBox Daycell.Column MsgBox Daycell.Row End If End With 

请参阅https://msdn.microsoft.com/en-us/library/office/ff839746.aspx