我試圖在工作表內搜索字符串並返回單元格地址,以便我可以使用該地址填充信息的相鄰單元格。我的問題是將「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
[這](http://www.siddharthrout.com/2011/07/14/ find-and-findnext-in-excel-vba /)應該清除你的概念:) –
@SiddharthRout我看了一下頁面,謝謝。我仍然掙扎在「Set Daycell」這一行的結果是「15/06/2017」,這是我正在尋找的字符串,而不是單元格地址。你能幫我嗎? –
'If Not Daycell is nothing Msgbox Daycell.Value' –