0
    Sub combine()
    Dim inName, inNum, inCity As String
    Dim IncNum As Integer
    Dim temp As Range
    Dim lrow As Long
    Dim counter As Integer

    Dim cityCells, sNameCells, sNumCells As Range

    cityCells = Sheets("Sheet2").UsedRange.Columns(1).Cells

    For Each Cell In cityCells
        If Cell <> "" And Cell.Row <> 1 Then
            inCity = Cell.Value
            inName = Sheets("Sheet2").Cells(Cell.Row, 2)
            inNum = Sheets("Sheet2").Cells(Cell.Row, 3)

            Set temp = Sheets("Sheet1").Columns(1).Find(what:=inCity)

            If temp Is Nothing Then
            'find the last row of the existing sheet
                lrowEx = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                IncNum = Sheets("Sheet2").UsedRange.Columns.Count

                For counter = 1 To IncNum
                    Sheets("Sheet1").Cells(lrow + 1, counter) = Cells(Cell.Row, counter)
                Next counter

            End If

        End If

       Next

       End Sub

我从 Sheets("Sheet1").Cells(lrow + 1, counter) = Cells(Cell.Row, counter) 行中收到 object required 错误,有什么帮助吗?

我是vba btw的新手,任何关于上述代码的内容将不胜感激。

4

3 回答 3

1

I can't make comments, so I'll leave an answer, but are you sure lRow is returning a value? You could be trying to do:

Sheets("Sheet1").Cells(NULL + 1, counter) = Cells(Cell.Row, counter) 

Also Try

Sheets("Sheet1").Cells(lrow + 1, counter).Value = Cells(Cell.Row, counter) 

ALSO

Your Cells(Cell.Row,Counter) is not part of a worksheet object

于 2013-07-22T19:44:48.947 回答
0

我在 OP 提到的行之前收到错误。

您应该声明所有变量并Option Explicit在模块顶部添加以确保声明所有变量。

这条线

Dim cityCells, sNameCells, sNumCells As Range

仅将 sNumCells 声明为Range其他将是变体。这意味着这一行:

cityCells = Sheets("Sheet2").UsedRange.Columns(1).Cells

返回一个 Variant-array,而不是Range导致我收到的错误之一。

声明所有变量允许代码运行(对我来说):

Dim lRowEx As Long
Dim Cell As Range

Dim cityCells As Range, sNameCells As Range, sNumCells As Range

Set cityCells = Sheets("Sheet2").UsedRange.Columns(1).Cells

For Each Cell In cityCells
于 2013-07-22T19:50:53.190 回答
0

这是我的解决方案。我添加了另一个循环,你可以忽略它。@埃利亚斯

    Sub combine()
    Dim inName, inNum, inCity As String
    Dim IncNum, inRow, ExcNum As Integer
    Dim temp As Range
    Dim lrowEx As Long
    Dim counter1, counter2 As Integer


    Dim cityCells, sNameCells, sNumCells As Range

    IncNum = Sheets("Sheet2").UsedRange.Columns.Count
    ExcNum = Sheets("Sheet1").UsedRange.Columns.Count

    If IncNum > ExcNum Then
        'find the last column of existing sheet and input sheet
        lcolEx = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        lcolIn = Sheets("Sheet2").Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

        For counter1 = lcolEx + 1 To lcolIn
          Sheets("Sheet1").Cells(1, lcolEx + 1) = Sheets("Sheet2").UsedRange.Rows(1).Cells(1, lcolEx + 1)
        Next counter1

    End If

    For Each cell In Sheets("Sheet2").UsedRange.Columns(1).Cells
        If cell <> "" And cell.Row <> 1 Then
            inCity = cell.Value
            inName = Sheets("Sheet2").Cells(cell.Row, 2)
            inNum = Sheets("Sheet2").Cells(cell.Row, 3)
            inRow = cell.Row

            Set temp = Sheets("Sheet1").Columns(1).Find(what:=inCity)

            If temp Is Nothing Then
            'find the last row of the existing sheet
                lrowEx = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

                For counter = 1 To IncNum
                    Sheets("Sheet1").Cells(lrowEx + 1, counter) = Sheets("Sheet2").UsedRange.Columns(1).Cells(cell.Row, counter)
                Next counter

            End If

        End If

    Next

End Sub
于 2013-07-23T19:59:46.467 回答