1

我一直在尝试Do While在工作簿中的多个工作表中运行相同的循环函数,并在另一个工作表中编译数据。该代码适用于指定的一个工作表,但我如何让它同时在工作簿中的其他工作表中工作?

还值得一提的是,我只希望它在一些工作表上运行,而不是在工作簿中的所有工作表上运行(工作表被命名为年份 - 2014、2015 等)。

这是代码

Sub Total_Button1_Click()

    Dim i As Integer

    Dim strSheetFrom As String

    Dim j As Integer
    Dim strSheetTo As String
    
    
    i = 3
    j = 2
    
    strSheetFrom = "2014"
    strSheetTo = "Total"
        
    Do While Trim(Sheets(strSheetTo).Range("B" & CStr(j)).Text) <> ""
        j = j + 2
    Loop

    Do While Trim(Sheets(strSheetFrom).Range("B" & CStr(i)).Text) <> ""
        If UCase(Trim(Sheets(strSheetFrom).Range("A" & CStr(i)).Text)) = "Y" Then
            Sheets(strSheetTo).Range("B" & j & ":G" & j).Value = Sheets(strSheetFrom).Range("B" & i & ":G" & i).Value
            Sheets(strSheetTo).Range("H" & j & ":I" & j).Value = Sheets(strSheetFrom).Range("I" & i & ":J" & i).Value
            Sheets(strSheetTo).Range("J" & j & ":J" & j).Value = Sheets(strSheetFrom).Range("L" & i & ":L" & i).Value
            Sheets(strSheetTo).Range("K" & j & ":K" & j).Value = Sheets(strSheetFrom).Range("Q" & i & ":Q" & i).Value
            Sheets(strSheetTo).Range("L" & j & ":AH" & j).Value = Sheets(strSheetFrom).Range("s" & i & ":AO" & i).Value
            j = j + 1
        End If
        i = i + 1
    Loop
       
    MsgBox "Total book created"


End Sub
4

2 回答 2

1

尝试使您的 strSheetFrom 变量成为这样的数组:

strSheetFrom = new strSheetFrom[3] 
strSheetFrom[2] = "2012"
strSheetFrom[1] = "2013" 
strSheetFrom[0] = "2014"

然后将您的代码放入另一个循环,如下所示:

dim w as integer
for w = 0 To 3

Do While Trim(Sheets(strSheetTo).Range("B" & CStr(j)).Text) <> ""
j = j + 2
Loop

Do While Trim(Sheets(strSheetFrom[w]).Range("B" & CStr(i)).Text) <> ""
If UCase(Trim(Sheets(strSheetFrom[w]).Range("A" & CStr(i)).Text)) = "Y" Then
    Sheets(strSheetTo).Range("B" & j & ":G" & j).Value = Sheets(strSheetFrom[w]).Range("B" & i & ":G" & i).Value
    Sheets(strSheetTo).Range("H" & j & ":I" & j).Value = Sheets(strSheetFrom[w]).Range("I" & i & ":J" & i).Value
    Sheets(strSheetTo).Range("J" & j & ":J" & j).Value = Sheets(strSheetFrom[w]).Range("L" & i & ":L" & i).Value
    Sheets(strSheetTo).Range("K" & j & ":K" & j).Value = Sheets(strSheetFrom[w]).Range("Q" & i & ":Q" & i).Value
    Sheets(strSheetTo).Range("L" & j & ":AH" & j).Value = Sheets(strSheetFrom[w]).Range("s" & i & ":AO" & i).Value
    j = j + 1
End If
i = i + 1
Loop

w -= 1
next

我没有测试过,但类似的东西。你明白了。

于 2013-09-18T21:22:43.683 回答
0

使用 For Each 并像这样遍历 Worksheet 集合

'Variables
Dim useWorkSheet As Worksheet
Dim totalWorkSheet As Worksheet
Dim yearAsNumeric As Integer
Dim startingYear As Integer

'Settings
startingYear = 2014

'To reference the total worksheet so we can work with it
Set totalWorkSheet = ActiveWorkbook.Worksheets("Total")

'Iterate over each item in the collection
For Each useWorkSheet In ActiveWorkbook.Worksheets
    'Force the name into a numeric value.  If it starts with anything non numeric (A-Z|a-z|$,#,etc) then it will return 0
    yearAsNumeric = Val(useWorkSheet.Name)

    'Greater than or equal to the year we want to start with?
    If yearAsNumeric >= startingYear Then
        'Yes. Do your stuff here
        useWorkSheet.Name
    End If
Next
于 2013-09-18T21:12:09.297 回答