简体   繁体   English

对于VBA上的i循环功能,给出“类型不匹配”的编译器错误

[英]For i loop functions on VBA gives “type mismatch” compiler error

I am getting a compile error 'type mismatch' on my loop function below. 我在下面的循环函数中收到编译错误“类型不匹配”。 I am trying to combine multiple worksheets in a master worksheet and it would help to have a loop function through them. 我试图在一个主工作表中合并多个工作表,这将有助于通过它们的循环功能。 Any help would be appreciated. 任何帮助,将不胜感激。

Thanks, 谢谢,

Public Sub combine2()
    Dim i As Worksheet
    For i = 1 To Worksheet.Count

        Worksheets(i).Select
        Columns("A:B").Select
        Selection.copy
        Sheets("sheet").Select
        Selection.End(xlDown).Select
        Selection.End(xlUp).Select
        ActiveSheet.Paste
        Range("A1").Select
        Selection.End(xlDown).Select
        Selection.End(xlDown).Select
        Selection.End(xlUp).Select

    Next i
End Sub

well in the first line you declare the variable "i" as a object of type Worksheet, then you go and assign it a value of a number. 在第一行中,您将变量“ i”声明为Worksheet类型的对象,然后为它分配一个数字值。 Which you can't do because you specifically said it was a Worksheet - not a number type (such as Integer). 您不能执行此操作,因为您专门说这是一个工作表-不是数字类型(例如Integer)。 That is a type mismatch. 那是类型不匹配。

And also it should be worksheets - not worksheet 而且它应该是工作表-而不是工作表

Dim i As Integer
For i = 1 To Worksheets.Count

The code below will copy everything from columns A and B (except the captions) of all sheets (except Sheet1) to the first worksheet in the workbook. 下面的代码会将所有工作表(Sheet1除外)的A和B列(标题除外)中的所有内容复制到工作簿中的第一个工作表中。 You may have to tweak it a little to do what you actually want. 您可能需要稍微调整一下以执行您实际想要的操作。

Public Sub combine2()

    Dim Master As Worksheet
    Dim Ws As Worksheet
    Dim Rng As Range
    Dim Rl As Long
    Dim i As Integer

    Set Master = Worksheets(1)
    Application.ScreenUpdating = False
    For i = 2 To Worksheet.Count
        Set Ws = Worksheets(i)
        With Ws
            Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
            Set Rng = Range(.Cells(2, "A"), .Cells(Rl, "B"))
            Rng.Copy
        End With

        With Master
            Rl = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            .Cells(Rl, "A").PasteSpecial xlValues
        End With
    Next i
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
End Sub

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

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