我正在尝试在工作表中搜索字符串并返回单元格地址,以便我可以使用该地址用信息填充相邻单元格。我的问题是设置" Daycell"作为细胞参考,任何人都可以看到我出错的地方吗?
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
答案 0 :(得分:2)
@SiddharthRout很抱歉,我不打算收到消息框,我想使用单元格地址来填充带有注释的单元格。如何使用Set Daycell线找到单元格地址/范围? - Steven Craig 1分钟前
你说你想要价值,因此我举了那个例子。如果你想在那个单元格中插入评论,那么你必须这样做(未经测试)
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
答案 1 :(得分:1)
试试这种方式
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