Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a string in VBA - Hello how are you.
I want to replace - with something else. How can I do that? I am beginning in VBA. Need help.
The easiest way would be to use the replace function.
For example:
Dim yourString As String
Dim newString As String
yourString = "- Hello how are you"
newString = Replace(yourString, "-", "something else")
MsgBox newString 'returns "something else Hello how are you"
If it's always the first character and that character is different for each string you can do something like this:
Dim yourString, subString, replacementString, newString As String
yourString = "- Hello how are you"
subString = Right(yourString, Len(yourString) - 1)
replacementString = "something else"
newString = replacementString + subString
MsgBox newString 'returns "something else Hello how are you"
If you are replacing with a single character than using Mid$ on the LHS is much quicker than appending to a RIGHT$ manipulation:
(posting this as not enough detail evident in your question - may be useful for others)
Dim strNew As String
strNew = "- Hello how are you"
Mid$(strNew, 1, 1) = "x"
Debug.Print strNew
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Question: Consider the following string, s, which is equal to:
\\xxxxxx.com\xxx\xxx\xxx\xxx\xxx\xxx\xxx\04. xxx\02. xxx\B. xxxx\yyy
What code might be used in order for PlaceHolderIndex to have the value "04.02.B" ?
Below is a small VBA function that accepts a string, splits it using "\" as a separator, ignores the first part ("\xxxxxxx.com\"), then looks for "." in each of the sections, and concatenates the data before the ".".
Function fGetData(strInput As String) As String
Dim aData() As String
Dim lngLoop1 As Long
aData = Split(strInput, "\")
For lngLoop1 = 3 To UBound(aData) ' ignore the first 3 elements as they are "\\xxxxx.com\"
If InStr(aData(lngLoop1), ".") > 0 Then
fGetData = fGetData & Left(aData(lngLoop1), InStr(aData(lngLoop1), "."))
End If
Next lngLoop1
If Right(fGetData, 1) = "." Then fGetData = Left(fGetData, Len(fGetData) - 1)
End Function
Regards,
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In excel VBA how do I display the following numbers as they appear without generating the 'Number stored as text' error in the cell?
1.0
1.1
1.2
.
.
1.99
1.100
1.101
1.102
.
.
1.20
1.21
1.22
.
.
etc...
Just keep track of the number of decimal places that need to be displayed. For example:
Sub marine()
Dim s As String, i As Long, a
s = "1.0,1.1,1.2,1.99,1.100,1.101,1.102,1.20,1.21,1.22"
arr = Split(s, ",")
i = 1
For Each a In arr
With Cells(i, 1)
.Value = a
brr = Split(a, ".")
.NumberFormat = "0." & Application.Rept("0", Len(brr(1)))
End With
i = i + 1
Next a
End Sub
This is based on the curious fact that if VBA puts a number-like string into a cell, Excel will convert it into a number:
Sub demo()
Dim s As String
s = "1.230"
Range("A1").Value = s
End Sub
EDIT#1:
On the other hand, if you want to enter text that look like numbers, but avoid raising the error flag, then:
Sub Macro1()
Application.ErrorCheckingOptions.NumberAsText = False
End Sub
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have multiple cells with bold font and normal font, I am looking to only extract the font that isn't bold to a new cell. Does anyone know how I can do this?
Thanks
You can just do the reverse of what Siddarth was doing in the post I linked - for example:
Using this:
Sub Test()
Dim mystring As String
For i = 1 To Len(Range("A1").Value)
If Range("A1").Characters(i, 1).Font.FontStyle = "Regular" Then
mystring = mystring & Mid(Range("A1").Value, i, 1)
ElseIf Len(mystring) > 0 Then
Debug.Print Trim(mystring)
mystring = ""
End If
If i = Len(Range("A1").Value) Then
Debug.Print Trim(mystring)
End If
Next i
End Sub
Returns:
This question already has answers here:
How to extract file name from path?
(16 answers)
Closed 7 years ago.
how can we extract a substring from a string if we give the positions of first and last letter in string?
Dim my_string As String
Dim my_substring As String
my_string="Excel/Sheet1.xls"
instrrev(my_string,"/") gives 6 (start position)
len(my_string) gives 16 (end position)
So to get "Sheet1.xls" as my substring what should we do?
Or we can get it selecting characters till "/" is met first/
How about this one?
my_substring = Split("Excel/Sheet1.xls","/")(1)
Mid(my_string,instrrev(my_string,"/")+1, len(my_string))
would give you Sheet1.xls.
How about this solution:
Dim strFileName As String
strFileName = ActiveWorkbook.Path & "/" & ActiveWorkbook.Name
Debug.Print "Start Position: " & Len(strFileName) - InStr(1, StrReverse(strFileName), "/")
Debug.Print "Extacted file name: " & Mid(strFileName, Len(strFileName) - InStr(1, StrReverse(strFileName), "/") + 2)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i am beginner in excel vba i have huge list where the first name and last name in one column without any space or symbol and some name are in capital alphabet and some have no capital alphabet and they are the same column what would be the popossible macro to make space batween them...or any possible way....
thanks in advance....
Not reliable but will get you there with simple names. Select the range of names and run the macro.
Sub InsertSpacesInNames()
Dim pos As Long: pos = 0
Dim uc As Long: uc = 0
For Each cell In Selection
For i = 1 To Len(cell.Value)
If Asc((Mid(cell.Value, i, 1))) >= 65 And Asc((Mid(cell.Value, i, 1))) <= 90 Then
uc = uc + 1
pos = i
End If
Next i
If uc = 2 Then
cell.Value = Mid(cell.Value, 1, pos - 1) & " " & Mid(cell.Value, pos, Len(cell.Value))
End If
uc = 0
pos = 0
Next
End Sub
My output:
This macro will work fine if and only if:
you use it with simple names (no compound names like Jean-PierreLeCosteau which's desired output is Jean-Pierre LeCosteau)
the full name has two capital letters only.
Assuming you know the first name then simple example using formulas:
A B
1 AmandaWinslet = "Amanda" & " " & RIGHT(A1, LEN(A1) - LEN("Amanda"))) // result is Amanda Winslet
The VBA way of doing this would be:
Sub AddSpace()
Range("A1") = "Amanda" & " " & VBA.Right$(Range("A1"), Len(Range("A1")) - Len("Amanda"))
End Sub
You will need to loop over the list in VBA and make FirstName a variable. Your post assumes you have the first name from somewhere