how to extract data using Xpath in Vba - vba

i want to extract table data from each rows, but it doesn't work.
it work when i change tr to tr[2], else extract only blank value
Dim driver As New WebDriver
Dim Url
Dim Rows As WebElements
Dim Value as Webelement
Set Rows = driver.FindElementsByXpath("/html/body/div[3]/div[2]/div/form/div[3]/div/div[3]/div[3]/div/table/tbody/tr")
cnt = 1
For Each Value In Rows
Range("a" & cnt) = Value.FindElementByXPath("td[8]").Text
Range("b" & cnt) = Value.FindElementByXPath("td[7]").Text
Range("c" & cnt) = Value.FindElementByXPath("td[10]").Text
Range("d" & cnt) = Value.FindElementByXPath("td[11]").Text
Range("e" & cnt) = Value.FindElementByXPath("td[18]").Text
Range("f" & cnt) = Value.FindElementByXPath("td[19]").Text
Range("g" & cnt) = Value.FindElementByXPath("td[20]").Text
cnt = cnt + 1
Next

Dim col_n as long
for i = 1 to NumCols
if Cells(1, i).Value = "column header you are looking for" Then col_n = i
next
for i = 1 to NumRows
Cells(i, col_n).Value = "PHEV"
next i

Related

VBA Code for Multiple if conditions

I need to categorize my data into different buckets. my worksheet has column V & Column Y (actually a name match & address match respectively) has values that are either "ok" or "check". Column O has IDs, of which some are only numeric and some are alpha numeric.i need to fill my column A based on these 3 columns.
category 1 - Column A to be filled with "Verify name & Address" - logic for this is - If Column A is blank, Column V value = "check", Column Y value = "check" and column O = all alphanumeric IDs (except that starts with CWT) and numeric IDs = 2 & 9612
Category 2 - Column A to be filled with "Verify Address" - logic for this is - If Column A is blank, Column V value = "ok", Column Y value = "check" and column O = all alphanumeric IDs (except that starts with CWT) and numeric IDs = 2 & 9612.
Sub Rules()
'
'Autofill based on Rules
Worksheets("ORD_CS").Activate
Dim sht As Worksheet
Dim LR As Long
Dim i As Long
Set sht = ActiveWorkbook.Worksheets("ORD_CS")
LR = sht.UsedRange.Rows.Count
With sht
For i = 8 To LR
If .Range("A" & i).Value = "" And Range("V" & i).Value = "check" And Range("Y" & i).Value = "check" And Range("O" & i).Value = "2" And Range("O" & i).Value = "9612" Then
.Range("D" & i).Value = "Verify Name & Address"
End If
Next i
End With
End Sub
I have not completed my code. Can someone help? Thanks
The below should work, I changed your O column to be an OR
Edit: for function
Public Function IsAlpha(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 65 To 90, 97 To 122
IsAlpha = True
Case Else
IsAlpha = False
Exit For
End Select
Next
End Function
With sht
For i = 8 To LR
If .Range("A" & i).Value = "" And Range("V" & i).Value = "check" And Range("Y" & i).Value = "check" And Range("O" & i).Value = "2" Or Range("O" & i).Value = "9612" Or IsAlpha(Range("O" & i).Value) = True Then
.Range("D" & i).Value = "Verify Name & Address"
Else
If .Range("A" & i).Value = "" AND .Range("V" & i).Value = "ok" AND .Range("O" & i).Value = "2" Or .Range("O" & i).Value = "9612" Then
Do Stuff
End If
End If
Next i
End With

VBA, concact 2 values with the same variable in cell

I'm trying to write a VBA script. This script would read 1 column and write the result in another column.
If the values are in bold or if is not blank, I would like to write the data in the column b1.
But if the values are not in bold, I would like to write the data in c1, and concatenate if I have 2 or more non-bold data in the same cell.
My code :
Sub Phone()
Dim valueLogon As String
Dim ValueDevice As String
Dim compteur As Integer
compteur = 1
For i = 1 To 2101
valueLogon = Range("A" & i)
If Range("A" & i).Font.bold = True And IsEmpty(valueLogon) = False Then
compteur = compteur + 1
Range("C" & i) = valueLogon
Else
Range("D" & compteur) = valueLogon & "," &
End If
Next i
End Sub
now, my result is like to the picture, but I would like concactenate the non-bold result in the same cell
change
Range("D" & compteur) = valueLogon & "," &
to
Range("D" & compteur).Value = valueLogon & "," & Range("D" & comptuer).Value

find month names withing for loop

It returns only saturday, for the dates it was simple because I only needed to add a plus 1 but how to i get it to loop from 1 to 7? Also how about for the months names as well
Sub netprofit()
Dim dates As Date
Dim count As Integer
Dim olweekdayname As String
olweekdayname = WeekdayName(Weekday(dates))
dates = DateSerial(2014, 9, 1)
For count = 2 To 366
Select Case Weekday(dates)
Case 1, 7
Range("B" & count) = 0
Range("C" & count) = 0
Range("D" & count) = 0
Range("E" & count) = olweekdayname
Case Else
Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
Range("D" & count) = Range("B" & count) - Range("C" & count)
Range("A" & count) = dates
Range("E" & count) = olweekdayname
End Select
dates = dates + 1
Next
End Sub
this line olweekdayname = WeekdayName(Weekday(dates)) must be inside of the loop, otherwise olweekdayname will not be changed, so your updated code is below, tested, works fine.
Sub netprofit()
Dim dates As Date
Dim count As Integer
dates = DateSerial(2014, 9, 1)
For count = 2 To 366
Select Case Weekday(dates)
Case 1, 7
Range("B" & count) = 0
Range("C" & count) = 0
Range("D" & count) = 0
Range("E" & count) = WeekdayName(Weekday(dates))
Range("F" & count) = MonthName(Month(dates))
Case Else
Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
Range("D" & count) = Range("B" & count) - Range("C" & count)
Range("A" & count) = dates
Range("E" & count) = WeekdayName(Weekday(dates))
Range("F" & count) = MonthName(Month(dates))
End Select
dates = dates + 1
Next
End Sub

Reset row increment before going to next worksheet

I have 2 workbooks. One wb acts as a database (divided into users), while the other is used to pull data from that wb based on a date. I looped the macro and at first it seems to pull data only from the first sheet. See NOTE below
Dim y As Integer
Dim r As Integer
Dim WS_Count As Integer
Dim I As Integer
r = CollateSh3.Range("D" & Rows.Count).End(xlUp).Row + 1
y = 3
WS_Count = wbLog.Worksheets.Count
For I = 1 To WS_Count
Do Until wbLog.Sheets(I).Range("A" & y) = ""
If wbLog.Sheets(I).Range("B" & y).Value = RequiredDate Then
CollateSh3.Range("A" & r).Value = wbLog.Sheets(I).Range("D" & y).Value
CollateSh3.Range("C" & r).Value = wbLog.Sheets(I).Range("F" & y).Value
CollateSh3.Range("D" & r).Value = wbLog.Sheets(I).Range("G" & y).Value
CollateSh3.Range("E" & r).Value = wbLog.Sheets(I).Range("H" & y).Value
CollateSh3.Range("F" & r).Value = wbLog.Sheets(I).Range("I" & y).Value
CollateSh3.Range("G" & r).Value = wbLog.Sheets(I).Range("J" & y).Value
CollateSh3.Range("H" & r).Value = wbLog.Sheets(I).Range("K" & y).Value
CollateSh3.Range("I" & r).Value = wbLog.Sheets(I).Range("L" & y).Value
CollateSh3.Range("K" & r).Value = wbLog.Sheets(I).Range("N" & y).Value
r = r + 1
y = y + 1
Else
y = y + 1
End If
Loop
'MsgBox wbLog.Sheets(I).Name
Next I
EDIT: I think I might have useful additional info. It seems that the macro doesn't skip. When I tried to add more entries to the database file, it pulls data, except that it starts at the row where it ended on the previous sheet. For e.g., if it ended on row 9 on sh1, the code starts at row 10 on sh2.
How do I reset the increment before going to the next sheet?
Here's the edited code. I opted to add it after the end of the loop, because that way I'd remember it for future reference. Thanks #PatricK!
Dim y As Integer
Dim r As Integer
Dim WS_Count As Integer
Dim I As Integer
r = CollateSh3.Range("D" & Rows.Count).End(xlUp).Row + 1
y = 3
WS_Count = wbLog.Worksheets.Count
For I = 1 To WS_Count
Do Until wbLog.Sheets(I).Range("A" & y) = ""
If wbLog.Sheets(I).Range("B" & y).Value = RequiredDate Then
CollateSh3.Range("A" & r).Value = wbLog.Sheets(I).Range("D" & y).Value
CollateSh3.Range("C" & r).Value = wbLog.Sheets(I).Range("F" & y).Value
CollateSh3.Range("D" & r).Value = wbLog.Sheets(I).Range("G" & y).Value
CollateSh3.Range("E" & r).Value = wbLog.Sheets(I).Range("H" & y).Value
CollateSh3.Range("F" & r).Value = wbLog.Sheets(I).Range("I" & y).Value
CollateSh3.Range("G" & r).Value = wbLog.Sheets(I).Range("J" & y).Value
CollateSh3.Range("H" & r).Value = wbLog.Sheets(I).Range("K" & y).Value
CollateSh3.Range("I" & r).Value = wbLog.Sheets(I).Range("L" & y).Value
CollateSh3.Range("K" & r).Value = wbLog.Sheets(I).Range("N" & y).Value
r = r + 1
y = y + 1
Else
y = y + 1
End If
Loop
y = 3 'to reset increment before moving on to next sheet
Next I

Application.Match Run Time Error 13 in VBA Macro

This code was working about a month ago with out any issues.
The run time error (#13) points to:
lastRow = Application.Match(campus, subject.Range("A1:A35000"), 1) + 2
I have run the macro a few times with it being partially successful some times. Other times it does not get any data at all.
If the macro is partially successful, what would cause the type mismatch if the data in the columns is the same through out. All the data exported from SQL Tools and then copied and pasted to the worksheets.
Dim lastRow As Long
Dim firstRow As Long
Dim count As Long
Dim campus As String
Dim gradeCount As Integer
Dim current As String
Dim previous As String
campus = schools.Range("A" & i).Value
With subject.Range("A:A")
Set Rng = .Find(what:=campus, _
After:=.Cells(.Cells.count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
gradeCount = 0
firstRow = 0
If Not Rng Is Nothing Then
firstRow = Application.Match(campus, subject.Range("A1:A35000"), 0) + 2
lastRow = Application.Match(campus, subject.Range("A1:A35000"), 1) + 2
gs.Range("A" & rowCount).Value = schools.Range("A" & i).Value
gs.Range("C" & rowCount).Value = schools.Range("C" & i).Value
currentCampus = gs.Range("A" & rowCount).Value
current = ""
previous = ""
For count = firstRow To lastRow
If campus = subject.Range("A" & count).Value Then
current = subject.Range("C" & count)
gs.Range("A" & rowCount).Value = schools.Range("A" & i).Value
If current <> previous Then
gs.Range("C" & rowCount).Value = schools.Range("C" & i).Value
gs.Range("D" & rowCount).Value = grade
gs.Range("E" & rowCount).Value = current
previous = current
gradeCount = gradeCount + 1
rowCount = rowCount + 1
End If
End If
Next count
If gradeCount > 1 Then
gs.Range("D" & rowCount).Value = grade
gs.Range("E" & rowCount).Value = "*"
gs.Range("C" & rowCount).Value = schools.Range("C" & i).Value
rowCount = rowCount + 1
End If
End If
End With
This is an indication that the Match failed to find a value.
If the same formula occupied a worksheet cell, you would see:
#N/A