简体   繁体   English

VBA 循环遍历行然后列

[英]VBA loop through rows then columns

I'm VERY new to VBA so be gentle...我对 VBA 很陌生,所以要温柔...

I'm attempting to automate the creation of a new tab in multiple workbooks.我正在尝试在多个工作簿中自动创建新选项卡。 Every week, a new tab is manually created for the upcoming weekend.每周,都会为即将到来的周末手动创建一个新选项卡。

I can get the code to search through a column to find the weekend that needs to be created, but then I would like it to move to the next column and do the same thing.我可以获取代码来搜索一列以查找需要创建的周末,但随后我希望它移动到下一列并执行相同的操作。 On one tab of the "creator" workbook I have the weekends (Dates), on another I have the list of separate workbooks (Books) that will need a new tab created.在“创建者”工作簿的一个选项卡上,我有周末(日期),在另一个选项卡上,我有需要创建新选项卡的单独工作簿(书籍)的列表。 The list of separate workbooks is grabbed by another script, then printed on both the Dates (in row 2 starting at C2) and Books (in column A starting in row 2) tabs.单独的工作簿列表由另一个脚本抓取,然后打印在日期(从 C2 开始的第 2 行)和书籍(从第 2 行开始的 A 列中)选项卡上。 I have attached an example of the "creator" workbook I'm using.我附上了我正在使用的“创建者”工作簿的示例。 The newly created tab will should be renamed to the correct weekend.新创建的选项卡应重命名为正确的周末。 Example of Dates tab日期选项卡示例

Example of Books tab图书选项卡示例

Here is the code I'm using.这是我正在使用的代码。 It will created the first new tab, but stops after that.它将创建第一个新选项卡,但在那之后停止。

Sub createNewTab()
    Dim wB As Workbook
    Dim wS As Worksheet
    Dim bK As Worksheet
    Dim r As Long
    Dim i As Long
    Dim c As Long
    Dim fPath As String
    Dim lastRow As Long
    Dim lastDate As Long
    Dim bDate As String
    Dim eDate As String
    Dim created As Long
    Dim newBook As Workbook
    Dim lDate As String
    Dim nDate As String
    Dim nBDate As String
    Dim nEDate As String
    Dim allSheets As Long


Set wB = Workbooks("Example Sheet Creator")
Set wS = wB.Sheets("Dates")
Set bK = wB.Sheets("Books")
lastRow = bK.Cells(Rows.Count, "A").End(xlUp).Row
lastDate = wS.Cells(Rows.Count, "C").End(xlUp).Row
allSheets = wS.Cells(2, Columns.Count).End(xlToLeft).Column


For c = 3 To allSheets
 Do While c <= allSheets
    For r = 2 To lastRow

        If r <= lastRow And wS.Cells(2, c).Value = bK.Cells(r, 1).Value Then

            For i = 3 To lastDate

                While i <= lastDate And wS.Cells(i, 3).Text = "n"

                    bDate = Format(wS.Cells(i - 1, 1).Value, "mmm dd")
                    eDate = Format(wS.Cells(i - 1, 2).Value, "mmm dd")
                    nBDate = Format(wS.Cells(i, 1).Value, "mmm dd")
                    nEDate = Format(wS.Cells(i, 2).Value, "mmm dd")
                    fPath = bK.Cells(r, 2).Value
                    Application.ScreenUpdating = False
                    lDate = bDate & " - " & eDate
                    nDate = nBDate & " - " & nEDate

                    Set newBook = Workbooks.Open(fPath)


                    newBook.Sheets("Mar 14 - Mar 15").Copy After:=Worksheets(Sheets.Count)
                    'On Error Resume Next
                    ActiveSheet.Name = nDate


                    wS.Cells(i, 3).Value = "Y"

                    i = i + 1
                Wend

            Next i

            r = r + 1

        End If

    Next r

c = c + 1
Loop
Next c

End Sub

Ideally, it would match the name in row 2 on Dates to the list in column A on Books.理想情况下,它会将 Dates 上第 2 行中的名称与 Books 上 A 列中的列表相匹配。 In the future, the name/order of the separate workbooks listed on the Books tab might change.将来,“书籍”选项卡上列出的单独工作簿的名称/顺序可能会更改。

This uses 2 loops.这使用了 2 个循环。 Scan down the books sheet and for each book scan down the dates sheets.向下扫描书籍表,并为每本书向下扫描日期表。 It checks the tab does not exist before creating it.它在创建之前检查选项卡不存在。

Sub createNewTab()

    Const COPY_SHEET = "Mar 14 - Mar 15"

    Dim wb As Workbook, ws As Worksheet, wsDates As Worksheet, wsBooks As Worksheet
    Dim wbTarget As Workbook, wsTarget As Worksheet
    Dim rng As Range, rngDates As Range
    Dim iBookRow As Long, iLastBook As Long
    Dim iDateRow As Long, iLastDate As Long
    Dim iNameCol As Long, sYesNo As String, bOK As Boolean

    Set wb = ThisWorkbook
    Set wsBooks = wb.Sheets("Books")
    Set wsDates = wb.Sheets("Dates")

    iLastBook = wsBooks.Cells(Rows.count, 1).End(xlUp).Row
    iLastDate = wsDates.Cells(Rows.count, 1).End(xlUp).Row
    Set rngDates = wsDates.Rows(2)
    Debug.Print iLastBook, iLastDate, rngDates.Address

    Dim sName As String, sFilename As String, sTab As String
    Dim count As Long, countWb As Long, n As Integer

    ' scan down the books sheet
    For iBookRow = 2 To iLastBook

        sName = wsBooks.Cells(iBookRow, 1)
        sFilename = wsBooks.Cells(iBookRow, 2)
        'Debug.Print sName, sFilename

        ' find column for this name on Dates
        Set rng = rngDates.Find(sName)
        If rng Is Nothing Then
           MsgBox "Could not find " & sName & " on Dates sheet", vbExclamation
           GoTo Skip
        End If
        iNameCol = rng.Column

        ' open target workbook
        Set wbTarget = Workbooks.Open(sFilename)
        countWb = countWb + 1

        ' scan down dates for sName
        For iDateRow = 3 To iLastDate
            sYesNo = wsDates.Cells(iDateRow, iNameCol)
            sTab = Format(wsDates.Cells(iDateRow, 1), "mmm dd") & " - " & _
                   Format(wsDates.Cells(iDateRow, 2), "mmm dd")

            ' is create needed
            If sYesNo <> "Y" Then

                ' check sheet doesn't exist
                bOK = True
                For Each ws In wbTarget.Sheets
                   If ws.Name = sTab Then bOK = False
                Next

                 ' create sheet
                If bOK Then
                    With wbTarget
                        n = .Sheets.count
                        .Sheets(COPY_SHEET).Copy After:=.Sheets(n)
                        .Sheets(n + 1).Name = sTab
                    End With
                    wsDates.Cells(iDateRow, iNameCol) = "Y"
                    count = count + 1
                Else
                    MsgBox sTab & " already exists in " & wbTarget.Name, vbExclamation
                    wsDates.Cells(iDateRow, iNameCol) = "Y"
                End If

            End If

            'Debug.Print sName, iDateRow, sYesNo, sTab
        Next
        wbTarget.Close True
Skip:
   Next

   MsgBox count & " sheets created in " & countWb & " workbooks", vbInformation

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM