AlexJames Posted September 4, 2008 Share Posted September 4, 2008 Hey guys... for all you excel gurus out there... I need some simple code but i seem to remember some of the code is written slightly different when using vb in excel instead of vb stand alone. I need something simple like ... If c2.value = "***" then msgbox ("correct") or If c2.value = "***" then D11.value = "something else" I know that in excel If statments will work when I code them against a command button but the .value field is obviously not correct... am I making sense? Quote Link to comment Share on other sites More sharing options...
stevie_b Posted September 4, 2008 Share Posted September 4, 2008 I assume c2 is a command button, is it? If so, what do you intend (in real world terms) the "value" to be? The only thing that command buttons usually do is kick off some code when they're clicked. Quote Link to comment Share on other sites More sharing options...
AlexJames Posted September 4, 2008 Author Share Posted September 4, 2008 no c2 would be the excel cell ref. Quote Link to comment Share on other sites More sharing options...
stevie_b Posted September 4, 2008 Share Posted September 4, 2008 Something like this: Sub wobble() If Range("C2").Value = "yes" Then MsgBox "hello" ElseIf Range("C2").Value = "no" Then MsgBox "goodbye" Else MsgBox "pepper-crusted fish in a lime and corriander vinigrette" End If End Sub Quote Link to comment Share on other sites More sharing options...
Jake Posted September 4, 2008 Share Posted September 4, 2008 If Range (“C2”) = “blah” Then Msgbox “Correct” Quote Link to comment Share on other sites More sharing options...
stevie_b Posted September 4, 2008 Share Posted September 4, 2008 As Jake said, you don't need to specify the ".Value" bit: Excel assumes that's what you mean. Quote Link to comment Share on other sites More sharing options...
AlexJames Posted September 4, 2008 Author Share Posted September 4, 2008 ahh thanks guys! your legends.. I'll give that a bash and let u know how I do! Quote Link to comment Share on other sites More sharing options...
stevie_b Posted September 4, 2008 Share Posted September 4, 2008 When you try it, you'll need to insert the code into the module for the sheet you want to operate on. If you insert it into a generic module not attached to a sheet, Excel doesn't know which sheet you're talking about: they each will contain a cell C2. Quote Link to comment Share on other sites More sharing options...
AlexJames Posted September 4, 2008 Author Share Posted September 4, 2008 true... i never thought of that... so if I wanted to say ... If range "c2" on sheet1 = "yes" then sheet2 range "c4" = yes does that makes sense? Quote Link to comment Share on other sites More sharing options...
stevie_b Posted September 4, 2008 Share Posted September 4, 2008 That's right, but the syntax is like this: If Sheets("Sheet1").Range("C2").Value = "yes" Then .... Basically, the code drills down through the layers of an Excel spreadsheet: first it specifies the sheet, then the cell in that sheet. If you want to go wild, you can specify the spreadsheet itself if you've got more than one open (only do this if you need to: it's pointless otherwise). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.