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
我从表格(“Sheet1”)中得到一个对象需要的错误。单元格(lrow + 1,counter)= Cells(Cell.Row,counter)行,有什么帮助吗?
我是vba btw的新手,任何指出上述代码的事情都会受到赞赏。
答案 0 :(得分:1)
我无法发表评论,所以我会留下答案,但你确定lRow正在返回一个值吗?你可能试图这样做:
Sheets("Sheet1").Cells(NULL + 1, counter) = Cells(Cell.Row, counter)
同时尝试
Sheets("Sheet1").Cells(lrow + 1, counter).Value = Cells(Cell.Row, counter)
ALSO
您的Cells(Cell.Row,Counter)
不属于工作表对象
答案 1 :(得分:0)
我在OP提到的行之前收到错误。
您应声明所有变量并在模块顶部添加Option Explicit
以确保声明所有变量。
这一行
Dim cityCells, sNameCells, sNumCells As Range
仅将sNumCells声明为Range
,其他人将为Variants。这意味着这一行:
cityCells = Sheets("Sheet2").UsedRange.Columns(1).Cells
返回一个Variant数组,而不是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
答案 2 :(得分:0)
这是我的解决方案。我添加了另一个循环,你可以忽略它。 @Elias
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