none
help create macro VBA Word 2010 unique calculating characters (hebrew) in selection... RRS feed

  • Question

  • I want to create a macro to calculate Hebrew characters found in the selection, when one category of characters will be multiplied as if it is a two value, and the other characters found will be multiplied as if it is a one value. And I need the result to appear in a cell in a near by table. So the cell in the table will be a data field, and it will be updated automatically by running the macro. I need a kind of Calculation expression or find and calculate, or the IF, Then Next expression or another expression, I'm sure that there is some way to do that. can you help me or give me some hints?
    Friday, August 26, 2011 8:14 PM

All replies

  • You will need something like

    Dim i As Long, j As Long
    With Selection
        j = 0
        For i = 1 To .Characters.Count
            Select Case .Characters(i)
                Case "" 'any of the characters with a value of 1
                    j = j + 1
                Case "" 'any of the characters with a value of 2
                    j = j + 2
            End Select
        Next i
    End With
    MsgBox j

    Where you insert the appropriate characters in the Case statements (check out the Select Case method for the syntax.

    Instead of displaying the j in a message box, you could have it inserted into a cell of a table, but you have not indicated how that table would be identified or into which cell you want to insert it.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Saturday, August 27, 2011 6:20 AM
  • It appears a massage box 0 without caculating :(

    how do i make him calculate


    Sunday, August 28, 2011 10:51 PM
  • Show us the code that you actually used.  You would need to put the appropriate strings inside the quote marks in the following lines of code:

    Case "" 'any of the characters with a value of 1

    and              

    Case "" 'any of the characters with a value of 2

     


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Monday, August 29, 2011 3:57 AM
  • Sub Macro5555()
    Dim i As Long, j As Long
     With Selection
         j = 0
         For i = 1 To .Characters.Count
             Select Case .Characters(i)
                 Case "here i put some hebrew characters without space" 'any of the characters with a value of 1
                     j = j + 1
                 Case "and here the other characters without space" 'any of the characters with a value of 2
                     j = j + 2
             End Select
         Next i
     End With
     MsgBox j
    End Sub

     

    It works but only if i put in only one character how do i edd to count multiple character

    as i see you know so please help me accomplish i need to loop it untill the end

    Monday, August 29, 2011 4:12 AM
  • On Mon, 29 Aug 2011 04:12:29 +0000, nashoomoo wrote:
     
    >
    >
    >Sub Macro5555()
    >Dim i As Long, j As Long
    > With Selection
    >     j = 0
    >     For i = 1 To .Characters.Count
    >         Select Case .Characters(i)
    >             Case "here i put some hebrew characters without space" 'any of the characters with a value of 1
    >                 j = j + 1
    >             Case "and here the other characters without space" 'any of the characters with a value of 2
    >                 j = j + 2
    >         End Select
    >     Next i
    > End With
    > MsgBox j
    >End Sub
    >
    >
    >It works but only if i put in only one character how do i edd to count multiple character
    >
    >as i see you know so please help me accomplish i need to loop it untill the end
     
    Doug instructed you to "check out the Select Case method for the syntax".  Clearly you did not or you would have realized that you need to use a comma-separated list of quote-delimited characters.
     
    e.g:  Case Is = "a", "b", "c"
     
     
     

    Ron
    Monday, August 29, 2011 11:07 AM
  • As I have understood "check out the select case method for the syntax" means to Google it How to select each case. However, I did not find anything but other what I need, and most of the explanation on the web is for Excel. and not plain for beginners. And now I know it is better to ask then try to do it myself. I spend already on this a whole week literally, With all the information found through Google. I can't figure it out. Now I know there is an easy way but only the right way.  now it works and i cant even believe that

     

    Thanks for the nice peaple on the web

    Monday, August 29, 2011 3:04 PM
  • I'm glad you got it working.  And I think we had a different way of interpreting what was recommended.

    But I must say that I am truly impressed by the level of your description of exactly what you had done:

      "Case "here i put some hebrew characters without space" 'any of the characters with a value of 1"

    Your description made it pretty simple to figure out your problem, and is at a level of sophistication that is quite a bit above what frequently gets posted on these forums.

    In this case, I interpreted "check out the select case..." as meaning go to HELP in the Visual Basic Editor.

    From VB Help for Select Case we see:

    expressionlist-n Required if a Case appears. Delimited list of one or more of the following forms: expression, expression To expression, Is comparisonoperator expression. The To keyword specifies a range of values. If you use the To keyword, the smaller value must appear before To. Use the Is keyword with comparison operators (except Is and Like) to specify a range of values. If not supplied, the Is keyword is automatically inserted.

    which, in my opinion, is pretty obscure (even for a non-beginner!).  But there is also an example using text (as well as a variable):

     

    Case "everything", "nuts" To "soup", TestItem

    In which we can see that the expressionlist is comma separated with the text strings in quotes.

     


    Ron
    Monday, August 29, 2011 3:32 PM
  • Now if I can make it to show the results on a cell in a table on the left, which will be some field. The macro will need to select the first row on page and show the results in that first cell. So I will be able to run it just by clicking update field, and it will select the first row on page and updating the result in that first cell automatically. Now if I can make it to select the second row and show the results on the second cell and so on. If you know a solution, a way to do it please suggest.

     

    Thanks

    Monday, August 29, 2011 3:33 PM
  • Hopefully Doug or someone else will be able to help you with that.  Although I have some familiarity with VBA and Excel, I am not as familiar with Word.
    Ron
    Monday, August 29, 2011 9:41 PM