I have a column (A) of values that I would like to trim to the left. Each value in column A has a / in it which separates a word in German and it's English translation. For example one value would be "nein / no". (note there is a space between the two words.
What I want to do is write a trim function that starts after the space that follows the /. In othe words I want the value in my example to trim from "nein / no" to just "no". The part that confuses me is the fact that each value changes in size so I don't know how to tell excel where to trim from. My code should look something like this
For each cl in range("A1:A300"). Trim cl.value. Next cl
Try below code :
Dim start As Long
For Each cl In Range("A1:A300")
str = c1
start = InStr(1, str, "/", vbTextCompare) + 1
strVal = Trim(Right(str, Len(str) - start))
msgbox strVal
Next cl
Start with something like this:
For each cl in Range("A1:A300")
With cl
.Value = Trim(Len(.Value) - Right(.Value, _
Application.WorksheetFunction.Find("/", .Value)))
End With
Next
You don't need VBA to solve this. A simple formula will suffice:
=RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND(" ",A1)+1))
nein / no = no
lederhosen / breeches = breeches
doppelganger / doppelganger = doppelganger
Related
I use an =IF function
=IF(RIGHT(A1;1)="-";"-"&LEFT(A1;LEN(A1)-1);A1)*1
to shift a minus sign from the end of the cell to the beginning but I'd like to use it in a macro so it is performed on the same column (or the same selection)...
1) Use the the For Each.... loop construct to loop through each cell in a range.
2) If you are wanting to convert "numbers" of the 123- to -123 to a proper number and not text, use the Val command to convert a string to a number.
Note however that if you if you have cell with something like "ABC-", this will become -ABC which VBA then attempts to convert to a number ...and produces zero as a result
Sub MoveMinus()
Dim c As Range
For Each c In Intersect(Selection, Selection.Worksheet.UsedRange)
If (Right(c, 1) = "-") Then
c = Val("-" & Left(c, Len(c) - 1)) 'Val to make the result numeric
End If
Next
End Sub
I have two columns M and U containing employee time record in an Excel sheet but the time format is wrong it is something like 08:13a. I want it look like 08.13 in order to use subtraction formula like =U2-M2 to get the difference. so I want to replace : by . and get ride of the last character using Excel VBA.
if 8:13a is really 8 hours, 13 minutes, then running:
Sub TimeFixer()
For Each r In Selection
ary = Split(Mid(r.Text, 1, Len(r.Text) - 1), ":")
r.Value = TimeSerial(ary(0), ary(1), 0)
r.NumberFormat = "hh:mm:ss"
Next r
End Sub
will produce a time you can use numerically.
EDIT#1:
Formula:
=TIMEVALUE(LEFT(A1,LEN(A1)-1))
for example:
Or in the code replace Selection with something like Range("A1:A100")There are many ways to skin this cat.
My column S needs to only contain one word, however, on import it usually has about three words separated by commas. I would like to only keep the first word in the cell.
For example, my S1 column has something like "x, y, z" and I would like to make S1 only say x. So basically, I am trying to remove everything to the right of the first comma in the cell. I have tried to use Replace, but that isn't working. Is there a simpler way to do this?
My (failing) macro right now:
For i = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
If Cells(i, 19) = "," Then Columns("S:i").Select
Selection.Replace What:=Trim(Right(ActiveCell.Value, 25), InStr(ActiveCell.Value, ",") - 1), Replacement:=""
Next
What you are looking for is a Split function.
It works like this:
Dim str As String: str = "x, y, z"
Debug.Print Split(str, ",")(0)
Result is x
The important parameters for Split are string and delimiter, the number after the function specifies the occurence. So if you wanted to select y, you would put 1 there etc. In that case, you would probably include the space in the delimiter so you would not need to trim the result further.
You can use left with Instr to give you all the characters before the first comma.
ActiveCell = Left(ActiveCell,Instr(ActiveCell,",")-1)
Thanks to #pnuts I was able to complete this task. I was unaware that wildcards could be used in Replace. This is my (working) macro:
Columns("S:S").Replace What:=",*", Replacement:=""
I have a variable like this:
var = "word1 (should be replaced by word1)"
I need to get word1 by itself and sometimes it may be a word(s) but it will always have (*) after it. Any way to delete text between the () signs, then remove the (), then remove any space(s) after the last word?
Not sure of any way to go about it.
The simplest would be:
var = Trim(Mid(var, 1, InStr(var, "(") - 1))
InStr returns the index of the first occurrance of "(". I subtract 1 to discount the position held by the "(".
Mid cuts the string from the first character at position 1, to the index returned by the InStr.
Then Trim to remove trailing spaces.
Like this?
Sub Sample()
Dim sVar As String
Dim MyAr() As String
sVar = "word1 (should be replaced by word1)"
MyAr = Split(sVar, "(")
Debug.Print MyAr(0)
End Sub
EDIT
The above code can be shortened further
Sub Sample()
Dim sVar As String
sVar = "word1 (should be replaced by word1)"
Debug.Print Split(sVar, "(")(0)
End Sub
I would use SPLIT function.
arrayVar2 = split(var, "(" )
You will now have an array with 2 elements.
Split the second element
arrayVar3 = split( arrayVar2[1], ")" )
Then join:
out = arrayVar2[0] & arrayVar3[1]
That will work as long as there is only ever a single set of brackets in the string.
The alternative way is to make use of the regular expression library but that is a longer answer. It wouldn't be as efficient but would be better if needing to handle more complex strings.
UPDATE: I went a bit further than the question perhaps called for. My solution ensures that the bracketed text is removed - any text AFTER the bracketed text is also retained.
I have a list of city names followed by the state in which they are located all in one column in Excel. How can I delete everything after the first space so that the city name is the only thing that's left in the cell?
example: A1 = "johnson TX"
should be just A1= "johnson"
I assume you want a VBA solution since you tagged your question excel-vba.
This works:
Sub KeepCity()
Dim strCityAndState As String
Dim strCityOnly As String
strCityAndState = Range("A1").Value
strCityOnly = Left(strCityAndState, InStr(strCityAndState, " ") - 1)
Range("A2").Value = strCityOnly
End Sub
If you don't want VBA and want a cell formula instead, then #JToland's answer works fine, though this one is more concise and doesn't keep the trailing space character:
=LEFT(A1, FIND(" ",A1)-1)
Well doing something like this
=Mid(A1, 1, Find(" ",A1))
in another column should grab all text before the " ". So you could build another column with just all the city names and then you could use that column for your purposes.
If you are looking for a VBA function, you can use Left and InStr as shown below.
Dim Temp As String: Temp = "Hello_World! This is my first answer here. :D"
Temp = Left(Temp, InStr(Temp, " ")-1)
In which case, Temp will be "Hello_World!"
Use of Split function
An elegant approach is to use the first token of the Split function:
Code Example extracting from cell A1 to A2
Option Explicit
Sub KeepCity()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("MySheet") ' << Change to your sheet name
ws.[A2] = Split(ws.[A1], " ")(0)
End Sub
Alternative Syntax
Instead of cell abbreviations [A2] or [A1] you can also use:
ws.Range("A2").Value = Split(ws.Range("A1").Value, " ")(0)
Note
The resulting split 1-dimensional array is zero based, so you get the first part (or token) of the original string via index (0).
If you are looking for a second part, I recommend adding an additional delimiter value (" ") to the original string, e.g. s: MsgBox split(s & " "," ")(1). By that way you avoid error number 9 "Subscript out of range", if there is no delimiter in between at all, thus no second part at all.