How Might I Extract Specific Characters From A String Using VBA [closed] - excel

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,

Related

Subtracting a comma separated string from another one in Excel [closed]

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 2 years ago.
Improve this question
How can I subtract a comma seperated string from another one in Excel?
If A1: 1,2,3,4,5,6,7,8,9
And A2: 2,6,9
My desired result should be in cell A3, after subtraction (A1-A2): 1,3,4,5,7,8
It was very easy when A2=2,3,4 (serially) via SUBSTITUTE function, however I can't find a solution to the above.
You have both tagged formula and VBA. So let me give you two options too:
1) Formula
=TEXTJOIN(",",1,FILTERXML("<t><s>"&SUBSTITUTE(A1&","&A2,",","</s><s>")&"</s></t>","//s[not(following::*=. or preceding::*=.)]"))
Note1: This is an array formula and needs to be confirmed through CtrlShiftEnter
Note2: This requires access to the TEXTJOIN function, available in office 365 and Excel 2019.
Note3: More usefull FILTERXML "tricks" can be found here
2) UDF
I'd recommend using Split and Join functions to do this:
Function TxtFilter(val1 As String, val2 As String, sep As String) As String
Dim arr1 As Variant, arr2 As Variant
Dim x As Long
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
arr1 = Split(val1, sep)
arr2 = Split(val2, sep)
'Iterate first array to load dictionary
For x = LBound(arr1) To UBound(arr1)
dict(arr1(x)) = 1
Next x
'Iterate second array to remove from dictionary
For x = LBound(arr2) To UBound(arr2)
If dict.Exists(arr2(x)) Then dict.Remove arr2(x)
Next x
'Join remainder back together
TxtFilter = Join(dict.keys, sep)
End Function
Call in any cell through =TxtFilter(A1,A2,",")

Excel Display Zeros in Number Without Error [closed]

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

Extract normal font to new cell [closed]

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:

How to replace first character in string [closed]

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

creating a macro to separate data [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
HI I am new to using excel and only have a basic knowledge in designing macros. I want to be able to design a macro that can separate different invoice details depending on the company unique id into a separate sheet. only problem there is two or three rows that need to be moved together. How would I go about doing this?
For example:
Here is a sample picture of the data. what i want to do is copy the H and N in rows 1 and 2 deepening on the value in row D
Assuming your testing for something like 'is value > 25'
Sub Macro1()
Dim dat As Variant
Dim rng As Range
Dim i As Long
Dim cntr As Integer
cntr = 1
Set rng = [A1:A5]
dat = rng ' dat is now array (1 to 5, 1 to 1)
For i = LBound(dat, 1) To UBound(dat, 1)
If rng(i, 1).Offset(0, 3).Value > 25 Then
Sheets("Sheet2").Range("A" & cntr).Value = Range("A" & i).Value
cntr = cntr + 1
End If
Next
End Sub

Resources