Excel/coding

Advertisement


Question
Hi Aidan

hoping you can help me ....

I want add a code via vba to a range of cells

that if a cell say b3 = two characters then c3 will put a code with a vlook up to show a result

example

b3 = 15 then c3=Sum(d5:d12)

for all the cells from b3:b22

do that make sense.....or if the cells in b = anything that a code be placed in the c cell next to it .....

I have this code which works for what it does but I cant figure out where to put the if statement or code

If Not Intersect(Target, Range("E6")) Is Nothing Then
   For Each cell In Intersect(Target, Range("E6"))
       If Len(cell.Value) > 9 Then
         MyStr = cell
         Do
         Ans = Application.InputBox("You've exceed the characters allowed, Omit dashes", _
         "Shorten this description", MyStr, Type:=2)
         If Ans = "False" Then
         'Loop will restart
         ElseIf Len(Ans) <= 9 Then
         cell = Ans
         Exit Do
         Else
         MyStr = Ans
         End If
         Loop
       End If
   Next cell
End If

thanks

Answer
If it's a range of cells, I'm not sure why intersect is being used - isn't it something like

for each cl in range("B3:B22")
 if len(cl.value)=2 then 'BUT if you have numeric values it would need to be
  'if cl.value>=10 and cl.value<20 then
   'put whatever you want in here
  'eg to put 10 in the cell
  cl.offset(0,1).value=10
  'would do it - you can record a macro with whatever formula you need
 end if
next
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


Aidan Heritage

Expertise

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

Experience

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

Education/Credentials
I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2016 About.com. All rights reserved.