Copy -> Paste Values Efficiency Question

BBrandt

Board Regular
Joined
Jul 14, 2008
Messages
155
Quick question here:

If I want to copy a cell (say... the input sheet, cell C4) and paste only its value somewhere else (say... the results sheet, cell B4), is there a better way to do it than
Code:
[C4].Copy
        Sheets("Results").Range("B4").PasteSpecial Paste:=xlPasteValues, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
?

I was thinking something along the lines of using [C4].Copy(Destination) but having it know to only put the values. Just trying to clean up my code a bit, thanks in advance!
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This would be more efficient

Code:
Sheets("Results").Range("B4").Value = Range("C4").Value
 
Upvote 0
Cheers, thanks!

Follow up question:
If C4 is part of a table with a variable number of rows depending on other user input, how might I go about performing that operation for every row of the table, starting at C4 and ending at ??4 where ?? is the last row in the table? Thanks again.
 
Upvote 0
Try

Code:
Sub cpy()
Dim LR As Long
With Sheets("Input")
    LR = .Range("C" & Rows.Count).End(xlUp).Row
    Sheets("Results").Range("B4:B" & LR).Value = .Range("C4:C" & LR).Value
End With
End Sub
 
Upvote 0
Thanks! That worked like a charm and is roughly a half the length of what I came up with. I rather like efficient code. :cool:
 
Upvote 0
Thanks! That worked like a charm and is roughly a half the length of what I came up with. I rather like efficient code. :cool:

You are welcome :)

If you 'lurk' on this board and study the solutions offered by the more prolific posters you will pick up methods to make your code more efficient.
 
Upvote 0
Yep, I'm sort of teaching myself VBA, and when I say that, I mean that sites like Mr. Excel are teaching me VBA.

Final question (I swear... for today anyway):

If I want to fill column D on the results page with a number that won't change per row (ex. all cells in the column will be equal to cell F1 from the inputs page), but will also match the variable table height, what would I do differently from the above code?

I know this could be accomplished by filling the column with an IF statement inside excel (ex. =IF(AND(B4="",C4=""),"",'Inputs'!F1), but in the long run it will actually be more convenient if the macro does it for me. Thanks.
 
Last edited:
Upvote 0
Apparently I can't edit my last post anymore, but regardless, I've figured it out. Took a couple tries but the working code is:

Code:
    With Sheets("Results")
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        .Range("D4:D" & LR).Value = [num]
    End With

...which fills column D with the constant cell labeled "num" (actually cell D12 on the input page) based on the number of rows in the previously filled column C.

Cheers
 
Upvote 0

Forum statistics

Threads
1,215,548
Messages
6,125,464
Members
449,229
Latest member
doherty22

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top