2017-08-25 41 views
0

我正在使用Excel並填寫一個單詞模板(之前從未做過)。
首先,我正在搜索特定條目的列,然後創建一個具有行特定值的bulletpoint。這裏是代碼:ApplyBullet默認不應用於Do-Loop

With wd.Selection 
    .GoTo what:=wdGoToBookmark, Name:="launches" 

    'Loop until cycled through all unique finds 
    Do Until foundCells Is Nothing 

    .Range.ListFormat.ApplyBulletDefault '<----- this doesn't work 

    'Find next cell with value 
    Set foundCells = Sh.Columns(2).FindNext(After:=foundCells) 
    Name = foundCells.Offset(0, 3).Value & Chr(11) 
    action = foundCells.Offset(0, 5).Value & Chr(11) 
    .TypeText (Name & " " & action) 

    'Test to see if cycled through to first found cell 
     If foundCells.Address = FirstFound Then Exit Do 
Loop 
End With 

如上所述,我想爲我找到的每個細胞創建一個bulletpoint。但如果它在Do循環中,突出顯示的行不起作用,但在循環外運行......問題在哪裏?

編輯: 輸入上述前,我做了不存在的值以下檢查:

Set foundCells = Sh.Columns(2).Find(what:=month) 
'Test to see if anything was found 
If Not foundCells Is Nothing Then 
    FirstFound = foundCells.Address 
Else 
    GoTo NothingFound 
End If 

所以不應該是關於不存在的值的任何問題(我希望)

+0

可以在(opt1)之前或之前(opt2)「Do Until foundCells Is Nothing」之後放置一行「msgBox(foundCells Is Nothing)」嗎? opt1:如果沒有彈出消息框,你的foundCells變量就什麼都沒有。 opt2:messageBox將顯示「true」或「false」。如果它是真的,foundCells是沒有,如果錯誤,那麼它應該進入循環,我們會繼續找到你的問題...;) – Kathara

+0

@Kathara Thx的答案 - 我想我要覆蓋這...看編輯 – dv3

+0

好吧,你是如何定義foundCells的?作爲範圍?作爲細胞? – Kathara

回答

1

你可以試試這個,告訴我它是否有效?

With wd.Selection 
    .GoTo what:=wdGoToBookmark, Name:="launches" 

    .Range.ListFormat.ApplyBulletDefault '<----- this doesn't work  

    'Loop until cycled through all unique finds 
    Do Until foundCells Is Nothing 

    'Find next cell with value 
    Set foundCells = Sh.Columns(2).FindNext(After:=foundCells) 
    Name = foundCells.Offset(0, 3).Value & Chr(11) 
    action = foundCells.Offset(0, 5).Value & Chr(11) 
    .TypeText (Name & " " & action & vbNewLine) 

    'Test to see if cycled through to first found cell 
     If foundCells.Address = FirstFound Then Exit Do 
Loop 

End With 
+0

大聲笑這實際上起作用.... vbNewLine救援 - Thx! – dv3

+0

我很高興能幫到;) – Kathara