All Xpath values are entered in column E of Excel.
Find the Xpath in column E and enter the value entered in column D into Chrome.
How can I make this code into a loop.
load_wb = load_workbook(filename, data_only=True)
load_ws = load_wb['apple']
elem=driver.find_element_by_xpath(load_ws['E2'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D2'].value)
elem=driver.find_element_by_xpath(load_ws['E3'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D3'].value)
elem=driver.find_element_by_xpath(load_ws['E4'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D4'].value)
elem=driver.find_element_by_xpath(load_ws['E5'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D5'].value)
elem=driver.find_element_by_xpath(load_ws['E6'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D6'].value)
elem=driver.find_element_by_xpath(load_ws['E7'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D7'].value)
elem=driver.find_element_by_xpath(load_ws['E8'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D8'].value)`
In case there are 7 rows in the Excel table your code can be simply rewritten as following:
load_wb = load_workbook(filename, data_only=True)
load_ws = load_wb['apple']
for i in range(2,9):
elem=driver.find_element_by_xpath(load_ws['E' + str(i)].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws['D'+ str(i)].value)
In case the amount of rows is different or can be changing you will have to change the range used in the for loop accordingly.
createa for loop using range , and then call i using format :
load_wb = load_workbook(filename, data_only=True)
load_ws = load_wb['apple']
for i in range(2,9):
elem=driver.find_element_by_xpath(load_ws[f'E{i}'].value)
elem.send_keys(Keys.CONTROL+'a',Keys.DELETE)
elem.send_keys(load_ws[f'D{i}'].value)
Related
I have an excel sheet table with the following data:
In VBA how to search and match for both values in columns A and B and return row value in column C.
Example:
I need to search for the exact match of c+c1 and have as result yy
Many thanks for the help
Use If statements when you loop through the rows like so:
If ws.cells(i,1).Value = c And ws.cells(i,2).value = c1 Then
result = yy
End If
Hope it helps!
U can create a 4th column, this column will be your key column to use in VBA. In this column you will concatenate the A and B values, after that we create a code that search the concat and return the 4th cell on the right.
.
Sub Example()
Dim keyRange As Range
Set keyRange = Planilha1.Range("A2:A8")
Dim SearchValue1, SearchValue2 As String
SearchValue1 = "a"
SearchValue2 = "a2"
Dim lin As Integer
lin = Application.WorksheetFunction.Match(SearchValue1 & SearchValue2, keyRange, 0)
Dim answer As String
answer = Planilha1.Range("A2:D8").Cells(lin, 4)
Debug.Print answer
End Sub
I am looking for a VBA code which will help me in calculating the no of characters in the range of cells.
If i use the excel option of LEN, it does gives me the no of characters in the particular cell.
Do we have any code which will give me the no of characters in a range of cells.
For ex; A1 = "Night" , B1 = "Day" C1 = "Noon"
The result should be 12 ( 5 + 3 + 4 )
Can any one help me with this
You can create a custom UDF, that using a loop will calculate the number of characters in a range, something like the code below:
Function SumLeninRange(Rng As Range) As Long
Dim C As Range
For Each C In Rng ' loop through all cells in Range
SumLeninRange = SumLeninRange + Len(C.Value2)
Next C
End Function
And use the Test Sub code below:
Sub Test()
MsgBox "Num of Characters in Range is : " & SumLeninRange(Range("A1:C1"))
End Sub
You do not need VBA to do this, just array formula
=SUM(LEN(A1:C1))
type it and then press Ctrl-Shift-Enter
How do i write the following code as a loop. I want to copy values from a table in sheet 4 in a row from range (b:17:L17"). is there a more efficient way to do it with loops ?
ActiveSheet.Range("B17").Value = Sheets(4).Range("G8")
ActiveSheet.Range("C17").Value = Sheets(4).Range("G9")
ActiveSheet.Range("D17").Value = Sheets(4).Range("G10")
ActiveSheet.Range("E17").Value = Sheets(4).Range("G11")
ActiveSheet.Range("F17").Value = Sheets(4).Range("G12")
ActiveSheet.Range("G17").Value = Sheets(4).Range("G13")
ActiveSheet.Range("H17").Value = Sheets(4).Range("G14")
ActiveSheet.Range("I17").Value = Sheets(4).Range("G15")
ActiveSheet.Range("J17").Value = Sheets(4).Range("G16")
ActiveSheet.Range("K17").Value = Sheets(4).Range("G17")
ActiveSheet.Range("L17").Value = Sheets(4).Range("G18")
Yes, there is:
ActiveSheet.Range("B17:L17").Value = Application.Transpose(Sheets(4).Range("G8:G18").Value)
You can, using something like this (VB.Net, but may copy easily to VBA):
Dim cell as Integer, c as Integer
cell = 8
For c = 66 To 76
ActiveSheet.Range(Chr(c) & "17").Value = Sheets(4).Range("G" & cell)
cell = cell + 1
Next
The Chr() function gets the character associated with the character code (66-76), and then this value is concatenated with the string "17" to form a complete cell name ("B17", "C17", ...)
I am also incrementing the cell number for G at the same time.
Use this if you really want to use a loop - but there could be better ways, like the answer given by #A.S.H
Solution explanation:
Establish your rules! What is changing in the range for active sheet? The column is going to grow as the for/to cycle does! So, we should sum that to it. What is the another thing that is going to increment? The Range in the other side of the '=' so, by setting an algorithm, we may say that the row is const in the Activesheet range and the column is the on variable on the other side.
Solution:
Sub Test()
Const TotalInteractions As Long = 11
Dim CounterInteractions As Long
For CounterInteractions = 1 To TotalInteractions
'where 1 is column A so when it starts the cycle would be B,C and so on
'where 7 is the row to start so when it begins it would became 8,9 and so on for column G
ActiveSheet.Cells(17, 1 + CounterInteractions).Value = Sheets(4).Cells(7 + CounterInteractions, 7)
Next CounterInteractions
End Sub
This is probably your most efficient solution in a with statement:
Sub LoopExample()
Sheets("Sheet4").Range("G8:G18").Copy
Sheets("Sheet2").Range("B17").PasteSpecial xlPasteValues, Transpose:=True
End Sub
What I'm trying to do is take the values from 4 textboxes and put them into 4 different sheets, but they need to be Integers so the Average and Sum functions work. But if the value is Null to clear the textbox so that value isn't calculated into the Monthly Sums or Averages. I keep getting a Type Mismatch Error with the Else clause, and when I mouse over it, it shows me frmDataEntry.tbBloodDraws = "". I thought that the else clause would just take the value and change it to an Int as long as it wasn't empty or Null? What am I missing?
Private Sub btnOK_Click()
' Get currently selected Cell of the Data Entry Sheet
Dim DataEntryCell As String
DataEntryCell = ActiveCell.Address
'Copy values from the dialog box into the correct sheets
Worksheets("Blood Draws").Activate
Range(DataEntryCell).Select
If (IsNull(frmDataEntry.tbBloodDraws)) Then
ActiveCell.Value = ActiveCell.Clear
Else
ActiveCell.Value = CInt(frmDataEntry.tbBloodDraws)
End If
End Sub
Use ClearContents to remove only the content of the cell and then check with IsNumeric if the string can be interpreted as number.
If tbBloodDraws.Text = "" Then
ActiveCell.ClearContents
ElseIf IsNumeric(tbBloodDraws.Text) Then
ActiveCell.value = CInt(tbBloodDraws.Text)
End If
I'm trying to pull all hyperlinks in an excel spreadsheet into a new worksheet. I want column A to show the text from the hyperlink, and column B to show the hyperlink address.
I've written the code below, and all of column B works fine, however the values in column A are not all coming over, and they don't match the hyperlink addresses in column B. What am I doing wrong?
Thanks in advance
Sub extract_links()
Dim hyp As Hyperlink
Dim ReadCols As Long
Dim ReadWriteRow As Long
ReadWriteRow = 1
ReadCols = 6
ActiveWorkbook.Sheets(2).Range("a:b").Clear
For c = 1 To ReadCols
For Each hyp In ActiveWorkbook.Sheets(1).Columns(c).Hyperlinks
ActiveWorkbook.Sheets(2).Range("a" & ReadWriteRow).Value = ActiveWorkbook.Sheets(1).Cells(ReadWriteRow, c).Value
ActiveWorkbook.Sheets(2).Range("b" & ReadWriteRow).Value = hyp.Address
ReadWriteRow = ReadWriteRow + 1
Next
Next c
End Sub
This time you need to change this:
ActiveWorkbook.Sheets(2).Range("a" & ReadWriteRow).Value = ActiveWorkbook.Sheets(1).Cells(ReadWriteRow, c).Value
into this:
ActiveWorkbook.Sheets(2).Range("a" & ReadWriteRow).Value = hyp.Range.Value