#1   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Programming

Hi all
I have a problem.
I want to write a program using VB in Excel.
I put a txtbox and a cmdbutton in my worksheet. When I click on cmdButton, I
want my program to do this:
See if the word in the text box, exists in column "A" or not. If it exists
in column A, then its related number in column B must get 1 grade higher
than before, but if the word doesn't exist in column "A", add the word to
the end of the column.
For example if these are my columns:
A B C
1 Name No
2 apple 10
4 banana 2
5 Lemon 15
6

Now if the word in txtbox is "Apple" , then the worksheet must be like this:
A B C
1 Name No
2 apple 11
4 banana 2
5 Lemon 15
6

But if the word is "Cherry", the worksheet must be changed into this:

A B C
1 Name No
2 apple 10
4 banana 2
5 Lemon 15
6 Cherry 1

Hope its clear. How can I do this? I don't know how to search for a word in
a column or how to find the position of end of a column.
Any help is appreciated.
Thank you
Javad


  #2   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Programming

You can find that next open cell in column A with something like:

dim NextCell as range
with worksheets("sheet1")
set nextcell = .cells(.rows.count,"A").end(xlup).offset(1,0)
end with

It's kind of like going to A65536, hitting the End key, then up arrow. Then
hitting the Down arrow to get to the open cell.

You can check to see if the value is in the range (A2:A(lastrow)) in a few ways.

One of them:

dim res as variant
dim myRng as range
dim myStr as string
dim NextCell as range
dim myValue as variant

'not sure where that textbox is.
mystr = textbox1.value

with worksheets("sheet1")
set myRng = .range("a2",.cells(.rows.count,"A").end(xlup))
set nextcell = .cells(.rows.count,"A").end(xlup).offset(1,0)
end with

if mystr = "" then
'do nothing
else
res = application.match(mystr,myrng,0)
if iserror(res) then
'not found
nextcell.value = mystr
nextcell.offset(0,1).value = 1
else
'was found
myvalue = myrng(res).offset(0,1).value
if isnumeric(myvalue) then
myrng(res).offset(0,1).value = myvalue + 1
else
msgbox "Non-numeric value!
end if
end if
end if


=======
I'm not sure where/what the textbox was--on a userform on a worksheet (and from
the Drawing toolbar or from the Control toolbox toolbar).


===
Just another option to look for existing values:

if application.countif(myrng,mystr)0 then
'found one
else
'didn't find one
end if




Javad wrote:

Hi all
I have a problem.
I want to write a program using VB in Excel.
I put a txtbox and a cmdbutton in my worksheet. When I click on cmdButton, I
want my program to do this:
See if the word in the text box, exists in column "A" or not. If it exists
in column A, then its related number in column B must get 1 grade higher
than before, but if the word doesn't exist in column "A", add the word to
the end of the column.
For example if these are my columns:
A B C
1 Name No
2 apple 10
4 banana 2
5 Lemon 15
6

Now if the word in txtbox is "Apple" , then the worksheet must be like this:
A B C
1 Name No
2 apple 11
4 banana 2
5 Lemon 15
6

But if the word is "Cherry", the worksheet must be changed into this:

A B C
1 Name No
2 apple 10
4 banana 2
5 Lemon 15
6 Cherry 1

Hope its clear. How can I do this? I don't know how to search for a word in
a column or how to find the position of end of a column.
Any help is appreciated.
Thank you
Javad


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CD Programming nelson Excel Discussion (Misc queries) 0 June 4th 06 04:32 PM
programming ernie Excel Discussion (Misc queries) 4 March 13th 06 02:06 PM
Please help with programming RandyJ Excel Programming 2 October 8th 04 10:59 PM
Programming lag Ernst Guckel[_3_] Excel Programming 4 June 9th 04 03:40 PM
How to add via programming ? Milind Excel Programming 3 September 10th 03 11:57 PM


All times are GMT +1. The time now is 02:21 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"