在偏移副本中复制值而不是公式
Copy Values instead of formulas in an offset copy
我无法弄清楚如何让我的偏移复制和粘贴代码,复制值而不是公式。
我有以下代码可以完美运行,但将复制的单元格放在下一个空白列中,但我需要的是值,而不是粘贴的公式。
Sub Burndown_Snapshot()
'Copies the Overall Status Summary Data from the Dashboard and adds to the next empty column of the Historic Status table
'Triggered by the 'Burndown Snapshot' button on the dashboard
Dim column_number As Integer
column_number = Sheets("Historic Status").Cells.Find(What:="*", After:=Range("IV65536"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
Sheets("Dashboard").Range("C3:C7").Copy Destination:=Sheets("Historic Status").Cells(1, column_number)
End Sub
我想我需要一个 .PasteValues
在那里的某个地方,但我不确定在哪里
谢谢
复制列范围
Set lCell = dws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious)
dws.Cells
表示所有工作表单元格,例如A1:XFD1048576
,或 A1:IV65536
对于 Office 2007
. 之前的版本
- (
Find
方法的第二个(省略)参数是 After
,其默认参数是 Cells(1)
或 Cells("A1")
,即范围的第一个单元格 ( dws.Cells
) 你将方法应用到。
- 结合
SearchDirection
参数的xlPrevious
参数,在最后一个单元格中首先查找(例如XFD1048576
)。
- 通过选择
SearchOrder
参数的 xlByColumns
参数,查找的下一个单元格将是 XFD1048575
等
LookIn
参数的 xlFormulas
参数确保找到第一个 non-empty 单元格(不是 =""
或 "'"
,只是空的(空白包括所有三个))。
- 第 4 个参数,
LookAt
参数不相关,因此省略。
Set dCell = dws.Cells(1, lCell.Column + 1)
- 找到单元格(
Not lCell Is Nothing
)后,我们在lCell
'中引用第一行(逗号前的1
)的单元格(dCell
)右侧相邻的 s 列 (lCell.Column + 1
)
Set drg = dCell.Resize(srg.Rows.Count, srg.Columns.Count)
- 目标范围必须与要通过分配复制的源范围大小相同。
- 在这种特殊情况下,您可以安全地省略
, srg.Columns.Count
,因为只有一列 Set drg = dCell.Resize(srg.Rows.Count)
(Resize
方法的两个参数的默认参数都是 1。
- 现在您可以通过分配复制值:
drg.Value = srg.Value
。
Sub Burndown_Snapshot()
'Copies the Overall Status Summary Data from the Dashboard and adds to the next empty column of the Historic Status table
'Triggered by the 'Burndown Snapshot' button on the dashboard
Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("Dashboard")
Dim srg As Range: Set srg = sws.Range("C3:C7")
Dim dws As Worksheet: Set dws = ThisWorkbook.Worksheets("Historic Status")
Dim lCell As Range
Set lCell = dws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious)
If lCell Is Nothing Then Exit Sub ' no data in range
Dim dCell As Range: Set dCell = dws.Cells(1, lCell.Column + 1)
Dim drg As Range: Set drg = dCell.Resize(srg.Rows.Count, srg.Columns.Count)
drg.Value = srg.Value
End Sub
所以我更新了我的代码以包含一个额外的复制粘贴功能来给我我想要的结果,这也有效,但需要一个定义的剪切和粘贴范围(在大多数情况下应该是可行的)
Sub Burndown_Snapshot2()
'Copies the Overall Status Summary Data from the Dashboard and adds to the next empty column of the Historic Status table
'Triggered by the 'Burndown Snapshot' button on the dashboard
Application.ScreenUpdating = False
Dim column_number As Integer
Dim rng As Range
column_number = Sheets("Historic Status").Cells.Find(What:="*", After:=Range("IV65536"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
Sheets("Dashboard").Range("C3:C7").Copy Destination:=Sheets("Historic Status").Cells(1, column_number)
For Each rng In Sheets("Historic Status").Range("B1:ZZ5")
If rng.Value <> "" Then
rng.Value = rng.Value
End If
Next rng
Application.ScreenUpdating = False
End Sub
但是@VBasic2008s 的答案更清晰并且没有任意终点
我无法弄清楚如何让我的偏移复制和粘贴代码,复制值而不是公式。
我有以下代码可以完美运行,但将复制的单元格放在下一个空白列中,但我需要的是值,而不是粘贴的公式。
Sub Burndown_Snapshot()
'Copies the Overall Status Summary Data from the Dashboard and adds to the next empty column of the Historic Status table
'Triggered by the 'Burndown Snapshot' button on the dashboard
Dim column_number As Integer
column_number = Sheets("Historic Status").Cells.Find(What:="*", After:=Range("IV65536"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
Sheets("Dashboard").Range("C3:C7").Copy Destination:=Sheets("Historic Status").Cells(1, column_number)
End Sub
我想我需要一个 .PasteValues
在那里的某个地方,但我不确定在哪里
谢谢
复制列范围
Set lCell = dws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious)
dws.Cells
表示所有工作表单元格,例如A1:XFD1048576
,或A1:IV65536
对于Office 2007
. 之前的版本
- (
Find
方法的第二个(省略)参数是After
,其默认参数是Cells(1)
或Cells("A1")
,即范围的第一个单元格 (dws.Cells
) 你将方法应用到。 - 结合
SearchDirection
参数的xlPrevious
参数,在最后一个单元格中首先查找(例如XFD1048576
)。 - 通过选择
SearchOrder
参数的xlByColumns
参数,查找的下一个单元格将是XFD1048575
等 LookIn
参数的xlFormulas
参数确保找到第一个 non-empty 单元格(不是=""
或"'"
,只是空的(空白包括所有三个))。- 第 4 个参数,
LookAt
参数不相关,因此省略。
Set dCell = dws.Cells(1, lCell.Column + 1)
- 找到单元格(
Not lCell Is Nothing
)后,我们在lCell
'中引用第一行(逗号前的1
)的单元格(dCell
)右侧相邻的 s 列 (lCell.Column + 1
)
Set drg = dCell.Resize(srg.Rows.Count, srg.Columns.Count)
- 目标范围必须与要通过分配复制的源范围大小相同。
- 在这种特殊情况下,您可以安全地省略
, srg.Columns.Count
,因为只有一列Set drg = dCell.Resize(srg.Rows.Count)
(Resize
方法的两个参数的默认参数都是 1。 - 现在您可以通过分配复制值:
drg.Value = srg.Value
。
Sub Burndown_Snapshot()
'Copies the Overall Status Summary Data from the Dashboard and adds to the next empty column of the Historic Status table
'Triggered by the 'Burndown Snapshot' button on the dashboard
Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("Dashboard")
Dim srg As Range: Set srg = sws.Range("C3:C7")
Dim dws As Worksheet: Set dws = ThisWorkbook.Worksheets("Historic Status")
Dim lCell As Range
Set lCell = dws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious)
If lCell Is Nothing Then Exit Sub ' no data in range
Dim dCell As Range: Set dCell = dws.Cells(1, lCell.Column + 1)
Dim drg As Range: Set drg = dCell.Resize(srg.Rows.Count, srg.Columns.Count)
drg.Value = srg.Value
End Sub
所以我更新了我的代码以包含一个额外的复制粘贴功能来给我我想要的结果,这也有效,但需要一个定义的剪切和粘贴范围(在大多数情况下应该是可行的)
Sub Burndown_Snapshot2()
'Copies the Overall Status Summary Data from the Dashboard and adds to the next empty column of the Historic Status table
'Triggered by the 'Burndown Snapshot' button on the dashboard
Application.ScreenUpdating = False
Dim column_number As Integer
Dim rng As Range
column_number = Sheets("Historic Status").Cells.Find(What:="*", After:=Range("IV65536"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
Sheets("Dashboard").Range("C3:C7").Copy Destination:=Sheets("Historic Status").Cells(1, column_number)
For Each rng In Sheets("Historic Status").Range("B1:ZZ5")
If rng.Value <> "" Then
rng.Value = rng.Value
End If
Next rng
Application.ScreenUpdating = False
End Sub
但是@VBasic2008s 的答案更清晰并且没有任意终点