简体   繁体   中英

VBA Copy+Paste Code

I have a subset of a macro I'm writing that needs a bit of modification & help from all you geniuses.

Sheet "Regions!A6:R6 contain formulas pulling data out of a table. I want "Regions!A6:R6 copied as values, then pasted in "Temp!A1""

Would you be able to help modify this code to accomplish this end goal?

Sub Testing()

' Gets Number of Row in Regions Sheet
Sheets("Regions").Activate
LR1 = Cells(Rows.Count, "A").End(xlUp).Row


'Creates a New Worksheet and copy's the data from regions, interst the copied data into the
'New Temp Sheet and then removes the duplicates to create your list of unique items to filter on

'Creating the New WorkSheet
Sheets.Add.Name = "Temp"

'Copy the data
Sheets("Regions").Activate
Range("A6:R" & LR1).Select
Selection.Copy

'Paste the data into Temp Sheet
Sheets("Temp").Activate
Range("A1").Select
ActiveSheet.Paste

I'm not a genius, but I'm someone who always tries to apply good coding practices. First thing I'd get rid of those Select and Activate stuff, simplifying the code, and then I assign Value instead of using copy/paste, which will get you rid of the formulas and copy values only.

Sub Testing()
    Sheets.Add.Name = "Temp"
    With Sheets("Regions").Range("A6:R" & Sheets("Regions").Cells(Rows.Count, "A").End(xlUp).Row)
        Sheets("Temp").Range("A1").Resize(.Rows.Count, .Columns.Count).value = .value
    End With
End Sub

use this

Sub Testing()
    'Creating the New WorkSheet
    Sheets.Add.Name = "Temp"

    'Copy the data
    With Sheets("Regions")
        With .Range("R6", .cells(.Rows.Count, "A").End(xlUp))
            Sheets("Temp").Range("A1").Resize(.Rows.Count, .Columns.Count).Value = .Value
        End With
    End With
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