在 vba 中粘贴特殊值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11003620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
paste special values in vba
提问by user1452091
I am working on a small project which requires me to copy and paste certain columns if I detect "true" in the row. I am trying to paste these selected columns onto a different sheet and I want to paste only their values not the formulas.
我正在做一个小项目,如果我在行中检测到“true”,我需要复制和粘贴某些列。我试图将这些选定的列粘贴到不同的工作表上,我只想粘贴它们的值而不是公式。
This is what I have so far and I am getting an error with the paste special feature. Please help.
到目前为止,这是我所拥有的,并且我在粘贴特殊功能时遇到了错误。请帮忙。
' CopyIfTrue()
Dim Col As Range, Cell As Excel.Range, RowCount As Integer
Dim nysheet As Worksheet
Set nysheet = Sheets.Add()
nysheet.Name = "T1"
Sheets("FemImplant").Select
RowCount = ActiveSheet.UsedRange.Rows.Count
Set Col = Range("I2:I" & RowCount) 'Substitute with the range which includes your True/False values
Dim i As Integer
i = 1
For Each Cell In Col
If Cell.Value = "True" Then
Cell.Copy
Sheets("T1").Select 'Substitute with your sheet
Range("b" & i).Select
ActiveSheet.Paste
'Get sibling cell
Sheets("FemImplant").Select
Dim thisRow As Integer
thisRow = Cell.Row
Dim siblingCell As Range
Set siblingCell = Cells(thisRow, 2)
siblingCell.Copy
Sheets("T1").Select 'Substitute with your sheet
Range("a" & i).Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues
Sheets("FemImplant").Select
i = i + 1
End If
Next
回答by Stefan
PasteSpecial must be Range.PasteSpecial not ActiveSheet.PasteSpecial. They are different things and ActiveSheet.PasteSpecial does not know any parameter "Paste".
PasteSpecial 必须是 Range.PasteSpecial 而不是 ActiveSheet.PasteSpecial。它们是不同的东西,ActiveSheet.PasteSpecial 不知道任何参数“粘贴”。
ActiveSheet.Range("a" & i).PasteSpecial Paste = xlPasteValues
回答by Siddharth Rout
Is this what you are trying?
这是你正在尝试的吗?
Option Explicit
Sub Sample()
Dim rRange As Range
Dim RowCount As Integer, i As Long
Dim nysheet As Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Sheets("T1").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Set nysheet = Sheets.Add()
nysheet.Name = "T1"
With Sheets("FemImplant")
RowCount = .Range("I" & Rows.Count).End(xlUp).Row
.AutoFilterMode = False
Set rRange = .Range("I2:I" & RowCount)
With rRange
.AutoFilter Field:=1, Criteria1:="True"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
nysheet.Range("B1").PasteSpecial xlPasteValues
.Offset(1, -7).SpecialCells(xlCellTypeVisible).Copy
nysheet.Range("A1").PasteSpecial xlPasteValues
End With
.AutoFilterMode = False
End With
End Sub
回答by Kent Lau Chee Yong
I believe the code you provided is much faster than earlier. However to help other understand easier, why not put some comment?
我相信您提供的代码比以前快得多。然而,为了帮助其他人更容易理解,为什么不发表一些评论呢?
I have done that for you.
我已经为你做到了。
Sub ExtractData()
Dim selectedRange As Range ' Range to check
Dim Cell As Range
Dim iTotalRows As Integer ' Selected total number of rows
Dim i As Integer ' marker to identify which row to paste in new sheet
Dim shtNew As Worksheet
Dim shtData As Worksheet
Set shtData = Sheets("data")
Set shtNew = Sheets.Add()
shtNew.Name = "Analyzed data"
iTotalRows = shtData.UsedRange.Rows.count
Set selectedRange = shtData.Range("F2:F" & iTotalRows)
i = 1
' Check the selected column value one by one
For Each Cell In selectedRange.Cells
If Cell.Value = "True" Then
Cell.Copy shtNew.Range("A" & i)
' Copy the brand to column B in "Analyzed data" sheet
shtNew.Range("B" & i).Value = _
shtData.Cells(Cell.Row, 2).Value
i = i + 1
End If
Next ' Check next cell in selected range
End Sub
回答by Tim Williams
Your copy/paste can be shortened considerably...
您的复制/粘贴可以大大缩短...
' CopyIfTrue()
Dim Col As Range, Cell As Excel.Range, RowCount As Integer
Dim nysheet As Worksheet, shtFI As Worksheet
Set shtFI = Sheets("FemImplant")
Set nysheet = Sheets.Add()
nysheet.Name = "T1"
RowCount = shtFI.UsedRange.Rows.Count
Set Col = shtFI.Range("I2:I" & RowCount)
Dim i As Integer
i = 1
For Each Cell In Col.Cells
If Cell.Value = "True" Then
Cell.Copy nysheet.Range("B" & i)
nysheet.Range("A" & i).Value = _
shtFI.Cells(Cell.Row, 2).Value
i = i + 1
End If
Next