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:
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 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 debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two Excels as an input. I am taking values from one excel and passing it as a parameter to other excel for applying filter. Once the filter is on, I am reading filtered values from a column and trying to match a condition as mentioned in the below code. And everything working fine up to this place. Once I get the specific match, I have to write it back to first Excel but I am getting runtime error while doing so.
strPath = "C:\Users\PSingh\Desktop\CodeInventory.xlsx"
strPath1 = "C:\Users\PSingh\Desktop\MyScripts\Processes.xlsx"
Dim rows
Set objExcel1 = CreateObject("Excel.Application")
objExcel1.Visible = True
Set objExcel2 = CreateObject("Excel.Application")
objExcel2.Visible = True
Set objDict = CreateObject("Scripting.Dictionary")
objDict.CompareMode = vbTextCompare
Const xlUp = -4162
Const xlCellTypeVisible = 12
Dim a(), val
objExcel1.Workbooks.Open(strPath1)
With objExcel1.Activeworkbook.Sheets("Sheet1")
rowcount = .Range("A" & .Rows.Count).End(xlUp).Row
MsgBox rowcount
End With
Redim Preserve a(rowcount)
For i=1 To rowcount
a(i) = objExcel1.Activeworkbook.Sheets("Sheet1").Cells(i,1).Value
objDict(a(i)) = a(i)
'msgbox a(i)
Next
n = objDict.Items
objExcel2.Workbooks.Open(strPath)
For i=0 To UBound(n)
With objExcel2.Activeworkbook.Sheets("All")
.Range("A1").AutoFilter 19, "="&n(i)
'rows=.usedrange.columns(1).specialcells(xlCellTypeVisible)
For Each cl In objExcel2.Activeworkbook.Sheets("All").UsedRange.Columns(12).SpecialCells(xlCellTypeVisible)
If (InStr(cl, "Seq") <> 0 Or InStr(cl,"seq")) <> 0 Then
objExcel1.Activeworkbook.Sheets("Sheet1").Cells(i,2) = "Data" 'Not Working
Exit For
End If
Next
End With
Next
Try it with,
objExcel1.Activeworkbook.Sheets("Sheet1").Cells(i + 1, 2) = "Data"
You loop with i begins at For i = 0 to .... There is no row zero in column B.
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
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
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
As per title, the codes below are located in my Private Sub Workbook_open(). Therefore every time I open my workbook it is slow. How do I get to optimize the code to run fastest? Any help is appreciated.
'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").Cells(3, 2).SpecialCells(xlLastCell).row
If Sheets("CustomerList").Cells(b, 2) <> "" Then
Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
Else
End If
Next
'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").Cells(2, 2).SpecialCells(xlLastCell).Column
If Sheets("RawMatList").Cells(2, a) <> "" Then
Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
End If
Next
'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").Cells(3, 2).SpecialCells(xlLastCell).row
If Sheets("WIPList").Cells(c, 2) <> "" Then
Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
End If
Next
For Each Worksheet In Worksheets
Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet
It looks like your loop is iterating through every row or every column on a worksheet. Instead of using the last row or last column try using the last used row or last used column. This way instead of moving through thousands of blank rows you only check rows containing data.
Try:
'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").UsedRange.Rows.Count
If Sheets("CustomerList").Cells(b, 2) <> "" Then
Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
Else
End If
Next
'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").UsedRange.Columns.Count
If Sheets("RawMatList").Cells(2, a) <> "" Then
Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
End If
Next
'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").UsedRange.Rows.Count
If Sheets("WIPList").Cells(c, 2) <> "" Then
Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
End If
Next
For Each Worksheet In Worksheets
Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet