我有代碼(如下)在B列中找到Total
這個詞。然後它將選擇輸出到PDF。字Total
然後被替換爲Done
。如何做while while vba
我試圖找到一種方式來重複這個代碼,直到出現在B列中沒有更多的Total
Columns("B:B").Select
Selection.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(-1, -1).Activate
ActiveSheet.Range(Selection, Selection.End(xlUp)).Select
Selection.Resize(, 15).Select
Selection.Offset(, 1).Select
Dim rng As Range
With ActiveSheet
Set rng = Selection
.PageSetup.PrintArea = rng.Address
.PageSetup.Orientation = xlLandscape
.PageSetup.FitToPagesWide = 1
.PageSetup.FitToPagesTall = 999
.PageSetup.PrintTitleRows = "$1:$4"
.PageSetup.LeftMargin = Application.InchesToPoints(0.45)
.PageSetup.RightMargin = Application.InchesToPoints(0.2)
.PageSetup.TopMargin = Application.InchesToPoints(0.25)
.PageSetup.BottomMargin = Application.InchesToPoints(0.25)
.PageSetup.HeaderMargin = Application.InchesToPoints(0.3)
.PageSetup.FooterMargin = Application.InchesToPoints(0.3)
.PageSetup.PaperSize = xlPaperA4
.PageSetup.CenterHorizontally = True
.PageSetup.CenterVertically = False
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:Users\kgs-aizat.kassim\Desktop\" & ActiveCell.Offset(0, -1).Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
Columns("B:B").Select
Selection.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Value = "Done"
End Sub
謝謝所有的細胞。我非常感謝你的幫助。除了一個小問題,它現在工作得很好。當所有TOTAL已被替換爲DONE時,對TOTAL這個詞的最終搜索將作爲錯誤出現。我如何擺脫那個錯誤信息? –
我不'認爲你需要它,因爲所有找到的單元已被替換。你可以刪除它嗎?如果你因爲某種原因需要額外的位數,我相信你會在這裏得到一個異常,因爲調用不會返回一個範圍對象,它不會返回任何東西 - 因爲它找不到任何TOTAL數據。你需要設置一個對調用的引用並檢查Nothing,就像我在我的代碼中所做的那樣... Set rPtr = DataArea.Find(SearchItem,DataArea(DataArea.Count),XlFindLookIn.xlValues)如果不是rPtr是Nothing。 ..... – PaulG