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
Related
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:
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 6 years ago.
Improve this question
I do know the formula but not sure how to translate into VBA.
Need a VBA script to do the following.
If cell D contains Debit, then value in F multiply -1
If cell D contains Credit, then value in F multiply 1
Loop until last row.
Use this macro:
Sub subMultiply()
For Each cel In Range("D1:D" & Range("D1").End(xlDown).Row)
If cel.Value = "Debit" Then
cel.Offset(0, 2).Value = Val(cel.Offset(0, 2)) * (-1)
ElseIf cel.Value = "Credit" Then
cel.Offset(0, 2).Value = Val(cel.Offset(0, 2)) * 1
End If
Next
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 7 years ago.
Improve this question
New to vba. Just starting to learn. I want to pull some specific data from a website.Code I am trying to modify is from Ron Retrieving specific data from website through excel.
Now this code work on a single url. I have urls in Column A of excel sheet and I want to macro to go one by one to all urls and paste results in Column B C D respectively.
Tried as best as my limited knowledge.
Regards
The main sub will get the ratings and the number of reviews for each URL in column A and will place them in Column B and C. I hope this help you a little.
Sub main()
Dim l As Long
l = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To l
test Range("A" & i)
Next
End Sub
Sub test(URL As Range)
my_url = URL.Value
Set html_doc = CreateObject("htmlfile")
Set xml_obj = CreateObject("MSXML2.XMLHTTP")
xml_obj.Open "GET", my_url, False
xml_obj.send
html_doc.body.innerhtml = xml_obj.responseText
Set xml_obj = Nothing
Set Results = html_doc.body.getElementsByTagName("i")
For Each itm In Results
If InStr(1, itm.outerhtml, "star-img", vbTextCompare) > 0 Then
numb_stars = itm.getAttribute("title")
Exit For
Else
End If
Next
Set Results = html_doc.body.getElementsByTagName("span")
For Each itm In Results
If InStr(1, itm.outerhtml, "reviewCount", vbTextCompare) > 0 Then
numb_rev = itm.innertext
Exit For
Else
End If
Next
URL.Offset(0, 1) = numb_stars
URL.Offset(0, 2) = numb_rev
End Sub
Preview of my output:
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
Could anyone give me advice on how to look up a row in a spreadsheet which matches two criteria, then change the data in specific Cells.
The data will be like this..
Reference Version date1 date2 date3
ABC1 1 11/12/2013
ABC1 2 31/12/2013
ABC2 1 12/12/2013
ABC3 1 12/12/2013
ABC1 3 01/01/2014
In VBA I wish to be able to find the row that matches the reference and the version number, then datestamp column 4 of that 1 unique row.
Any help would be really appreciated.
Many Thanks
Solving for a position for two criteria can be solved by this array formula (in this sample looking up ABC3 and 1 in A2:B6
This formula can be used in VBA to provide the position for the timestamp:
VBA Equivalent
Sub OneWay()
Dim lngRow As Long
lngRow = Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")
Cells(lngRow + 1, 4) = Now()
End Sub
or just:
Sub OneWay2()
lngRow = Cells(Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")+ 1, 4) = Now()
End Sub
Try this code:
Sub test()
Dim refToFind As String
Dim versToFind As String
refToFind = "ABC1"
versToFind = "1"
'address of first reference
Set Rng = Sheet1.Range("B2")
lastrow = Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row - Rng.Row
For i = 0 To lastrow
If CStr(Rng.Offset(i, 0).Value) = refToFind _
And CStr(Rng.Offset(i, 1).Value) = versToFind Then
Rng.Offset(i, 4) = Now
End If
Next i
End Sub