Results 1 to 6 of 6

Thread: [RESOLVED] Dim and Copy and Paste issue in VBA

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    23

    Resolved [RESOLVED] Dim and Copy and Paste issue in VBA

    I am getting a few errors when I try to run this. I have a secret suspicion that I am missing something super simple.

    Code:
    Sub StatSig()
    Dim FirstSht As Worksheet
    ActiveSheet.Name = FirstSht
    Dim StatSigSht As Worksheet
    Sheet23 = StatSigSht
    FirstSht.Range("B61:C62").Value = StatSigSht.Range("D19:E20").Value
    StatSigSht.Range("D27").Value = FirstSht.Range("B71").Value
    StatSigSht.Range("C29").Value = FirstSht.Range("B72").Value
    
    End Sub
    Help me out!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Dim and Copy and Paste issue in VBA

    as well as declare the sheet you also have to set the object to the variable

    try like, below, but i am guessing if this is the desired requirement
    vb Code:
    1. Dim FirstSht As Worksheet
    2. set  FirstSht = activesheet
    3. Dim StatSigSht As Worksheet
    4. Set StatSigSht = sheets("sheet23")
    5. FirstSht.Range("B61:C62").Value = StatSigSht.Range("D19:E20").Value
    6. StatSigSht.Range("D27").Value = FirstSht.Range("B71").Value
    7. StatSigSht.Range("C29").Value = FirstSht.Range("B72").Value
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Dim and Copy and Paste issue in VBA

    You have to assign something to the worksheets you dim.

    Code:
    Sub StatSig()
    Dim FirstSht As Worksheet
    Set FirstSht = Worksheets("NAMEHERE")
    ActiveSheet.Name = FirstSht
    Dim StatSigSht As Worksheet
    Set StatSigSht = Worksheets("NAMEHERE")
    Sheet23 = StatSigSht
    FirstSht.Range("B61:C62").Value = StatSigSht.Range("D19:E20").Value
    StatSigSht.Range("D27").Value = FirstSht.Range("B71").Value
    StatSigSht.Range("C29").Value = FirstSht.Range("B72").Value
    
    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    23

    Re: [RESOLVED] Dim and Copy and Paste issue in VBA

    Thanks for the help on that. That was super simple!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    23

    Re: [RESOLVED] Dim and Copy and Paste issue in VBA

    So I may have clicked Resolved a little too soon. I have to go back to the initial sheet and paste other info. Does anyone have a simple idea on how to address this? It's telling me the String isn't working.

    Code:
    Sub StatSig()
    Dim strInitialCellAddress As String
    strInitialCellAddress = ActiveCell.Address
    ActiveSheet.Range("B61:C62").copy
    Sheets("Inputs & Outputs").Select
    ActiveSheet.Range("D19:E20").PasteSpecial xlPasteValues
    ActiveSheet.Range("D27").copy
    strInitialCellAddress.Select
    ActiveSheet.Range("B71").PasteSpecial xlPasteValues
    strInitialCellAddress.Select
    ActiveSheet.Range("C29").copy
    strInitialCellAddress.Select
    ActiveSheet.Range("B72").PasteSpecial xlPasteValues
    
    End Sub

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Dim and Copy and Paste issue in VBA

    your first code using sheet objects to work with multiple sheets, is far superior to selecting and using activesheet, as far as possible, avoid selection, select and active anything in your code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width