VBA循环excel工作表

VBA循环excel工作表,excel,vba,Excel,Vba,我实际上是一个vba初学者,并创建了我的第一个代码,但我需要做得更灵活。。。 当我使用makro时,我想选择更多excel表格。实际上,我只使用名为“扇区共享”的表,但如何为选定的表运行相同的makro,例如“表1”、“表5”和其他?此外,是否有一种方法可以计算cloumn中填充的行数?然后我可以创建一个灵活的范围,而不是范围(“A400”)和范围(“A4:A400”) 我在这里做了什么 我想在每月的scala上设置年度数据。我在填充行之间创建了11个空行,并从名为“MSCI”的工作表中复制了月

我实际上是一个vba初学者,并创建了我的第一个代码,但我需要做得更灵活。。。 当我使用makro时,我想选择更多excel表格。实际上,我只使用名为“扇区共享”的表,但如何为选定的表运行相同的makro,例如“表1”、“表5”和其他?此外,是否有一种方法可以计算cloumn中填充的行数?然后我可以创建一个灵活的范围,而不是范围(“A400”)和范围(“A4:A400”)

我在这里做了什么

我想在每月的scala上设置年度数据。我在填充行之间创建了11个空行,并从名为“MSCI”的工作表中复制了月度比例。最后一步是用NA填充空单元格(以每行11个NA的方式为一个值)

提前谢谢 勒内

使用@MatthewD的信息进行编辑:

' I understand iIndex as number of the excel sheet and it is possible to 
' adress the correct sheet with it but how can I use here more sheets that I
' can enter only the Number (or Name) of the sheet e.g. iIndex = 1,2,3,7,9,15 


Dim lRow As long
Dim iIndex As long
For iIndex = 1 To ActiveWorkbook.Worksheets.count
    Set ws = Worksheets(iIndex)
    For ws = Range("A400").End(xlUp).Row To 5 Step -1
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
    Next i

' I hope that I set the maximum lenght with "ws.UsedRange.Rows.count" 
' and copy it row by row
   For lRow = 1 To ws.UsedRange.Rows.count
     Sheets("MSCI").Range("1:lRow").Copy
     Sheets("banking_sector").Range("1:lRow").PasteSpecial
   Next lRow

' I think that this is quiet okay (I recorded it only), it should 
' select the hole data and replace the NA's
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Replace What:="", Replacement:="NA", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

这是一个将循环工作表并按名称激活一个工作表的子工作表

Dim ws As Excel.Worksheet
Private Sub loopsheets(strSheetName As String)
    iFoundWorksheet = 0
    For iIndex = 1 To ActiveWorkbook.Worksheets.count
        Set ws = Worksheets(iIndex)
        If UCase(ws.Name) = UCase(strSheetName) Then
            iFoundWorksheet = iIndex
            Exit For
        End If
    Next iIndex
    If iFoundWorksheet = 0 Then
        MsgBox "No worksheet was found with the name RESULTS (this is not case sensetive). Aborting."
    End If
    Set ws = ea.Worksheets(iFoundWorksheet)
    ws.Activate

End Sub
或者只是在床单上绕一圈做点什么

For iIndex = 1 To ActiveWorkbook.Worksheets.count

    Set ws = Worksheets(iIndex)
    'Do something here.

    'Or call you sub
    Year_to_Month

Next iIndex
与使用范围(“A400”)和范围(“A4:A400”)相比,usedRange是一种更好的循环浏览表单内容的方法

要检查是否有填充物,您可以这样做

If  ws.cells(RowIndex, ColIndex).Interior.ColorIndex > 0 then
     'We have a filled cell
End if
您可以按名称或索引对工作表进行寻址

Set ws = Worksheets(3)


您可以录制一个宏,然后重复该流程。按f8键逐行运行。。如果这样做,您可能会更好地理解它。

我尝试实现循环,但是当我设置以下代码时,我在带值的行之间得到了大约1000行。我需要更改什么才能使其正确。我认为我使用了错误的代码行。你能把它用在一张纸上吗?谢谢,哪个循环?用上面的问题发布您当前的代码。我们可以使用你现在拥有的。我添加了新的,不起作用的代码。如果你能这么好,看看它,给我一个提示,我的错误在哪里。
If  ws.cells(RowIndex, ColIndex).Interior.ColorIndex > 0 then
     'We have a filled cell
End if
Set ws = Worksheets(3)
Set ws = ActiveWorkbook.Sheets("Sheet1")