Excel宏仅将工作表复制到新工作表粘贴值

时间:2019-02-20 19:15:54

标签: excel vba

我正在使用Excel中的宏,它将复制当前工作表并将值粘贴到新工作表中。工作表名称将与它后面的数字相同[即,Sheet1,Sheet1(2)]

除了将所有内容复制并粘贴到Sheet1(2)之外,我的代码可以正确执行此操作。我只希望它将Sheet1的值(而不是公式)粘贴到Sheet1(2)。我充其量是vba的新手,因此非常感谢任何建议。

Sub SPACER_Button4_Click()
' Compile Button to Generate Quote
'
'variables definitions
ActiveSheetValue = ActiveSheet.Name
'
'This section creates a copy of the active worksheet and names it with the next corresponding number
    Sheets(ActiveSheetValue).Copy After:=Sheets(ActiveSheetValue)

'This section should look for X value in each row, column 4. If value equals X, it deletes the row on the copied sheet
Dim i As Integer
i = 26
Do Until i > 300
    If ActiveSheet.Cells(i, 11).Value = "X" Then
        Rows(i).Delete
        Skip = True
    End If
    '
    If Skip = False Then
        i = i + 1
    End If
    '
    Skip = False
Loop

'This part hides columns on Right K thru R of new copied sheet
    Sheets(ActiveSheet.Name).Range("K:R").EntireColumn.Hidden = True
End Sub

2 个答案:

答案 0 :(得分:0)

如果数据是连续的,请考虑创建一个新工作表,选择并复制数据范围,然后使用以下代码粘贴到新工作表上。

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

答案 1 :(得分:0)

我使用这样的东西:

Sub KopyKat()
Dim s1 As Worksheet, s2 As Worksheet
Dim r As Range, addy As String

Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")

For Each r In s1.UsedRange
    If r.Value <> "" Then
        If Not r.HasFormula Then
            addy = r.Address
            r.Copy s2.Range(addy)
        End If
    End If
Next r

End Sub