I am trying to combine functions RIGHT and Application.UserName.
The user name is Smith, John. I want to have only name - John.
I tried to do it like below, but it doesn't work.
Sub test ()
Dim x as string
x = Right(Application.Username, Len(Application.UserName) - Application.UserName.Find(" ",Application.UserName, 1))
End Sub
Could you please advise?
Thanks,
There is no Find method of Application.UserName.
Use Split:
Dim splitName
splitName = Split(Application.UserName)
Debug.Print splitName(Ubound(splitName)) '<~ returns John
Related
What I Have:
A column I have this data 24/25,25/26, up to 100/101 and in B column I have this data 24,25,26, up to 101. Using Left function I'm checking Left(24/25,2) with column B.... when coming to 100/101 it's giving me return as 10.
What I want:
Left(100/101,2) Then its have return 100 how do I do that I know if we give 3 then it will return but when we give three then 24/ also come. Give me any Suggestion.
You can use the Split Function:
Dim str As String
str = "24/25,25/26"
Dim arr As Variant
arr = Split(str, "/")
The result then is
'arr(0) is "24"
'arr(1) is "25,25"
'arr(2) is "26"
Sheet formula version:
You want to Find the position of "/" in the cell, then remove 1. That will give the number of characters before the "/" that you are after for Left. So, if data was in A2, you would put the following in B2:
=IFERROR(LEFT(A2,FIND("/",A2)-1),"")
A VBA version (User defined function) might look like:
As you mention using Instr.
Option Explicit
Public Sub TEST()
Debug.Print GetLeft([A2].Text)
End Sub
Public Function GetLeft(ByVal aString As String) As Variant
If InStr(aString, "/") > 0 Then
GetLeft = Left$(aString, InStr(aString, "/") - 1)
Else
GetLeft = CVErr(xlErrNA)
End If
End Function
Use Like.
If Range("A4").Value2 & "/" Like "*" & range("B4").Value2 & "/*" Then
I added a "/" at the right of each side, to avoid 10 matching 100 or 101.
I have a variable which contains five words.
Eg:- My name is Albert Einstein.
If the first word is "My" then it should be bold, else the word has to be deleted.
Hope the below code logic could help you to solve the problem:
Sub test1()
Debug.Print boldAndDeletion("Your name is Albert Einstein.")
End Sub
Function boldAndDeletion(inputString As String) As String
Dim splitStr1 As Variant
splitStr1 = Split(inputString, " ")
If splitStr1(0) = "My" Then
boldAndDeletion = "<b>My</b>" & Mid(inputString, 3)
Else
boldAndDeletion = Empty
End If
End Function
This is a sample data contained in one cell:
2014/08/19 12:59 John Doe
add sample#hotmail.com
I need to extract the name in the text. I know that it is always placed after the datetime stamp.
My idea is to find the position of ":" and add 4 thus getting the position of the first letter of the first name:
colonLoc = InStr(sampleData, ":")
firstLetterLoc = colonLoc + 4
How can I get the first and last name after that?
Here is a one liner to achieve what you want.
debug.print Mid(Split(Split(Range("A1").Value, Chr(10))(0), ":")(1), 3)
EDIT:
Actually you don't need VBA for this. You can use Excel formulas as well
=MID(A1,FIND(":",A1)+3,FIND(CHAR(10),A1)-(FIND(":",A1)+3))
This works even for names with spaces:
Function ExtractName(str As String) As String
Dim i As Long
Dim splitStr() As String
Dim nameParts() As String
splitStr = Split(str, " ")
ReDim nameParts(LBound(splitStr) To UBound(splitStr) - 4)
For i = LBound(nameParts) To UBound(nameParts)
nameParts(i) = splitStr(i + 2)
Next i
ExtractName = Join(nameParts, " ")
End Function
What this effectively does is remove four substrings: the date, the time, the add bit, and the e-mail address. Everything else in the middle is assumed to be part of the name.
Example usage:
Debug.Print ExtractName("2014/08/19 12:59 John Doe add sample#hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 Johan Sebastian Bach add sample#hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 Fuh Wei Guo Tang add sample#hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 Jens von dem Hagen add sample#hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 José Manuel de Santiago Itthuralde add sample#hotmail.com")
EDIT Now you say your input string is split over two lines... This works for me with the input you specify:
Function ExtractName(str As String) As String
Dim i As Long
Dim splitStr() As String
Dim nameParts() As String
splitStr = Split(Split(str, vbLf)(0), " ")
ReDim nameParts(LBound(splitStr) To UBound(splitStr) - 2)
For i = LBound(nameParts) To UBound(nameParts)
nameParts(i) = splitStr(i + 2)
Next i
ExtractName = Join(nameParts, " ")
End Function
It's like this:
Option Explicit
Public Sub Playground()
Const SampleData As String = "2014/08/19 12:59 John Doe add sample#hotmail.com"
Dim Parts() As String
Dim FirstName As String
Dim LastName As String
Parts = Split(SampleData)
FirstName = Parts(2)
LastName = Parts(3)
Debug.Print FirstName
Debug.Print LastName
End Sub
For more complicated cases (e.g. spaces in names) you might have to tweak it a little bit.
This will give you firstname (assuming there is only 1), lastname (all other names) and email in a variant array
Option Explicit
Public Function Name(source As String) As Variant
Dim breakup As Variant
breakup = Split(source, ":")
breakup = Split(Mid(breakup(1), 4), " ")
Dim i As Integer
Dim FirstName As String
FirstName = breakup(0)
Dim LastName As String
For i = 1 To UBound(breakup) - 2
LastName = LastName & " " & breakup(i)
Next
LastName = Mid(LastName, 2)
Dim Email As String
Email = breakup(UBound(breakup))
Name = Array(FirstName, LastName, Email)
End Function
sampleData = "2014/08/19 12:59 John Doe add sample#hotmail.com"
New_String = Split(sampleData)
sName = New_String(2) & " " & New_String(3)
Debug.Print sName
Easy as that :)
I am trying to obtain the string to the right of the delimiter "|" and anything to the left can be ignored.
I tried the following, but it gives me the values to the left
Dim s As String = "John Smith | 09F2"
Console.WriteLine(s.Substring(0, s.IndexOf("|")))
Console.WriteLine(s.Split(CChar("|"))(0))
Console.ReadKey()
Result is: John Smith. I want the 09F2 value.
I also tried the following lines of code:
Dim strEmployeeInfo As String = cmbSelectEmployee.Text
Dim employeeID = Microsoft.VisualBasic.Right(strEmployeeInfo, strEmployeeInfo.IndexOf("|"))
But the result of that is Smith | 09F2. Again I only need the 09F2
When you split on the | character, the resulting array will have "John Smith " in the first position, and " 09F2" in the second position. So you need to access the second position. Since it's 0-based pass in 1 to access the second position. Next, trim the result to get rid of any additional spaces:
Dim s As String = "John Smith | 09F2"
Dim split As String() = s.Split("|"c)
Dim result As String = split(1).Trim()
Try this:
Console.WriteLine(s.Substring(s.IndexOf("|") + 1));
Suppose you have more than one "|" in your string and you want the last part after the last "|", you can use this and also it works for the above example:
Dim SArray() As String = s.Split("|"c);
//if necessary, check the length of SArray() for correctness
Console.WriteLine(SArray(SArray.Length - 1));
I'd go with this:
Dim s As String = "John Smith | 09F2"
Console.WriteLine(s.Split("|").Last().Trim())
Hi guys i am hoping that you guys can point me in the right direction. I am trying to come up with a macro that will sort words in ascending order for each paragraph. To make it clear i am giving an example below:
The quick brown fox jumps over the lazy dog. <---- given
brown dog fox jumps lazy over quick the the <---- the output
The output should show below the para/s right at the end of the docu already sorted. Any help or advice as to how i can go about it using Ranges, pls let me know. thanks!
I believe this code might point you in the right direction.
Notice you'll need to add somewhere the sorted array (as of now, it's only sorting the array values).
I used the sorting function available HERE.
Hope it helps!
Option Explicit
Option Compare Text
Sub orderParagraph()
Dim oParagraph As Word.Paragraph
Dim vParagraphText As Variant
For Each oParagraph In ActiveDocument.Paragraphs
If Len(Trim(oParagraph.Range.Text)) > 0 Then
vParagraphText = oParagraph.Range.Text
vParagraphText = Split(vParagraphText, " ")
SortArray vParagraphText
End If
Next oParagraph
End Sub
Private Function SortArray(ByRef TheArray As Variant)
Dim x As Integer
Dim bSorted As Boolean
Dim sTempText As String
bSorted = False
Do While Not bSorted
bSorted = True
For x = 0 To UBound(TheArray) - 1
If TheArray(x) > TheArray(x + 1) Then
sTempText = TheArray(x + 1)
TheArray(x + 1) = TheArray(x)
TheArray(x) = sTempText
bSorted = False
End If
Next x
Loop
End Function