Excel/Excel VBA


good day to you,
I have three worksheets, titled response, positive words, negative words. Given a list of positive and negative words, i have placed them accordingly in the worksheets.
In the response sheet, i have an input cell, A(11). In excel VBA, i need to determine how many positive and negative words there are in the input box.
Ex. I am in a happy mood but my mum is angry.
There is a total of 1 positive "happy" and one negative "angry."
I need to develop a fraction, # of positive/# of negative. which will be given in the output box A(22) in response.
Your assistance is greatly appreciated!

Hi Min sang,

Something like this perhaps (Assuming the words are the only thing on those two sheets):

Note that this does not cater for words that may be part of other words.

Function GetWordsFraction(sSentence As String)
   Dim lPos As Long
   Dim lNeg As Long
   Dim lPosCt As Long
   Dim lNegCt As Long
   Dim vPos As Variant
   Dim vNeg As Variant
   vPos = Worksheets("Positive Words").UsedRange.Value
   vNeg = Worksheets("Negative Words").UsedRange.Value
   For lPos = LBound(vPos, 1) To UBound(vPos, 1)
       If InStr(sSentence, vPos(lPos, 1)) > 0 Then
         lPosCt = lPosCt + 1
       End If
   For lNeg = LBound(vNeg, 1) To UBound(vNeg, 1)
       If InStr(sSentence, vNeg(lNeg, 1)) > 0 Then
         lNegCt = lNegCt + 1
       End If
   If lNegCt > 0 Then
       GetWordsFraction = lPosCt / lNegCt
       GetWordsFraction = "Div/0"
   End If
End Function

Use like so:
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


All Answers

Answers by Expert:

Ask Experts


Jan Karel Pieterse


Excel and Excel/VBA questions


Excel MVP

Self employed Excel developer

Bachelor in Chemical Engineering

Awards and Honors
Microsoft MVP award since 2002

Past/Present Clients
Shell, Fortis bank, ABN-AMRO bank, Morgan Stanley, ...

©2016 About.com. All rights reserved.