Find and replace a character to create a new cell - vba

For example if have
" size:1, color:red CH 4"
in one cell, I want
" size:1. color:red" new cell "4"
Basically, find "CH" and replace it with a new cell and everything in front of it.
Anyone know how?

Yeap, I do this stuff all the time. You'll need two functions, they're found under the text section. THey're called SEARCH & MID.
What you are going to want to do is use an excel function SEARCH to first find the position of "CH" - this will be different depending on the length of the other strings.
Then you use another excel function, MID, to extract a substring starting from the location you found in the first function. Now, you can copy the column that just has "4" in it and use "Paste Special" from the right click menu. In paste special, choose "values". This will allow you to move the actual value the function generated and not the function itself.
Detailed Example
A1 B1 C1
size:1, color:red CH 4 =SEARCH("CH",A1)+3 =MID(A1,B1,1)
(Text we want to decode) (Returns 21) (returns 4)
+3 is because CH starts at the 19th character, then there is the "H" and a space, making the number your 3rd digit (+3). Then do the "Paste Special" procedure on cell C1, and you have isolated that value.

Related

Checking if a Word table's cell is empty - Empty cell containing a "marker"

This Word file has a giant table with 2 columns. I have to make a statement like:
If theCellIsEmpty, then
Fill it
End if
The thing is that when I get the content of the cell through:
FirstCellText = ActiveDocument.Tables(Tbl).Cell(Rw, 1).Range.Text
It doesn't get the "" I'm expecting, but it contains a sort of dot, so the text read is never empty.
Here is the dot I'd like to test for:
Moreover, I have no idea what this dot can be or where it came from. I tried to find it in the characters table without success.
Could someone explain me how to test the presence of this "end of line character" or the emptiness of the row? I don't want to check if the cell only has 1 character as if this character is something put there with a purpose, I don't want to delete it.
Also, every single line has this dot at the end...
What is it? How can I use/avoid it?
All you should need is something like:
Dim Cll As Cell
For Each Cll In ActiveDocument.Tables(1)
With Cll.Range
'if all it has is the end-of-cell marker, it's empty, so fill it
If Len(.Text) = 2 Then .Text = "Filler Text"
End With
Next
Thanks for all your help, I might have a simple solution thanks to Freeman, mostly :-)
I took the ascii number of the character to check what it is (through asc(xxxx) function) and it happens to be a "carriage return" having the ascii number "13"
Therefore, so I just need check if the content of the cell gives the ascii number 13 and it's done. (the "asc(xxxxxx)" gives the ascii number of only the first character of the text chain :) )
Of course, if the cell starts with a carriage return and there is text after, it won't be nice, so checking also if the text contained in the cell is only 2 char long will be a nice thing (this "carriage return" is apparently 2 char long... strange...) :)
If Asc(FirstCellText) = 13 and len(FirstCellText) = 2 Then

(Excel VBA) - Draw from cell text in a macro

I'm trying to build a small macro that allows the user to format multiple different documents at once.
I would like for the user to be able to enter into a particular cell within the document containing the macro a particular piece of text.
I then want for this piece of text to be able to be drawn upon in the macro while affecting a different document.
For instance, a code to add another column might say
Worksheets(1).Range("A1").EntireColumn.Insert
Instead of specifying the column (A), I would like it to draw on a value in the host document. For instance, the user types "G" into the particular cell, and then clicks a button to run the macro, and the macro will dynamically know to affect column G in all excel documents it targets based off of the value in the host document.
I hope this makes sense.
Any suggestions for the sort of functions I should be looking at to make this work?
"Any suggestions on the sort of functions I should be looking at?"
Here's a few...
To get the value which is entered...
If the cell will always be in the same address, say A1:
' Define a string variable and set it equal to value in A1
Dim cellText as String
cellText = ThisWorkbook.ActiveSheet.Range("A1").Value
or instead of using Range you can also use Cells which takes a row and column number.
cellText = ThisWorkbook.ActiveSheet.Cells(1, 1).Value
If the cell changes then you may need to look into the Find function to look for a label/heading next to the input cell. Then you can use it (easily with Cells) to reference the input...
Once you have this variable, you can do what you like with it.
To put this value into cell B3 in another (open) workbook named "MyWorkbook", and a sheet named "MySheet" you can do:
Application.Workbooks("MyWorkbook").Sheets("MySheet").Range("B3").Value = cellText
To insert a column at cellText, do
Application.Workbooks("MyWorkbook").Sheets("MySheet").Range(cellText & "1").EntireColumn.Insert
Notably here, the & concatonates the strings together, so if
cellText="B"
then
cellText & "1" = "B1"
Further to your comment about moving values between sheets, see my first example here, but always refer to the same workbook. If you find yourself repeatedly typing something like
ThisWorkbook.Sheets("MySheet").<other stuff>
then you can use the With shorthand.
With ThisWorkbook.Sheets("MySheet")
' Starting anything with a dot "." now assumes the with statement first
.Range("A1").Value = .Range("A2").Value
.Range("B1").Value = .Range("B2").Value
End With
Important to note is that this code has no data validation to check the cell's value before using it! Simply trying to insert a column based on a value which could be anything is sure to make the macro crash within its first real world use!

get multiple column names (header) in table associated with particular value in to a cell

i need to get multiple column names (header) in table associated with particular value in to a cell
as i explained, i need to get the heading names corresponding to value "n" to column E.
i used the formula
=INDEX((A$1:D$1),MATCH("n",A2:D2,0))
here. but it only give one column name.
i am open to vba scripts also. but i think it doesn't need vba. just improve the the above formula, may be. i tried and failed. any help. thank you guys
if you are really "open" to vba, I'll use one simple UDF like:
Function HeatherNames(rg As Range, rf As String) As String
For Each cell In rg
If cell = rf Then HeatherNames = HeatherNames & Cells(1, cell.Column).Value & "-"
Next cell
HeatherNames = Left(HeatherNames, Len(HeatherNames) - 1)
End Function
you can use it in the column E `=HeatherNames(A2:D2;"n") now you can select the arg.1 (range) and type (or referring to another cell) the arg.2
Assuming you have Excel 2010 or later, in E2:
=IF(COLUMNS($A:A)>COUNTIF($A2:$D2,"n"),"",INDEX($1:$1,AGGREGATE(15,6,COLUMN($A2:$D2)/($A2:$D2="n"),COLUMNS($A:A))))
Copy to the right and down as required.
It would actually be slightly more efficient (and certainly if your dataset in reality is quite large) to have the initial IF clause held within its own cell, such that it is calculated for each row only once, rather than for each instance of the formula within that row. So a better set-up would be, in E2:
=COUNTIF($A2:$D2,"n")
copied down. Then, in F2:
=IF(COLUMNS($A:A)>$E2,"",INDEX($1:$1,AGGREGATE(15,6,COLUMN($A2:$D2)/($A2:$D2="n"),COLUMNS($A:A))))
copied to the right and down again.
Regards

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.

Major formatting issue in Excel - VLOOKUP

I need help with a formatting issue in Excel, which is interfering with the VLOOKUP function in my Excel sheet.
I have two sheets with more than 20,000 column values as lookup, and the same number of values for reference. All the values in both cells are weirdly formatted, some with green triangles at the upper left corner of cells, some are text, etc.
Is there a way in Excel using a macro/VBA to remove or make all formatting similar in both sheets? The reason for VBA is because the person who is going to work with this file needs everything automated and is not familiar with Excel at all. I already have the VLOOKUP function in the cells, I just need to work with the formatting.
Well, I fight with partial lookups this way:
In the items array, I create new empty FIRST column and then place formula
="+"&B2
This will take the content of Cell B2 and add + in the front of it.
When I do vlookup, I add "+" to searcheable value
=VLOOKUP("+"&A6,A:O,2,FALSE)
Therefore, instead of comparing for example Strings and numbers, I compare Strings, by adding "+" in the front.
Another technique, is to kill all formatting:
Select whole column, click DATA-TEXT TO COLUMNS-DELIMITED and then DESELECT ALL DELIMITERS. Click Finish. This will clear your formatting.
===========================================================================
This is the VBA solution you asked for:
Call it from Excel
=GetLookup(G2,A:C)
Here is VBA:
Function GetLookup(LOOKFOR As String, RANGEARRAY As Range) As String
GetLookup = Application.WorksheetFunction.VLookup("+" & LOOKFOR, RANGEARRAY, 3, False)
End Function
Good luck!
I'm assuming the data type in all of the cells is the same, or you want it to all be the same. The following steps will make the cells a uniform type:
Save your workbook, in case this does not do as you require
Select all cells you wish to be of the same cell type
Press Ctrl+1, on the "Number" tab, select the type you wish these cells to take. Press OK.
Open the VBA editor using Alt+F11
Open the immediate window with Ctrl+G
Type the following: for each cell in selection : cell.formula = cell.value : next cell
Press enter (you may have to wait a few seconds).
If you take this action with the same data type (e.g. choose "Text" for both ranges in step 3) on both your ranges you should be "comparing apples with apples" and your VLOOKUP should function as required.
Hope this helps.
Edit: formatting, clarification