2017-06-15 34 views
0

我試圖在工作表內搜索字符串並返回單元格地址,以便我可以使用該地址填充信息的相鄰單元格。我的問題是將「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

[這](http://www.siddharthrout.com/2011/07/14/ find-and-findnext-in-excel-vba /)應該清除你的概念:) –

+0

@SiddharthRout我看了一下頁面,謝謝。我仍然掙扎在「Set Daycell」這一行的結果是「15/06/2017」,這是我正在尋找的字符串,而不是單元格地址。你能幫我嗎? –

+0

'If Not Daycell is nothing Msgbox Daycell.Value' –

回答

2

@SiddharthRout對不起,我不希望得到一個消息框,我想使用的單元格地址來填充細胞並發表評論。我如何使用Set Daycell行來查找單元格地址/範圍? - 史蒂芬克雷格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 
+0

這工作,謝謝。如果我想向「Daycell」添加1行,我將如何處理這個問題? –

+1

試試吧。我已經向你展示了一個例子。你不能在一篇文章中提出你所有的問題;)你從某些事情開始,然後結束其他事情。 –

+0

不同的問題=不同的帖子;) –

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

+0

'LookAt:= xlValues'我猜猜錯了嗎? :) –

+1

哎呀..感謝Siddharth整頓 – Maddy

+0

感謝這一點,但它現在返回錯誤9? –

相關問題