如果行数和列数不断变化,是否有更快的方法将单元格值从一个 sheet 复制到另一个
Is there a faster way to copy cell values from one sheet to another if the number of rows and columns keep changing
我正在尝试制作最通用的宏来将数据从一个单元格传输到另一个单元格,此时我正在将一个单元格一个单元格复制到一个 var 并以此方式设置值,但这真的会减慢任何想法吗?
For i = TableDataRowNum To lastDataRow - 1
For activeCounter = 0 To colCount
aCellValue = ServiceTemp.Cells(TableDataRowNum + printCounter, activeCounter + 1).Text
aSheet.Cells(rowCount + printCounter, activeCounter + 1).value2 = "'" & aCellValue
aSheet.Cells(rowCount + printCounter, activeCounter + 1).Font.colorIndex = colorValue
Next
printCounter = printCounter + 1
statusBarCounter = statusBarCounter + 1
If statusBarCounter = 450 Then
theStatus.ShowStatus "Formatting Data To WorkSheet " & aSheet.Name & " " & printCounter & " Rows Processed"
statusBarCounter = 1
End If
Next
没有足够的声誉来评论,但如果你有一个连续的范围,并且你知道起始单元格,你可以把它放在一个变量中,然后你可以像下面这样定义要复制的范围并粘贴以设置目标:
Dim luc As Range
Dim src As Range
Dim dest As Range
Set luc = Cells(1, 1)
Set dest = Cells(10, 1)
Set src = Range(luc, luc.End(xlToRight))
Set src = Range(src, src.End(xlDown))
src.Copy
dest.PasteSpecial (xlPasteAll)
您可以将其包装在一个将源和目标范围的左上角作为输入的子项中。
处理数组中的数据比处理 table 或工作表的实际单元格内容要快得多。
Sub GetArrayData()
'Set the table to a listObject
Dim tbl As ListObject
Set tbl = shData.ListObjects("tblTest")
'Create array and fill it with the data of the table
Dim arr As Variant
arr = tbl.DataBodyRange()
Dim row As Long, col As Long
'Loop through the rows/columns of the array if you need to process the data
For row = LBound(arr, 1) To UBound(arr, 1)
For col = LBound(arr, 2) To UBound(arr, 2)
'Debug.Print arr(row, col)
Next col
Next row
'Paste data to cell F1
shData.Range("F1:G999") = arr
End Sub
这将 运行 几乎立即处理 2000 条数据,并且随着您添加更多数据将继续快速。
我正在尝试制作最通用的宏来将数据从一个单元格传输到另一个单元格,此时我正在将一个单元格一个单元格复制到一个 var 并以此方式设置值,但这真的会减慢任何想法吗?
For i = TableDataRowNum To lastDataRow - 1
For activeCounter = 0 To colCount
aCellValue = ServiceTemp.Cells(TableDataRowNum + printCounter, activeCounter + 1).Text
aSheet.Cells(rowCount + printCounter, activeCounter + 1).value2 = "'" & aCellValue
aSheet.Cells(rowCount + printCounter, activeCounter + 1).Font.colorIndex = colorValue
Next
printCounter = printCounter + 1
statusBarCounter = statusBarCounter + 1
If statusBarCounter = 450 Then
theStatus.ShowStatus "Formatting Data To WorkSheet " & aSheet.Name & " " & printCounter & " Rows Processed"
statusBarCounter = 1
End If
Next
没有足够的声誉来评论,但如果你有一个连续的范围,并且你知道起始单元格,你可以把它放在一个变量中,然后你可以像下面这样定义要复制的范围并粘贴以设置目标:
Dim luc As Range
Dim src As Range
Dim dest As Range
Set luc = Cells(1, 1)
Set dest = Cells(10, 1)
Set src = Range(luc, luc.End(xlToRight))
Set src = Range(src, src.End(xlDown))
src.Copy
dest.PasteSpecial (xlPasteAll)
您可以将其包装在一个将源和目标范围的左上角作为输入的子项中。
处理数组中的数据比处理 table 或工作表的实际单元格内容要快得多。
Sub GetArrayData()
'Set the table to a listObject
Dim tbl As ListObject
Set tbl = shData.ListObjects("tblTest")
'Create array and fill it with the data of the table
Dim arr As Variant
arr = tbl.DataBodyRange()
Dim row As Long, col As Long
'Loop through the rows/columns of the array if you need to process the data
For row = LBound(arr, 1) To UBound(arr, 1)
For col = LBound(arr, 2) To UBound(arr, 2)
'Debug.Print arr(row, col)
Next col
Next row
'Paste data to cell F1
shData.Range("F1:G999") = arr
End Sub
这将 运行 几乎立即处理 2000 条数据,并且随着您添加更多数据将继续快速。