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の初心者ですが、上記のコードについて指摘されたことがあれば幸いです。

4

3 に答える 3

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 に答える
0

OPが言及している行の前にエラーが発生します。

すべての変数を宣言し、モジュールの先頭に追加Option Explicitして、すべての変数が宣言されるようにする必要があります。

この行

Dim cityCells, sNameCells, sNumCells As Range

sNumCells を宣言するだけでRange、その他はバリアントになります。これは、次の行を意味します。

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

Rangeは、受信したエラーの 1 つを引き起こすではなく、バリアント配列を返します。

すべての変数を宣言すると、コードを実行できます(私にとって):

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 に答える