Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Business Applications Business Applications
Search Search
Search for:
Tech Support Guy Forums > > >

Solved: Excel 2003 vba code not working in Excel 2007


(!)

Teacherless's Avatar
Teacherless   (John) Teacherless is offline
Computer Specs
Member with 51 posts.
THREAD STARTER
 
Join Date: Jun 2012
Location: South Coast, South Africa.
Experience: Beginner
28-Jun-2012, 10:54 AM #1
Solved: Excel 2003 vba code not working in Excel 2007
Firstly I am a beginner of beginners but I try to learn as much as I can.
I have put the following protection code into an Excel 2003 workbook and everything runs ok. I then opened the workbook in Excel 2007, and saved it as a .xlsm file. When I try to open the .xlsm file in Excel 2007, I get a vba Runtime error 13, "Type mismatch", and the code sticks at the first line below("If Worksheets...). I have tried everything that my limited knowledge allows, searched the web for 2 days, and still have no answers.
Any help would be greatly appreciated.

Private Sub Workbook_Open()
If Worksheets(28).[A51] > "" And Worksheets(28).[A50] = Worksheets(28).[A51] = False Then
MsgBox "Sorry, this program is not registered for this computer, please e-mail .......... for more info."
Application.DisplayAlerts = False
Application.Quit
End If
End Sub


My system specs are shown below.

Tech Support Guy System Info Utility version 1.0.0.2
OS Version: Microsoft Windows 7 Home Premium, Service Pack 1, 64 bit
Processor: Intel(R) Core(TM) i3 CPU M 380 @ 2.53GHz, Intel64 Family 6 Model 37 Stepping 5
Processor Count: 4
RAM: 2934 Mb
Graphics Card: Intel(R) HD Graphics, 1243 Mb
Hard Drives: C: Total - 290143 MB, Free - 172186 MB;
Motherboard: Dell Inc., 0WXY9J
Antivirus: avast! Antivirus, Updated and Enabled
Jimmy the Hand's Avatar
Member with 1,217 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: Level 15 Paladin
28-Jun-2012, 03:19 PM #2
Try this way

Code:
If Worksheets(28).Range("A51") > "" And Worksheets(28).Range("A50") = Worksheets(28).Range("A51") = False Then
Although I find this statement a little curious...
Are you sure it does what you think it does?

Jimmy
Keebellah's Avatar
Keebellah   (Hans) Keebellah is offline Keebellah is a Trusted Advisor with special permissions. Keebellah has a Profile Picture
Computer Specs
Trusted Advisor with 4,767 posts.
 
Join Date: Mar 2008
Location: Oegstgeest, The Netherlands
Experience: Advanced
28-Jun-2012, 03:58 PM #3
These erros are programming errors and as Jimmy syas these statements are 'a little curious'
Where do the macro's come from?
Are macro's enabled?
Teacherless's Avatar
Teacherless   (John) Teacherless is offline
Computer Specs
Member with 51 posts.
THREAD STARTER
 
Join Date: Jun 2012
Location: South Coast, South Africa.
Experience: Beginner
29-Jun-2012, 04:28 AM #4
Hi guys, thanks for your prompt replies to my call for help. I will try to explain my code a little better.
The workbook in question I want to give to another person and I do not want it copied and used on another pc unless I give permission. I placed the code in "This Workbook". What happens is this, in sheet28(A50) there is a formula "GetName(3)" which displays the users pc name. Other code I have written into the sheet copies (A50) and pastes the value into (A51) thus leaving a permanent record of the first users pc name. If the workbook is then copied and opened in another pc, the Cells(A50) and (A51) will not be equal and the workbook will close. The only way that the workbook will function is if Cell(A51) is empty when first opened, thereafter Cells(A50) and (A51) must be equal, which they will be unless opened on another pc.
As I said, everything works perfectly in 2003 but the code sticks on the first line in 2007.
I have tried Jimmy's suggestion but still get the error.
I have also inserted code into "This Workbook" which forces the user to "Enable Macros" otherwise the workbook will not open.
I hope this explains thing a little better.

Teacherless.

Last edited by Teacherless; 29-Jun-2012 at 04:33 AM..
Keebellah's Avatar
Keebellah   (Hans) Keebellah is offline Keebellah is a Trusted Advisor with special permissions. Keebellah has a Profile Picture
Computer Specs
Trusted Advisor with 4,767 posts.
 
Join Date: Mar 2008
Location: Oegstgeest, The Netherlands
Experience: Advanced
29-Jun-2012, 04:46 AM #5
I don't know what the getname(3) formula does, but that's not really an issue.
I assume you use the username variable to regsiter this
First if you have sheet28 and somebody deteles it you'll have a problem.
I suggest a (very) hidden sheet wher you store th value.
You can leave it hidden and just do this.

For the eaxmple
create a sheet named user.
You will have to include an option to test if it exists and if does not exist create it and hide useing the vba xlsheetveryhidden, it won't show in the availabe hidden sheets.

the simply a1 the current user and a2 the using user.
Code:
Sub TestUser()
if sheets("User").range("A1").text = "" then
 Sheets("User").Range("A1;A2").value = environn$("username")
elseif if environ$("username") <> sheets('User").Range("A2") then 
Activeworkbook.Close False
end if
end Sub
See if you can work it out further, I typed thsi without Excel so syntax is not checked but this would be the main idea.
The sheet does not have to be unhidden to referto
__________________
Cheers,
Hans
It's all in the code... and may the code be with you! If it isn't . . . start debugging! (Time zone: Western Europe)
Jimmy the Hand's Avatar
Member with 1,217 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: Level 15 Paladin
29-Jun-2012, 06:42 AM #6
I suggest trying this code:

Code:
If (Worksheets(28).Range("A51") <> "") And (Worksheets(28).Range("A50") <> Worksheets(28).Range("A51")) Then
Jimmy
Teacherless's Avatar
Teacherless   (John) Teacherless is offline
Computer Specs
Member with 51 posts.
THREAD STARTER
 
Join Date: Jun 2012
Location: South Coast, South Africa.
Experience: Beginner
29-Jun-2012, 08:51 AM #7
Hi again, I tried your suggested code Jimmy, but still got the same error, (Type mismatch).
Hans, I created a new workbook, placed your code in "This Workbook" using "Private Sub Workbook_Open()", but when I ran the code I got an error "Sub or Function not defined" and
the line after "Then" was in red text. By the way, my sheet(28) is "Veryhidden" by default.
What I can't understand is why everything works fine in 2003 but the same code creates errors in 2007!!!
Again thank you guys for taking the time to help.
John (Teacherless).
Jimmy the Hand's Avatar
Member with 1,217 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: Level 15 Paladin
29-Jun-2012, 11:43 AM #8
Three more suggestions:
1. Try this code
Code:
If (Worksheets(28).Range("A51").Value <> "") And (Worksheets(28).Range("A50").Value <> Worksheets(28).Range("A51").Value) Then
2. Try separating the conditions so that you can see which one is disliked by Excel. E.g.
Code:
If (Worksheets(28).Range("A51").Value <> "") Then
    If (Worksheets(28).Range("A50").Value <> Worksheets(28).Range("A51").Value) Then
        'something
    End If
End If
3. Make sure that all references (VB editor - Tools - References) to outer libraries are in order.
Excel can throw very strange exceptions when any of those references is missing.

If none of these helps, then I'm out of ideas. You should post the workbook so that we can have a close look.

Jimmy
__________________
'
It is advised to provide a clear, detailed description of the task, so that others can understand it, and offer the best possible help. Otherwise, you risk experts ignoring your request.
Keebellah's Avatar
Keebellah   (Hans) Keebellah is offline Keebellah is a Trusted Advisor with special permissions. Keebellah has a Profile Picture
Computer Specs
Trusted Advisor with 4,767 posts.
 
Join Date: Mar 2008
Location: Oegstgeest, The Netherlands
Experience: Advanced
29-Jun-2012, 12:09 PM #9
I put the code i suggested in the file

Try it out.
Attached Files
File Type: xlsm Teacherless-Book1.xlsm (19.6 KB, 20 views)
Teacherless's Avatar
Teacherless   (John) Teacherless is offline
Computer Specs
Member with 51 posts.
THREAD STARTER
 
Join Date: Jun 2012
Location: South Coast, South Africa.
Experience: Beginner
29-Jun-2012, 01:16 PM #10
Excel 2003 code not working in Excel 2007
Thank you everyone for being so patient and persistent trying to solve my problem. First Jimmy, I tried your code suggestions, the first one created the same error, the second one produced the same error, but stuck on the second "If". It looks as though that if more than one cell address is in the code line, that's where the trouble starts.
Next Hans, I tried your workbook, (thanks for sending). What I need to know is how do I test this wb as I only have one pc. Could you explain what I must do to the wb in order to duplicate opening it on another pc.
What I also going to do tomorrow(it's getting late here in S.A.), is create 2 workbooks, one in 2003 and one in 2007 both using the same code and set up exactly the same as my original but using only 3 sheets as opposed to 30, which my original contains, and attach them here for you to examine.
Have a good day(or night) and thanks again.
Keebellah's Avatar
Keebellah   (Hans) Keebellah is offline Keebellah is a Trusted Advisor with special permissions. Keebellah has a Profile Picture
Computer Specs
Trusted Advisor with 4,767 posts.
 
Join Date: Mar 2008
Location: Oegstgeest, The Netherlands
Experience: Advanced
29-Jun-2012, 01:55 PM #11
How to reproduce? weel on one PC i don't know.
You could open the book, place another username in the USER sheet in both ranges.

Disable the before close macro so that the range is not cleared befor closing, save it, close it and open it again.
You don't need two version, just one xls file which works in both versions with no porblems
__________________
Cheers,
Hans
It's all in the code... and may the code be with you! If it isn't . . . start debugging! (Time zone: Western Europe)
Teacherless's Avatar
Teacherless   (John) Teacherless is offline
Computer Specs
Member with 51 posts.
THREAD STARTER
 
Join Date: Jun 2012
Location: South Coast, South Africa.
Experience: Beginner
30-Jun-2012, 03:22 AM #12
Hi there, I've created two workbooks and attached them. One is a working version in Excel 2003, the other is the problem version in Excel 2007. I have placed some information in sheet 1 (2003). I hope with these wbs you will be able to solve my problem.
Attached Files
File Type: xls Excel 2003.xls (42.0 KB, 22 views)
File Type: xlsm Excel 2007.xlsm (22.7 KB, 31 views)
Keebellah's Avatar
Keebellah   (Hans) Keebellah is offline Keebellah is a Trusted Advisor with special permissions. Keebellah has a Profile Picture
Computer Specs
Trusted Advisor with 4,767 posts.
 
Join Date: Mar 2008
Location: Oegstgeest, The Netherlands
Experience: Advanced
30-Jun-2012, 04:18 AM #13
All I did was reorganize your macro's, the non sheet macro's should always be in a separate module
You can elimintae the Call because it's a macro and no parameters.
I tried them and they both work, no syntax notihing.

I would however change your code to specific sheet names , not sheet(1) or so because you never know when someone reindexe them then Sheet(1) could all at once become Sheet(2) and then your whole plan goes down the drain.

I don't see any issues (anymore) and once again, you can use let's say the 2003 version under 2007 or 2010 without any problems unless you have a specific reason for both versions.

Let me know it it works, you're in the same timezone as I am (The Netherlands)
Attached Files
File Type: xls Teacherless-Excel 2003.xls (64.0 KB, 26 views)
File Type: xlsm Teacherless-Excel 2007.xlsm (25.1 KB, 19 views)
__________________
Cheers,
Hans
It's all in the code... and may the code be with you! If it isn't . . . start debugging! (Time zone: Western Europe)
Teacherless's Avatar
Teacherless   (John) Teacherless is offline
Computer Specs
Member with 51 posts.
THREAD STARTER
 
Join Date: Jun 2012
Location: South Coast, South Africa.
Experience: Beginner
30-Jun-2012, 05:20 AM #14
Good day my friend, you where remarkably quick to reply with 2 workbooks, thanks. I opened the 2003 version and it worked perfectly as you said it would. I then opened it using Excel 2007 and the same problem is still there, it sticks at the first line in "This Workbook".(Below Private Sub Workbook_Open()).
I then opened your 2007 version in Excel 2007, same, still sticks in the same place.
It would be good if I could have one version that opened in all versions of Excel but I don't mind having two, and giving the user a choice, depending upon the version of Excel that they are using.
Do you have 2003, 2007 or both on your pc as it is impossible to check both wbs using only one version ?
Changing the subject slightly, you guys made me smile when you commented on my code, "These statements are a little curious", because I know very little about writing vba from scratch, and this line in question I just used my knowledge of normal cell "If And" formulas and tried to write what I thought would work. After many attempts I did get it to work, but only in Excel 2003, as you know. I just wondered what it is about the code that caused that comment as I am always willing to learn.
Thanks, John.
Keebellah's Avatar
Keebellah   (Hans) Keebellah is offline Keebellah is a Trusted Advisor with special permissions. Keebellah has a Profile Picture
Computer Specs
Trusted Advisor with 4,767 posts.
 
Join Date: Mar 2008
Location: Oegstgeest, The Netherlands
Experience: Advanced
30-Jun-2012, 06:06 AM #15
I suggest the following.

Throw away the 2007 version.
use the attached 2003 version in borth Excels, they work here.

I have a virtual PC running Office 2003 and the 'normal' one wiht 2010

Both work perfectly with this version.

If needed then just open the 2003 version and choose saveas 1007 macro enabled and you will have the exact copy of the 2003 version

Try and let me know.
Attached Files
File Type: xls Teacherless-Excel 2003.xls (64.0 KB, 18 views)
__________________
Cheers,
Hans
It's all in the code... and may the code be with you! If it isn't . . . start debugging! (Time zone: Western Europe)
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑