简体   繁体   中英

VBA macros:How to check every value of a range to a specific value and paste the specific row(of the range) as pastespecial using VBA macro?

I have my data as

Row1 2012 12 13 14 15 16

Row2 2013 10 20 30 40 50

Row3 2014 54 34 50 67 78

Row4 2015 67 82 33 44 66

I have formulas in my column3,column4,column5,column6,column7 I have minimum value which might be 2012 or 2013 or 2014 accordingly.Let us say it's 2013. I want to create a macro to Paste the entire row as values when the yr is less than the minimum value.In this case i want the row R1 to be pasted as values since my min is 2013 and the rest remains the same as formulas.

I tried the below macro but got stuck in the pastespecial part.Please help me out debug it or write it in a more efficient way.

Sub pasteasvalues()
Application.ScreenUpdating= False

Dim rng as Range
Dim cell as Variant
   Sheets("working").Activate
   Set rng=Range("A1:A4")
   For each cell in rng
   If cell.value=2013 then
   cell.EntireRow.copy
   cell.EntireRow.PasteSpecial Paste:x1PasteValues
   End if
   Next

End Sub

I face an error in the Paste:x1PasteValues part.Let me know where am i going wrong or how can i write it more efficiently

You can remove the formulas by making the range.value=range.value, you don't need to copy/pasteSpecial.

Sub pasteasvalues()
    Dim Sh As Worksheet
    Dim rng As Range
    Dim cell As Range
    Set Sh = Sheets("working")
    Set rng = Sh.Range("A1:A4")
    For Each cell In rng.Cells
        If cell.Value = 2013 Then cell.EntireRow.Value = cell.EntireRow.Value
    Next cell

End Sub
Sub pasteasvalues()
Application.ScreenUpdating = False

Dim rng As Range
Dim cell As Variant

Sheets("working").Activate
Set rng = Range("A1:A4")        

    For Each cell In rng
           If cell.Value = 2013 Then
               cell.EntireRow.Copy
               Range("a6").Select
               Selection.PasteSpecial Paste:=xlPasteValues,Operation:=xlNone, SkipBlanks :=False, Transpose:=False
            End If
     Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM