I have a worksheet with 2 columns “Key” and “Value”. by VBA code, i want search Input_key on Key columns, if not exist, I will add new row [input-key]-[input-value]. How do i code?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You will realise from the comments that “please solve my problem for me” questions are not popular.
I will guess that you do not know where to start and will give you some initial guidance.
Go to Google and type in “excel vba tutorial”. You will be offered many sites. They are all different so try a few and find one that is right for you.
Try the macro recorder. I set up a worksheet which matches your description, switched on the macro recorder, selected column A, clicked
Ctrl+Fto get the Find screen and clicked the option button to show me all the options. The result is:Look at the options. For example, is case important? Select as required. I ticked “match entire cell contents”, entered “k” and clicked
Find Next. The cursor jumped to the cell containing “k”. I then switched the macro recorder off.The code saved for me was:
This is valid VBA but is not good VBA. The macro recorder has recorded each action as you performed it. It does not know your intentions. So we need to tidy this code up.
The key changes are:
Copy the macro below to the module in which the macro recorder saved its code. I created this macro by amending the saved code to create a test vehicle for you to play with. It asks for a value, searches for it in column A and says whether the value was found or not. This is the basis of the code you need.
Now switch the macro recorder on again. Position the cursor in column A below any rows with values. Click
Ctrl+UpArrow. The cursor will jump to the last value in column A. Switch the macro recorder off.The saved code will look like:
End(xlUp)is the VBA forCtrl+UpArrow. It is the easiest way of finding the last used row.To add a new row, which you want to do if the value is not found:
If you look at other questions you will discover that
Endwill sometimes not give you the result you expect. TryCtrl+DownArrowandCtrl+UpArrowon a empty column, a column with one then two values at the top, a column with one then two values at the bottom and a column with several values separated by blank rows.This should get you started. Welcome to Excel programming. Good luck.