首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何执行while循环vba

如何执行while循环vba
EN

Stack Overflow用户
提问于 2015-04-30 16:38:51
回答 3查看 831关注 0票数 1

我编写的代码(如下所示)在B列中找到单词Total,然后将选择的内容导出到PDF。然后,单词Total将被替换为Done

我正在尝试找到一种方法来重复此代码,直到B列中没有更多的Total

代码语言:javascript
运行
复制
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
EN

回答 3

Stack Overflow用户

发布于 2015-04-30 17:17:07

下面的代码将在列B中搜索SearchItem的所有条目。您需要在其中包含对PDF处理的调用。

顺便说一句,如果您将单元格内容更改为“Done”,以查看是否没有更多的单元格需要处理,则不需要这样做。如果注释掉这一行:

rPtr.Value = ReplaceItem

代码仍然只会找到单元格一次。

代码语言:javascript
运行
复制
Option Explicit

Sub test()

Dim rData As Range
Set rData = Sheets(1).Range("B:B")
Call ReplaceContents("Test", "Test1", rData)

End Sub

Public Sub ReplaceContents(ByVal SearchItem As String, ByVal ReplaceItem As String, ByVal DataArea As Range)

Dim rPtr As Range
Dim sFirstCell As String
Dim bFinished As Boolean

Set rPtr = DataArea.Find(SearchItem, DataArea(DataArea.Count), XlFindLookIn.xlValues)
If Not rPtr Is Nothing Then
    sFirstCell = rPtr.Address
    Do While bFinished = False
        rPtr.Value = ReplaceItem
        Set rPtr = DataArea.FindNext(rPtr)
        If StrComp(rPtr.Address, sFirstCell, vbTextCompare) = 0 Then bFinished = True
    Loop
End If

End Sub
票数 1
EN

Stack Overflow用户

发布于 2015-04-30 16:44:25

看一看http://www.excel-easy.com/vba/loop.html

你需要做的就是点击上面的链接。然后,您将获得列"B“中使用的总行数,并将其用作for循环的结尾。

所以基本上会是这样的

代码语言:javascript
运行
复制
For i = 2 to columnBCount
    do code.......
next

您只需将columnBCount替换为实际的方法即可获得计数。

我已经将i设置为2,就像你有标题一样,这将不包括它们,并从第二行开始。

但请阅读链接中的循环

票数 0
EN

Stack Overflow用户

发布于 2015-04-30 17:30:00

我看到你在使用'find‘命令,你可以使用'findnext’

代码语言:javascript
运行
复制
Dim rng As Range
With ActiveSheet
    set c = .Columns("B:B").Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Offset(-1, -1)

    if c is not nothing then
        firstaddress  = c.address
     do
        c.select
    .Range(Selection, Selection.End(xlUp)).Select
    Selection.Resize(, 15).Select
    Selection.Offset(, 1).Select

    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

    loop While Not c Is Nothing And c.Address <> firstAddress 
End if 
end with
 End Sub

这将遍历所有符合条件的单元格

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29962933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档