VBA: Copy number from a string of text and insert into cells below - vba

I want to create a makro in Excel which performs - after pressing a button - the below. I attached some dummy data that is formatted like the actual sheet.
There are several data blocks that are seperated by headlines in Sheet 1. From these headlines, I want to get a string of numbers and put it into column S for each line with data below that heading. In line 6 the heading says "2000", thus lines 8-19 should have a "2000" in column S and so on. The number I want to get is always after the word "Monthlyaccount" but the lenght of the number can be different - from 1 to 7 digits.
For lines where no data is given (data lines might be recognized by looking if in A is a valid date given) there should just be "ERR" instead of the number.
Can anyone help out?
Thanks so much upfront!

Say the string (in cell A1) contains "Monthlyaccount" followed by a blank followed by a number followed by another blank. To extract the number, use:
=--LEFT(MID(A1,FIND("Monthlyaccount",A1)+15,9999),-1+FIND(" ",MID(A1,FIND("Monthlyaccount",A1)+15,9999)))

This does as requested. Paste the following into S3 and drag down
=IF(AND(ISNUMBER(RIGHT(A2,4)*1),ISNUMBER(RIGHT(A3,4)*1)),S2,IF(ISNUMBER(RIGHT(A3,4)*1),LEFT(RIGHT(A1,LEN(A1)-FIND("Monthlyaccount",A1)-14),FIND(" ",RIGHT(A1,LEN(A1)-FIND("Monthlyaccount",A1)-14))),"ERR"))

Related

Excel VBA: How to search and copy a value of a certain pattern found within large texts in a column to another column?

As shown in the image above, I need to get the values following a pattern like for example TK AK15590 (TK AK followed by a series of numbers as shown) into cell B2. The same with the rest of the cells in the description column using vba macro in excel. Appreciate if anyone could give me a sample vba code or some information on how I could do this.
VBA is not requiered here. You should use formulas that manipulate text. In this particular case, the following would work:
=IFERROR(MID(A2,FIND("TK AK",A2),FIND(" ",A2,FIND("TK AK",A2)+4)-FIND("TK AK",A2)), "ERROR MESSAGE HERE")
When the length of the string you want to extract is fixed (10 in this case), you could simply use this:
=IFERROR(MID(A2,FIND("TK AK",A2),10), "ERROR MESSAGE HERE")

VBA script in excel to find and highlight text

I am looking for a VBA script that will help me find certain keywords in a cell and if found highlight the entire row. Below are my requirements:
I have a database of words eg hell, get out, shut up, don't you dare etc. I need a macro to search the data in column "E" of excel and in case any of the cell in column "E" contains any word listed in the database (irrespective of the case of the word upper or lower)the entire row is highlighted. The word can be in the beginning, middle or end of the cell and the macro should be able to find that word and highlight the column.
Seeking help from all VBA masters for this.
You can do this with conditional formatting, instead of VBA.
Conditional formatting works by applying a 'second formula' to a given cell. If the 'second formula' results in TRUE, then special formatting conditions can be applied.
EXAMPLE CONDITIONAL FORMATTING
For example, if you have a single column of Data, A:A, and you want to check if that column has the exact string "hello world", you could add a conditional format [Home ribbon, Styles section, Conditional Formatting] that turns a cell yellow with this formula:
=$A1="hello world"
This will only result in TRUE if the cell in column A at that row equals exactly "hello world" [note that Column A has an absolute-reference $, and row 1 does not, so row 1 is relative to the position of the cell in the condiitonal format rule].
To check to see if any row in column A includes hellow world, we need to add a SEARCH function, which checks to see if a small search string is inside of a larger string:
=SEARCH("hello world",$A1)>0
Because SEARCH by default returns the first character in a larger string that matches the search term (and if it finds nothing, it returns #N/A), we check to see if our search for "hello world" in column A returns a number.
SEARCHING MULTIPLE COLUMNS
Now, to see if ANY column, say from A-D, includes "hello world", we concatenate each value of each column so that it gives us a single string, which we can search through for "hello world", like so:
=SEARCH("hello world",$A1&$B1&$C1&$D1)>0
This will first create a single string, equal to A1 & B1 & C1 & D1 all in a row. Then it will search that newly created string to see if "hello world" is inside it, and return a number value if it is.
ARRAY FORMULA BASICS
Finally, we need to do the tricky part - searching for multiple terms instead of just "hello world". This is called an Array Formula. An array formula works by performing a single operation on multiple cells, and then returning multiple results in an Array. In an Excel sheet, an array formula must be confirmed with CTRL + SHIFT + ENTER (instead of just ENTER), but in conditional formatting, you actually don't need to do anything special - it will recognize an array formula without a special command.
As an example of conditional formatting, see this example, which checks whether any value from A1:A5 = 10, and if it does, it gives us the value in B1:B5:
=IF(A1:A5=10,B1:B5,"")
Remember in Excel on a worksheet, this would be confirmed by pressing CTRL + SHIFT + ENTER. If you do test this, it will give you the following result, assuming A2 = 10 and A5 = 10:
={"";B2;"";"";B5}
This result would actually be hidden, because Excel can't "collapse" an array function on its own. So assume column B had values, and we actually want to sum them together. We would then wrap the Array formula in a SUM function:
=SUM(IF(A1:A5=10,B1:B5,""))
As you can see if you test this, we have actually created our own SUMIF function, using Array formulas instead of the built-in SUMIF.
SEARCHING FOR MULTIPLE TERMS WITH ARRAY FORMULAS
So now we apply these principles to the conditional formatting, to create an array formula which will check our concatenated 'NEW STRING' for any number of provided terms, as follow [Assumes the search terms are typed into cells E1:E10]:
=SUM(SEARCH($E$1:$E$10,$A1&$B1&$C1&$D1)>0)
This formula can be placed as a conditional formatting rule which reaches all of A:D. Set the rule to highlight / change format in whatever way you like.

Get the id of the selected value in a dropdown validation list on Excel in VBA

Using Excel, lets say I have a validation list made of 5 values like this one :
Patate
Tomate
Courgette
Concombre
Patate
In a cell containing a drop down list made of these 5 value, I select the fifth value : "Patate".
I want to get in VBA that this cell contains the 5th value of my validation list. Something like :
x = Cell.Validation.GetIDValueSelected
x = 5
I can't use Vertical search because I might have 2 or even more time the same value in my list (too long to explain why).
This list is also dynamics (depending of another sheets) so it doesn't always contains 5 values.
I hope I made it clear for everyone to understand my needs but I will be glad to add more information if needed.
Thank you for your time.
Sadly, once you have used DV to fill a cell with junk, there is no way to tell which piece of junk you picked:
You would have to pad each piece of junk with a different number of blanks.

VBA/EXCEL Dynamic Named Range That Includes Both Text and Numbers

Information
I have a column of about 300 part numbers that is constantly expanding. These part numbers can be only numbers, only letters, or a combination of both. I'm using dynamic named ranges with the part numbers to fill combobox lists. When the user types in the part number in the combobox, if we've done that part before it will autofill as they keep typing. I've recently ran into a problem with this however with a recent group of parts that was added.The part number is just a six digit number, and are the only part part numbers that solely consist of numbers, every other one has letters, or if its just numbers there are hyphens in the part number. The combobox I'm filling with this named range does show these numerical part numbers in its list, but does not autofill them when the user types and does not autofill the other information associated with the part number in other textboxes when the part number is done being typed in.
Question
Can anyone help me understand why it isn't autofilling the numerical value but it fills every other value?
Code
Here is the code I'm using to fill the combobox list and the dynamic range formula. Let me know if you need anything else to help you give an answer.
=OFFSET('Part List'!$A$1,0,0,COUNTA('Part List'!$A:$A),1)
comboxPartNumber.List = Range("Part_Number").Value
List of Sample Parts
X-600-ASSY-SM
LO-5093-020-023
LO-5093-020-025
AB-1541754
764761
766415
I cannot add a comment, sice I am 2 points short :(, so I am forced to post this as a solution:
Try to make all the numbers strings.
Dim R As Range
For Each R In Range("Part_Number").Cells
If IsNumeric(R.FormulaLocal) Then R.FormulaLocal = "'" & R.FormulaLocal
Next
Let me know if it worked.

Number to be copied from cell with the same number format and show it as a label for a shape without any change in the number format

I have a cell in excel which contains a value, lets say 100000.
Now i want this value to have commas in between them to represent the thousands and millions i.e. 100,000. I can do this by changing the number format in the home menu.
Now i want this value to be copied from that cell and paste it as a label for a shape. When i am doing this the commas go away showing me just the numbers.
I want it to happen through VBA but this is not happening in excel itself.
Does anyone have a plausible solution for this?
In range object use Text property, like this:
Sheet1.Shapes(1).TextFrame.Characters.Text = Range("A1").Text