+ Reply to Thread
Results 1 to 13 of 13

PasteSpecial with variable

  1. #1
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    PasteSpecial with variable

    Wondering if this is possible and is a beginners question so fogive me if it sounds stupid
    Part of macro looks like this

    Please Login or Register  to view this content.
    and I then go on to pastespecial dividing by 20.
    Is it possible to assign 20 to a variable instead and then use that variable in the pastespecial code?

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: PasteSpecial with variable

    I do not know what are you trying to do, but let me guess.

    You assigned the value of 20 in to A1 and copied A1, but that is. Excel will copy and you will see the copy sign and will not disappear, even you paste it somewhere. You have to tell VBA where to paste the copied range. You can only paste in to a cell and that cell needs to have an address. Yes, you can represent the address with a variable, and this will point VBA to paste it into that address.

  3. #3
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    Re: PasteSpecial with variable

    macro put a value of 20 in cell a1.
    Macro then copied this and used paste special command thus:

    Sheets(strSheetName).Range("M25", Range("O65536").End(xlUp)).PasteSpecial Paste:=xlPasteAll, Operation:=xlDivide, SkipBlanks _
    :=False, Transpose:=False

    I was asking if instead of putting 20 in a cell and copying it i could do something like
    x = 20
    with x....do the pastespecial

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: PasteSpecial with variable

    Yes, but not with paste.
    Let' say

    Range("a1").value = 20
    Range("a1").Copy

    Range("a2").PasteSpecial xlvalues

    Is the same as

    Range("a2")= Range("a1").value
    The first gives you a number of options, like paste all, values only, formats only, but the second one does not have these options, but works if you just want to copy values only. The second also does not leave the copy option flicking which is the case with the first one. You need to include a line.

    Application.CutCopyMode = 0
    to remove the copy command
    Last edited by AB33; 03-10-2017 at 05:23 PM.

  5. #5
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    Re: PasteSpecial with variable

    OK...thanks.
    Thought as much but wanted to be clear

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: PasteSpecial with variable

    Let me be clear.

    Is this what you are trying to do

    Range("a1").value = 20


    Range("a2").PasteSpecial xlvalues=Range("a1")


    If it is, it is a syntax error- the same as putting the copy value and paste in a single line is syntax error.

    Range("a1") could be a variable, but it is still a syntax error
    Range("a1").Copy Range("a2").PasteSpecial xlvalues ' error
    ?

  7. #7
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    Re: PasteSpecial with variable

    no
    It's all in post 3

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: PasteSpecial with variable

    The PasteSpecial value has to be a range on the Excel (not Office) clipboard. You can't do it from a variable, other than that you could have used a variable to put the value in the copied range.
    Last edited by shg; 03-10-2017 at 06:04 PM.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: PasteSpecial with variable

    Let' say, you have

    Range("a1").Copy
    Range("M25", Range("O65536").End(xlUp)).PasteSpecial Paste:=xlPasteAll

    Range("M25", Range("O65536") is the address of the cell where the copied value will be pasted.

    PasteSpecial is a method of a range, so I do not think you can change it (Assign a variable to it).
    You can only change the cell (Range) address.
    I hope I understand your drift.
    Last edited by AB33; 03-10-2017 at 06:38 PM.

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: PasteSpecial with variable

    Okay! shg gives you another angle, but still xlPasteSpecialOperationDivide can not be assigned to a variable.

  11. #11
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    Re: PasteSpecial with variable

    Quote Originally Posted by shg View Post
    Do you mean this?

    Please Login or Register  to view this content.
    would that take x and use it in pastespecial?
    If so then it is what I was looking for but I see it won't make my code shorter

  12. #12
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    Re: PasteSpecial with variable

    Quote Originally Posted by AB33 View Post
    Let' say, you have

    Range("a1").Copy
    Range("M25", Range("O65536").End(xlUp)).PasteSpecial Paste:=xlPasteAll

    Range("M25", Range("O65536") is the address of the cell where the copied value will be pasted.

    PasteSpecial is a property of a range, so I do not think you can change it (Assign a variable to it).
    You can only change the cell (Range) address.
    I hope I understand your drift.

    You explained it well. It can't really be done in a way that would reduce the amount of code

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: PasteSpecial with variable

    It may be, but remember short code does not mean efficient, nor it is better. I do not think it will make any difference in performance since we are dealing with 2-3 more lines.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PasteSpecial in VBA
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2016, 07:05 AM
  2. pastespecial into variable
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-08-2015, 04:59 PM
  3. [SOLVED] PasteSpecial
    By Phixer in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-06-2014, 03:47 PM
  4. PasteSpecial Help
    By jgupte in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-20-2008, 10:32 PM
  5. Help with PasteSpecial
    By staeri@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2006, 06:40 AM
  6. [SOLVED] PasteSpecial
    By John Austin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-14-2005, 11:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1