Trim string starting at comma - Excel - excel

I have an csv sheet with data of Employee as Last Name, First Name.
Below is an example. (These values are in a single cell)
Flynn, Jeremy
Early, Allison
Epstein, David
Newman, Joanna
Biord, Brooke
I need to left trim the data so that I only need the first name without any trailing space or comma.
OutPut Sample should be:
Jeremy
Allison
David
Joanna
Brooke
How can I write a formula or macro that will process the entire sheet of more that 5000 records.

Formula, in an empty column put:
=TRIM(MID(A1,Find(",",A1)+1,LEN(A1)))
Where A1 is the first cell of your list. Then copy down the entire list.
If you want vba this will do it nearly instantly:
Sub firstName()
Dim rng As Range
With ActiveWorkbook.ActiveSheet
'Change the Second Criterion in Each .Cells() to the column number of your data.
Set rng = .Range(.Cells(1, 1), Cells(.Rows.Count, 1).End(xlUp))
'Change the Offset value to the number of columns to offset the answer.
rng.Offset(, 1).Value = .Evaluate("Index(TRIM(MID(" & rng.Address & ",Find("",""," & rng.Address & ")+1,LEN(" & rng.Address & "))),)")
End With
End Sub
It assumes you data is in column A and it will put it in column B

As a formula in an used column to the right using the REPLACE function,
=REPLACE(A1, IFERROR(FIND(",", A1), LEN(A1)), LEN(A1), TEXT(,))
As a VBA sub procedure using Range.Replace method,
Sub firstOnly_bySel()
With Selection
.Replace what:=",*", replacement:=vbNullString, lookat:=xlPart
'selection.replace(
End With
End Sub
Select one or more cells and run the sub from the Macros dialog ([alt]+[F8])

Just do simple find and replace. Find ",*" and Leave Replace blank.

Related

Insert Row Every Two Rows and Subtract the Difference

I have numerous excel sheets that contain rows that have paired data. Specifically, I need to subtract the first row from the one that follows (e.g., row 2-row 1; row 4-row3; etc.) and place the result into a new row below each pair. My data in each sheet appear as follows:
I am not new to programming languages, but I am new to visual basic.
My current code is:
Sub test() Dim rng As Range
Columns(1).Insert
With Range("b2", Range("b" & Rows.Count).End(xlUp)).Offset(, -1)
.Formula = "=if(mod(row(),2)=1,1,"""")"
.Value = .Value
.SpecialCells(2, 1).EntireRow.Insert
End With
Columns(1).Delete
With Range("a1", Range("a" & Rows.Count) _
.End(xlUp)(2)).Resize(, 3)
.Columns(1).SpecialCells(4).Value = "Difference"
Union(.Columns(2).SpecialCells(4), .Columns(3) _
.SpecialCells(4)).Formula = _
"=r[-1]c-r[-2]c"
End With
End Sub
However, the result is this:
I am mainly interested in calculating the differences between row pairs in the first column shown, but it is clearly not working.
Any help would be greatly appreciated!
Easier to use formulae, rather than VBA.
Go to a second sheet in the file ("Sheet2")
Enter in A1: =Sheet1!A1-Sheet1!A2
On Sheet2, select Rows1 AND 2.
Drag down.
Then depends on what you need to do.
May be Copy | Paste Special | Values to Sheet3, and sort to remove blank rows.

Countif with reference to different sheet

Hello I will need help with problem I am facing right now and even Google couldn't help me.
I would like to add to field AS2 a COUNTIF formula with source information from different sheet.
This COUNTIF should jump to sheet ee_lpk and then take a range from column A2 down to the end of last used row. and compare that with criteria from field D.
so for AS2 will be comparing with D2 for AS3 with D3.
When I recorded that it showed:
ActiveCell.FormulaR1C1 = COUNTIF(ee_lkp!R[-143]C[-44]:R[217]C[-44],R[-143]C[-41])"
this is working but just in case that there is on ee_lpk page same number or rows what is changing from day to day.
Any help will be much appreciated.
Martin
You need to break this problem down using variables. Try something like this:
sub Answer()
Dim srcRng as Range
Dim srcLength as Long
'First find how many rows on sheet ee_lpk and store it as a variable
srcLength = Sheets("ee_lkp").UsedRange.Rows.Count
'Then use that variable to get your range
Set srcRng = Range(Cells(2,1), Cells(srcLength, 1))
'Or another viable option would be:
'Set srcRng = Range("A2:A" & srcLength)
'Then put that in your Countif formula
ActiveCell.FormulaR1C1 = _
"=COUNTIF(ee_lkp!" & srcRng.Address(True, True, xlR1C1) & ", R[-143]C[-41])
End Sub

Return cells content from range

Yesterday I learned here how to copy a row to a second sheet.
Sub maJolieProcedure(Texte As String)
With Worksheets("employes").Range("A:A")
Set c = .Find(what:=Texte)
If Not c Is Nothing Then
firstAddress = c.Row
Worksheets("employes").Rows(firstAddress).Copy _
Destination:=Worksheets("rapport").Range("A1")
MsgBox "Ok"
Else
MsgBox "Nok"
End If
End With
End Sub
To respect the formatting of the second sheet, I want to copy and paste the contents of each cell one by one.
I can identify the line number. However, I can't figure out how the Range object can return each cell one by one. For example, C3 content if Rows = 3.
Thanks a lot.
If you don't want to paste the formatting from one range to another paste values only.
Worksheets("employes").Rows(firstAddress).Copy
Worksheets("rapport").Range("A1").PasteSpecial xlValues
That's the same for all ranges, whether 1 cell or a million. The Copy process copies the subject to memory as one block. Any parsing must be done before the Copy instruction. An alternative is to read a range into an array.
Dim Arr As Variant
Arr = Worksheets("employes").Rows(firstAddress).Value
This will create a 3D array of 1 row and about 16000 columns. You might decide to limit your enthusiasm to only what you need.
With Worksheets("employees")
Arr = .Range(.Cells(firstAddress, 1), .Cells(firstAddress, .Columns.Count).End)xlToLeft)).Value
End With
Pay attention to the leading periods within the With statement. Each such period stands for the object mentioned in the With statement.
If your goal is to respect the formating of the second sheet, you don't need to loose time copying cell by cell.
It is more effective to do a paste special, like you do with the mouse:
Range("A1").Copy
Range("B1").PasteSpecial Paste:=xlPasteValues
works very well also with bigger ranges if you need:
Range("A1:A12").Copy
Range("B1:B12").PasteSpecial Paste:=xlPasteValues
or even
Range("A1:A12").Copy
Range("D3").PasteSpecial Paste:=xlPasteValues
If your goal is to really access all cell of a range individually , you just iterate on the range. For example:
For Each cell In Range("A1:A12")
cell.Value = cell.Value + 2
Next cell

Use value from one cell to define "coordinates" of another cell

I want to use the value of one cell, e.g. 3 to specifiy the coordinates of another cell.
This is what I tried:
Wert = Range("E22").Value
Sheets("Tabelle2").Range("A1:BWert").Copy
When cell coordinates are calculated you will find it more expedient to specify cells as `Cells([Row], [Column]) and define a range by its first and last cell. When you employ this syntax your task would be solved like this:-
With Sheets("Tabelle2")
.Range(.Cells(1, 1), .Cells(Wert, 2)).Copy
End With
The variation below is equivalent and perhaps easier to read for a beginner. However it is logically inconsistent because column letters can't be calculated.
With Sheets("Tabelle2")
.Range(.Cells(1, "A), .Cells(Wert, "B")).Copy
End With
I tried it in my actual code and it did copy it however it pasted it from A1 and A1 to like J 1 and J2. heres my entire code: It was supposed to copy the range A1:B10 from Table2 to A1:B10 in Table3.
How could this be done?
Option Explicit
Sub Kopieren()
Dim Wert As Integer
Wert = Range("E22").Value
Sheets("Tabelle2").Range("A1:B" & Wert).Copy
Sheets("Tabelle3").Range("A1:B" & Wert).PasteSpecial xlPasteValues, Transpose:=True
End Sub```

Applying formula to range of cells, then pasting values

I need to apply a formula/function (the DOLLAR function) to a range of cells, then cut and paste just the values.
I have a heading row and use Find to get to the column that I need. Then with all of the cells below, I want to apply the DOLLAR formula. However, the result at the end of my macro needs to be that those cells now contain just the value after applying the formula, and not the formula itself (this is because I'm going to use the sheet to run a mail merge so it can't have formulas).
I've tried running Do While and other cut/paste methods. I just don't have the skill to get it done :(
Sub ProjectSummaryAdjustment()
Workbooks("Project Summary").Activate
Range("A1",Range("A1").End(xlToRight)).Find("TD Budget Effort").Select
This is as far as I've gotten with the code. Now for all the cells below, I want to apply the DOLLAR formula, then have just the values pasted there. I don't mind if it has to cut and paste to the first empty column and then cut and paste back to the original column.
First make sure that you should avoid Select and Activate. Then you can simple use evaluate to do the conversion. You do need to set the field as text first:
With Workbooks("Project Summary").Worksheets("Sheet1") 'Change to your sheet name
Dim rng As Range
Set rng = .Range("A1", .Range("A1").End(xlToRight)).Find("TD Budget Effort")
'Test to make sure the header was found
If Not rng Is Nothing Then
With .Range(.Cells(2, rng.Column), .Cells(.Rows.Count, rng.Column).End(xlUp))
.NumberFormat = "#"
.Value = .Parent.Evaluate("index(DOLLAR(" & .Address & "),)")
End With
End If
End With

Resources