我想使用-1
运算符将数据从“ Sheet1”传输到“ Sheet2”,因为它很快。但是,以下代码不起作用:
'*******************************************************************************
' Purpose: Finds the Nth occurrence of a value in cells of a row *
' to return the column number of the cell where it was found. *
'*******************************************************************************
' Inputs *
' FindValue: The value to search for. *
' FindRow: The row to search in. *
' OccurrenceNumber: The occurrence number of the value to search for. *
'*******************************************************************************
' Returns: The column number of the Nth occurrence of the value. *
' 0 if value was not found. *
' -1 if value was found, but not the specified occurrence of it. *
' -2 if worksheet has no values (-4163). *
' -3 if workbook is add-in (No ActiveSheet). *
'*******************************************************************************
Function FoundinrowColumn(FindValue As Variant, Optional FindRow As Long = 0, _
Optional OccurrenceNumber As Integer = 1) As Integer
Dim intCol As Integer ' Search Start Column Number
Dim intCount As Integer ' OccurrenceNumber Counter
Dim intWrap As Integer ' Wrap Around Stopper
' Check if ActiveSheet exists.
If ActiveSheet Is Nothing Then FoundinrowColumn = -3: Exit Function
' Check if sheet has no values.
If Cells.Find("*", Cells(Rows.count, Columns.count), -4163, 1, 1) _
Is Nothing Then FoundinrowColumn = -2: Exit Function
' Find first used row if no FindRow parameter.
If FindRow = 0 Then
FindRow = Cells.Find("*", Cells(Rows.count, Columns.count)).Row
End If
' Set initial Search Start Column Number.
intCol = Columns.count
' Try to find the Nth occurence of 'FindValue' in 'FindRow'.
For intCount = 1 To OccurrenceNumber
If Not Rows(FindRow).Find(FindValue, Cells(FindRow, intCol)) Is Nothing Then
intCol = Rows(FindRow).Find(FindValue, Cells(FindRow, intCol)).Column
If intCount > 1 Then
If intCol = intWrap Then FoundinrowColumn = -1: Exit Function
Else
intWrap = intCol
End If
Else
FoundinrowColumn = 0: Exit Function
End If
Next
FoundinrowColumn = intCol
End Function
'*******************************************************************************
为什么?
答案 0 :(得分:2)
如果Range
和Cells
不完全合格,则将从ActiveSheet
中提取它们。
代码行
Sheets("Sheet2").range(cells(1, 1),cells(1,5)).value = Sheets("Sheet1").range(cells(1, 1),cells(1,5)).value
与
相同Sheets("Sheet2").range(ActiveSheet.cells(1, 1),ActiveSheet.cells(1,5)).value = Sheets("Sheet1").range(ActiveSheet.cells(1, 1),ActiveSheet.cells(1,5)).value
此操作失败,因为Sheet2
的单元格或Sheet1
的单元格不能来自ActiveSheet
。如果Sheet2
和Sheet1
都不是活动工作表,则可能两者都不是。
因此我们必须完全限定单元格:
Sheets("Sheet2").Range(Sheets("Sheet2").Cells(1, 1), Sheets("Sheet2").Cells(1, 5)).Value = Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, 1), Sheets("Sheet1").Cells(1, 5)).Value
更好地使用Worksheet
变量以提高可读性:
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
ws2.Range(ws2.Cells(1, 1), ws2.Cells(1, 5)).Value = ws1.Range(ws1.Cells(1, 1), ws1.Cells(1, 5)).Value
答案 1 :(得分:-1)
如果您一个接一个地执行并循环遍历它们,它确实可以工作。
Sheets("Sheet2").cells(1, 1).value = Sheets("Sheet1").cells(1,1).value
我不确定是否可以在范围上使用=,但是会外观。但是您可以复制并粘贴范围。