|
-
Oct 26th, 2011, 11:18 AM
#1
Thread Starter
Junior Member
[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!
-
Oct 26th, 2011, 03:21 PM
#2
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:
Dim FirstSht As Worksheet set FirstSht = activesheet Dim StatSigSht As Worksheet Set StatSigSht = sheets("sheet23") 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
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
-
Oct 26th, 2011, 03:22 PM
#3
Fanatic Member
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
-
Oct 26th, 2011, 03:47 PM
#4
Thread Starter
Junior Member
Re: [RESOLVED] Dim and Copy and Paste issue in VBA
Thanks for the help on that. That was super simple!
-
Oct 27th, 2011, 12:20 PM
#5
Thread Starter
Junior Member
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
-
Oct 28th, 2011, 04:24 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|