View Single Post
 
Old 10-08-2012, 05:44 AM
tomlam tomlam is offline Windows 7 64bit Office 2007
Advanced Beginner
 
Join Date: Oct 2012
Posts: 33
tomlam is on a distinguished road
Default VBA code for count w and write into another column in excel

Hi all

I face a problems that is i need to count how many w in column B and write into column C frequently in Excel .how to write VBA code to automatic
count and write total w as follow column c. Because i need to estimate the performance of thousand of students frequently

Column A / column B / column C
peter / good;(2w2w)slow/ 4w
mary/ good /
john/ not bad(1w1w1w1w)/ 4w
alxe /good/
tom /bad(2w4w5w3w) / 14w
simon/ good(1w2w3w)/ 6w

if we put the formula in column c

=((LEN(B2)-LEN(SUBSTITUTE(B2,"1w","")))*1+(LEN(B2)-LEN(SUBSTITUTE(B2,"2w","")))*2+(LEN(B2)-LEN(SUBSTITUTE(B2,"3w","")))*3+(LEN(B2)-LEN(SUBSTITUTE(B2,"4w","")))*4+(LEN(B2)-LEN(SUBSTITUTE(B2,"5w","")))*5+(LEN(B2)-LEN(SUBSTITUTE(B2,"6w","")))*6+(LEN(B2)-LEN(SUBSTITUTE(B2,"7w","")))*7+(LEN(B2)-LEN(SUBSTITUTE(B2,"8w","")))*8)/2

it will work but if want to use vba progamme, how to write

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 1 To lastrow

If (Cells(1, ("a"))) <> "" Then Value.Cells(x, ("c")) =
=((LEN(B2)-LEN(SUBSTITUTE(B2,"1w","")))*1+(LEN(B2)-LEN(SUBSTITUTE(B2,"2w","")))*2+(LEN(B2)-LEN(SUBSTITUTE(B2,"3w","")))*3+(LEN(B2)-LEN(SUBSTITUTE(B2,"4w","")))*4+(LEN(B2)-LEN(SUBSTITUTE(B2,"5w","")))*5+(LEN(B2)-LEN(SUBSTITUTE(B2,"6w","")))*6+(LEN(B2)-LEN(SUBSTITUTE(B2,"7w","")))*7+(LEN(B2)-LEN(SUBSTITUTE(B2,"8w","")))*8)/2

it dont work, the vba code should write as ?


Tom
Reply With Quote