Search and Replace random cell text by first : - excel

I have a slew of rows (100K+) to search and modify the contents.
For example the cells contain similiar text as DGC9610411:DB:10:82
All of this text can change per row except that fact that the : means something to me.
In this and every other row, i need to remove the first : and all the text after so that the cell would look like this DGC9610411
Next I will be adding the contents of another cell to the end. I think that will be an easy step and I could most likely figure that out without much effort.
I have this code in a while loop for each row. so the code is looking at one row at a time.
I have searched but everyone seems to have a different set of needs.

Just use Find and Replace, no need for vba or formulas.
Select the column containing the data that you need to modify
Press Ctrlh to open the Find and Replace dialog.
In the "Find what:" field, type :*
In the "Replace with:" field, leave it blank
Click Replace All
EDIT: If it has to be VBA, this simple macro will accomplish the same thing. Be sure to change the A to your actual column letter.
Sub tgr()
Columns("A").Replace ":*", ""
End Sub
EDIT: Upon request I am adding a formula solution. In cell B1 and copied down:
=LEFT(A1,FIND(":",A1&":")-1)

Try this small macro:
Sub colonoscopy()
Dim c As String
Dim r As Range, I As Long
For Each r In ActiveSheet.UsedRange
v = r.Value
I = InStr(1, v, ":")
If I > 0 Then
r.Value = Mid(v, 1, I - 1)
End If
Next r
End Sub

Related

Excel Changes number value automatically

I´m trying to remove the dollar sign in a column which should only include numbers. Therefore I tried to to use simply change the cell format to number but nothing changed.
Now I copied the values inside a text editor and removed the dollar signs. After inserting excel automaticallly changes some values to different numbers.
For Exaxmple it changed 8.59 to 21763,00. When I change the cell format to standard then it displays me something like 28 Jan except 8.59.
In this picture I tried to illustrate my problem with the different columns. Sold Price in Thousands is the original column which I liked to change.
Select the cells you wish to fix and run this short macro:
Sub FixData()
Dim r As Range, s As String
For Each r In Intersect(Selection, ActiveSheet.UsedRange)
s = r.Text
If Left(s, 1) = "$" Then
r.Clear
r.Value = Mid(s, 2)
r.NumberFormat = "0.00"
End If
Next r
End Sub
this is a known issue and the only work around that you can use is the following:
Copy the correct values in notepad.
From notepad make a Find and Replace in order to remove the $ sign.
Select a blank column in excel and set its format to TEXT.
Only now you can copy back the values from notepad to the new TEXT column.
This should fix your issue.

Identify second line of text in a cell with VBA

I have a bunch of cells that have two lines of text in each cell after pulling the data into Excel.
What I am looking to do is use a macro with the Trim function to remove everything in the cell after the second line.
I'm puzzled with this data, it's as if you were to enter data in a cell and Enter down to the next cell, but it's one cell and is not merged.
ex.
Someone's Name [123]
Procedure room, procedure done
Is there a way to identify this line break?
thanks so much for any assistance, my heads spinning and I'm punching out for the day.
cheers
Just look for the ASCII-10. Select the cells you wish to process and run:
Sub KleanUp()
Dim r As Range
For Each r In Selection
r.Value = Split(r.Value, Chr(10))(0)
Next r
End Sub
Only the first line will be retained in each cell.
EDIT#1:
As Ralph points out, ASCII-13 should also be considered, therefore:
Sub KleanUp2()
Dim r As Range
For Each r In Selection
r.Value = Split(Replace(r.Value, Chr(13), Chr(10)), Chr(10))(0)
Next r
End Sub
This converts to a single-style line-break.
EDIT#2:
It is possible to improve the performance of the sub by:
reading the data into a VBA array as a single large block
looping through the array (in VBA)
transferring the data back to the worksheet in a single block

Is there a shortcut to edit thousands of excel formulas?

I want to change references in formulas, from
=IF(N14="YES","A","B")
to
=IF($N$14="YES","A","B")
Fine on a single cell, my problem is that I have 20 x 1000 cells to edit like this. Is there a quick way to do it?
I don't think find and replace can be applied, as both row and column change (to be edited formulas spread over 20 rows and 1000 columns), so in the best case I need to do it 20+1000 times.
Select the desired range and run this code.
Sub RelativeToAbsolute()
Dim c As Variant
For Each c In Selection
If c.Formula <> "" Then
c.Value = Application.ConvertFormula(c.Formula, xlA1, , xlAbsolute)
End If
Next c
End Sub
Find and Replace. Find N14, Replace with $N$14, Look in Formulas.
If that does not fit the situation, edit your question and explain the situation in more detail, then post a comment.

Excel: search for information

Guys I need to find specific information in text, and write them down in column.
So, I have Column L with Long Description, and I have Column M with words that I need to find in Long Description. When word has been found write that word in Column N at same row as Long Description.
I tried coding this one but ain't work.
=INDEX(M1:M4;MAX(IF(ISERROR(FIND(M1:M4;L1));-1,1)*(ROW(M1:M4)-ROW(M1)+1)))
This is sample of what I mean.. Pleaaase really need help.
This UDF should do the trick - I won't provide a detailed description of what it does beyond the comments you can see in it, as it's not very complex piece of code, but if there is something you don't understand in it, feel free to ask.
Option Explicit
Function find_keywords(cell_to_search As Range, range_of_keywords As Range) As String
Dim c As Range
' Check for the value of each cell in the range of keywords passed to the function
For Each c In range_of_keywords
' If the string-value we search for is in the cell we check against, we add it to the return-value
If InStr(1, cell_to_search.Text, c.Text, vbTextCompare) > 0 Then
' We don't want a comma before the first keyword we add, so check for the length of the return value
If Len(find_keywords) > 0 Then
find_keywords = find_keywords & ", "
End If
find_keywords = find_keywords & c.Text
End If
Next
End Function
You need to paste the code above into a module in your workbook, and then enter the formula into the cell you want the return-value to as normal:

How to view text of merged cells when filtering another cell?

The two columns look like on this image.
When I want to show only the cells which contain a letter 'b', I can no longer see the text "Title1" and "Title2" which is normally visible in the column B.
I guess although the cells in column B are merged, the text is still bound to A3, respectively to A7.
So how can I at the same time filter the visible content and preserve the merged text? In simple words, I want to filter content by letter 'b' and I still want to see the text "title 1/2" in the column B.
You tagged excel so here is a solution in excel:
You need to click on that column with the merged cells and unmerge all cells.
Then you need to put this formula at the top of your list and enter it with ctrl+shift+enter(this will enter it as an array formula):
=OFFSET(C3,MAX(IF(NOT(ISBLANK(C$3:C3)),ROW(C$3:C3),0))-ROW(C3),0)
Then you need to autofill that down.(this function seems a little verbose but I just got it online - there is probably a simpler way to do this - but it finds the last nonblank cell in a range).
I think openoffice has similar functions so you should be able do the same or something similar in openoffice.
Alternatively if you are using excel you could click on the column you want to unmerge and run this macro:
Sub UnMergeSelectedColumn()
Dim C As Range, CC As Range
Dim MA As Range, RepeatVal As Variant
For Each C In Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
If C.MergeCells = True Then
Set MA = C.MergeArea
If RepeatVal = "" Then RepeatVal = C.Value
MA.MergeCells = False
For Each CC In MA
CC.Value = RepeatVal
Next
End If
RepeatVal = ""
Next
End Sub
Good Luck.
EDIT:
I found a Non-VBA solution that will work in both excel and openoffice and doesn't require you to enter it as an array formula(with ctrl+shift+enter):
=INDEX(B:B,ROUND(SUMPRODUCT(MAX((B$1:B1<>"")*(ROW(B$1:B1)))),0),1)
In open office I think you want to enter it like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0),1)
or maybe like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0))
You just need to autofill that formula down:
Your main problem seems to be the one "blank row" that you have left after the filter fields.
Remove it, and it will work fine.

Resources