Excel/VBA If statement

Advertisement


Question
QUESTION: Hi Tom

Need a little help on doing an If statement in vba for a cell.  This is what i want to happen.

cell J14 if "Yes" then automatically takes them to Cell AA21

if its not then nothing

and place that within the code for that sheet.....

thanks ahead of time Tom

Tony

ANSWER: Tony,

You don't say what triggers the action.  If they select J14 then

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
if Target.address = "J14" then
 if lcase(target.value) = "yes" then
     Range("AA21").Select
 end if
End if
End Sub


If they edit cell J14 (i.e. they type in yes in J14) then

Private Sub Worksheet_Change(ByVal Target As Range)
if Target.address = "J14" then
 if lcase(target.value) = "yes" then
     Range("AA21").Select
 end if
End if
End Sub


Either code would need to go in the sheet module of the sheet where you want that behavior.

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: J14 has a data validation for Yes or No,
if they select Yes, then we need the cursor to
scroll over to AA21 so they can manually enter
the data required.

i put both codes in on the sheet, neither one worked,
i tried replacing the lower cap yes to Yes in the code
and just typing in yes in the cell, either way didn't
result in the screen moving over to AA21 to begin
to input data.....

Tony

Answer
Tony,

My typo  The address will be reported with $ signs.  So the code should be


Private Sub Worksheet_Change(ByVal Target As Range)
if Target.address = "$J$14" then
 if lcase(target.value) = "yes" then
     Range("AA21").Select
 end if
End if
End Sub

Making a selection with data validation should still trigger a change event.

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.