I'm currently working for a engineering company and it is my first time using visual basic. This piece of code was written by another person and i must fix the problem within the code. If anybody can find it, it would be really thankful.
Here's the code (This is one section of the whole code, one class):
Sub CheckSummerWinter()
'Check to see if Summer/Winter selection changed
'Remove protection
Passwrd = "ESGtrcCv" & Trim(Right(Worksheets("EnerSpectrum Group").Range("Version"), 4))
Worksheets("Avoided Load Profile").Unprotect (Passwrd)
Sheets("Avoided Load Profile").Select
If Worksheets("EnerSpectrum Group").OptionButton1.Value = True Then ' Summer Peaking
' Avoided Distribution Capacity Costs
Range("AW7").Select
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AV7*(L7/L$6*M7)"
Else ' OEB Table
ActiveCell.Formula = "=AV7*M7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "S"
Range("C7").Select
Else 'Must be Winter Peaking
' Avoided Distribution Capacity Costs
Range("AW7").Select
'Worksheets("Avoided Load Profile").Range("AW7").Select ' Avoided Distribution Capacity Costs
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AV7*(C7/C$6*D7)"
Else ' OEB Table
ActiveCell.Formula = "=AV7*D7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "W"
Range("C7").Select
End If
' Reset Generation Capacity formulas based on NPV OptionButtion
Range("AS7").Select
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AR7*(L7/L$6*M7)"
Else ' OEB Table
ActiveCell.Formula = "=AR7*M7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "W"
Range("C7").Select
' Reset Transmission Capacity formulas based on NPV OptionButtion
Range("AU7").Select
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AT7*(L7/L$6*M7)"
Else ' OEB Table
ActiveCell.Formula = "=AT7*M7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "W"
Range("C7").Select
' Sheets("EnerSpectrum Group").Select
'Restore Protection
Worksheets("Avoided Load Profile").Protect Password:=Passwrd, DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub 'CheckSummerWinter

Error Message
m0
I agree with what Spotty said, I would also add that it's pretty ugly code, but that's perhaps beside the point. It looks quite VBA to me, which is sometimes a bit of nightmare.
The first thing I would check is the section where the password protection is removed from the file. There it is referenced as worksheets("avoid loaded...") whereas later on it's sheets("avoid loaded..."). So possibly one of them is wrong, or the protection is not being removed.
Ankit Jain
Thank you for your concern and i appreciate your help. The error message that i get is:
Run-time error '1004':
select method of worksheet class failed
and this error occurs on the line thats hi-lighted in yellow below:
Sub CheckSummerWinter()
'Check to see if Summer/Winter selection changed
'Remove protection
Passwrd = "ESGtrcCv" & Trim(Right(Worksheets("EnerSpectrum Group").Range("Version"), 4))
Worksheets("Avoided Load Profile").Unprotect (Passwrd)
If Worksheets("EnerSpectrum Group").OptionButton1.Value = True Then ' Summer Peaking
Sheets("Avoided Load Profile").Select
' Avoided Distribution Capacity Costs
Range("AW7").Select
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AV7*(L7/L$6*M7)"
Else ' OEB Table
ActiveCell.Formula = "=AV7*M7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "S"
Range("C7").Select
Else 'Must be Winter Peaking
Sheets("Avoided Load Profile").Select
' Avoided Distribution Capacity Costs
Range("AW7").Select
'Worksheets("Avoided Load Profile").Range("AW7").Select ' Avoided Distribution Capacity Costs
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AV7*(C7/C$6*D7)"
Else ' OEB Table
ActiveCell.Formula = "=AV7*D7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "W"
Range("C7").Select
End If
' Reset Generation Capacity formulas based on NPV OptionButtion
Sheets("Avoided Load Profile").Select
Range("AS7").Select
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AR7*(L7/L$6*M7)"
Else ' OEB Table
ActiveCell.Formula = "=AR7*M7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "W"
Range("C7").Select
' Reset Transmission Capacity formulas based on NPV OptionButtion
Range("AU7").Select
If Worksheets("NPV TRC").OptionButton4 Then 'Direct Input
ActiveCell.Formula = "=AT7*(L7/L$6*M7)"
Else ' OEB Table
ActiveCell.Formula = "=AT7*M7"
End If
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Worksheets("HiddenData").Range("PriorPresentYear") = "W"
Range("C7").Select
' Sheets("EnerSpectrum Group").Select
'Restore Protection
Worksheets("Avoided Load Profile").Protect Password:=Passwrd, DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub 'CheckSummerWinter
mparter
If you are getting an error you should state the exact error messaage you are getting and hopefully the line that is causing this error. If there is no error but it's not doing what you expect it to do then you should state so, state what you want it to do and state what it is actually doing.
Thanks.
In this case you needto clarify what is going wrong. If you are getting an error please state the full error message. This allows us to help you much quicker. Thanks.
Dave Howe
Is the VBA or VB.Net Code.
The VB within Excel is Visual Basic for Application. VBA is a very different product from VB.NET and there are some other locations where youy will probably get a quicker and better response to your VBA questions.
You may find more assistance in following which specifically deals with VBA development.
http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=74&SiteID=1
Or if you are using VBA from within on of the office applications
Office Automation: office.developer.automation newsgroup
http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.office.developer.automation&lang=en&cr=US
Or the Office Newgroups
http://www.microsoft.com/office/community/en-us/default.mspx d=1
This is really a VBA issue using Excel rather than an VB.NET issue. Does the sheet named "Avoided Load profile" actually exist. As you dont appear to have any error handling in here - I would start by debugging the code and ensuring that the sheet exists before selecting it.
A simple web search on Excel and Error 1004 shows numerous hits and numerous causes. I would check these out as well as debugging you code.
More than that I think the other forums/newgroups will provide you with much more information.
Hope that helps