Listbox not displaying proper vlookup result - excel

I am trying to execute this part of the code.
Do While ws2.Cells(i, "A").Value <> Empty
arr2(k, j) = ws2.Cells(i, "A").Value
arr2(k, j + 1) = Application.VLookup(ws2.Cells(i, "A").Value, ws.Range("B:K"), 2, False)
formula = ("VLOOKUP($A" & i & "&" & User.TextBox1.Value & ", CHOOSE({1,2}, 'DATA'!$B$4:$B$7669&'DATA'!$D$4:$D$7669,'DATA'!$E$4:$E$7669),2,0)")
q = CStr(Evaluate(formula))
arr2(k, j + 2) = q
k = k + 1
i = i + 1
Loop
I don't know why, but I get different results at times, without making any changes. Sometimes it shows error for the vlookup value but sometimes it shows correct number. What can I do to fix this problem?

You are not specifying on which sheet the evaluate should run.
So the $A" & i & .. is looking on the active sheet.
Change
q = CStr(Evaluate(formula))
to
q = CStr(ws2.Evaluate(formula))

Related

How to get my code to output a specific piece of text from an existing spreadsheet?

I am currently working on a combination restriction on my VBA code where individuals can not select certain options together. At the moment the code works, yet on the log page where I am displaying all errors I can not get the code to show the two combinations that are restricted (referring to the code '& rs.Cells(r, 4) &'). I really want the log to state the two modules that are restricted and I'm stuck/confused as to the text I need to enter to ensure this happens.
Any help would be greatly appreciated!
excluded = True
For r = 3 To rs.Cells(Rows.Count, 4).End(xlUp).Row
If WorksheetFunction.CountIf(rq.Rows(i), rs.Cells(r, 4).Text) > 0 And _
WorksheetFunction.CountIf(rq.Rows(i), rs.Cells(r, 5).Text) > 0 Then
excluded = False
End If
Next r
If excluded = False Then
logNextRow = logNextRow + 1
lg.Cells(logNextRow, 1) = studentID
lg.Cells(logNextRow, 2) = "Student selected restricted combination - " & rs.Cells(r, 4) & ""
isLog = True
'GoTo nxti
End If
Move the logging to inside the loop.
Dim s1 As String, s2 As String
For r = 3 To rs.Cells(Rows.Count, 4).End(xlUp).Row
s1 = rs.Cells(r, 4).Text
s2 = rs.Cells(r, 5).Text
If WorksheetFunction.CountIf(rq.Rows(i), s1) > 0 And _
WorksheetFunction.CountIf(rq.Rows(i), s2) > 0 Then
logNextRow = logNextRow + 1
lg.Cells(logNextRow, 1) = studentID
lg.Cells(logNextRow, 2) = "Student selected restricted combination - " & s1 & " " & s2
End If
Next r

Get XPATH for selenium VBA

I am struggling with a web page on a secure website. I will put a snapshot of what I am working on.
The XPATH that has the rows of a table (equals to 13 rows) is that
//div[#id='Section3']
But the data is not inside that XPATH but after it in 9 columns.
How can I refer to those children or ancestors ( I don't know the exact term)?
Here's the HTML for that page ( I couldn't include it in the question)
https://pastebin.com/hEq8K75C
Here's the snapshot (may clarify the issue well)
How to implement the variable j in such lines?
Dim x As Long, i As Long, j As Long
x = .FindElementsByXPath("//div[#id='Section3']")
For i = 1 To x
For j = 1 To 9
Cells(i, j).Value = .FindElementByXPath("//div[#id='Section3']/following-sibling::div[following-sibling::div[#id='Section3'][count(preceding-sibling::div[#id='Section3'])=" & i & " and count(following-sibling::div[#id='Section3'])=" & x - (i + 1) & "]][" & j & "]").Text
Next j
Next i
Try to use below code:
counter = len(driver.find_elements_by_id("Section3"))
xpath = "//div[#id='Section3']/following-sibling::div[count(preceding-sibling::div[#id='Section3'])={0} and count(following-sibling::div[#id='Section3'])={1}]"
for i in range(counter):
print('\nRow #{} \n'.format(i + 1))
_xpath = xpath.format(i + 1, counter - (i + 1))
cells = driver.find_elements_by_xpath(_xpath)
for cell in cells:
value = cell.find_element_by_xpath(".//td").text
print(value)

How to copy the values in a range from one sheet to and paste it another sheet on a predefied order using VBA?

I am trying to copy the values in a specific range from one sheet to another. While pasting the values to the new sheet, there should be a predefined order in which the values are pasted.
I have created the program, but there comes an error in definition of For loop. Please note that in this example Num_Tacksta = 2, m = 9 (These two are variables).
Sub New_Try(m)
Dim n, i, j, x, k, a, rowinres, Num_Tacksta, Num_TackMul As Variant
Num_Tacksta = Sheets("ALLO").Range("E4").Value
Num_TackMul = (Sheets("ALLO").Range("E4").Value) * 2
x = Sheets("ALLO").Range("E4").Value
For rowinres = 2 To Num_Tacksta Step 1
For i = 2 To Num_TackMul Step 2
Sheets("Final").Range("A" & rowinres &, ",M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & i)
rowinres = rowinres + 1
Next i
For j = 3 To Num_TackMul Step 2
Sheets("Dummy_Result").Range("A" & rowinres &, ",M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & j)
rowinres = rowinres + 1
Next j
For k = Num_TackMul + 1 To m Step 1
Sheets("Dummy_Result").Range("A" & rowinres &, ",M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & k)
rowinres = rowinres + 1
Next k
Next rowinres
End Sub
To my basic Knowledge in VBA, I think the problem is in this Sheets("Dummy_Result").Range("A:M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & j) line. I would like to also ask if there is any simple method available.
I am new to programming and also to Stack Overflow. Helping me in this would be really helpful!
I thank you guys in advance.
Try colon instead of comma in your copy range:
Sheets("Results").Range("A" & rowinres & ":M" & rowinres).Copy Destination:=Sheets("Final").Range("A" & i)
A comma is used in functions such as SUM() but a colon is used to specify a range.

How can i use For Loop correctly for multiple conditions

I have the following loop below:
With Sheets("Sheet Name")
For i = 2 To 26
.Cells(11, i).Formula = Application.WorksheetFunction.CountIfs(Sheets("Sheet1").Range("F8:F" & n), Sheets("Sheet2").Range(Chr(64 + i) & "8"), Sheets("Sheet1").Range("AC8:AC" & n), "S")
.Cells(12, i).Formula = Application.WorksheetFunction.CountIfs(Sheets("Sheet1").Range("F8:F" & n), Sheets("Sheet2").Range(Chr(64 + i) & "8"), Sheets("Sheet1").Range("AC8:AC" & n), "YS")
Next i
End With
The last condition at the end of each line changes by a string i.e. "S", "YS". I want to avoid having to use 1 line for each "S" and "YS" and so on i.e. there will be many lines for each criteria. I also then want to add another for loop that deals with the rows
codes = Array("S", "YS")
For i = 2 To 26
For j = 0 to UBound(codes)
Sheets("Sheet Name").Cells(11+j, i).Formula = Application.WorksheetFunction.CountIfs(Sheets("Sheet1").Range("F8:F" & n), Sheets("Sheet2").Range(Chr(64 + i) & "8"), Sheets("Sheet1").Range("AC8:AC" & n), codes(j))
Next j
Next i
The next step in my long list of issues and improvements was to simulate this but for a SUMIFS, as i was looking at cost data.
Please see below and i hope others can benefit from this and perhaps improve!
With Sheets("Operations Schedules -Summary")
For i = 2 To 26
For j = 0 To UBound(codes)
.Cells(37 + j, i).Formula = Application.WorksheetFunction.SumIfs(Sheets("Sheet1").Range("AD8:AD" & n), Sheets("Sheet1").Range("F8:F" & n), Sheets("Sheet2").Range(Chr(64 + i) & "8"), Sheets("Sheet1").Range("AC8:AC" & n), codes(j))
Next j
Next i
End With
Some basic syntax of SUMIFS:
The first part is to specify the sum range
The Second part(s) is to specify the criteria range, and then the critera
This goes on for 197 criterias i believe!
For any Novices looking at the various qualifications of variables above:
i,j, and n are variables of type long. n is used to count (dynamically), the number of rows in the target dataset

Multiple array assignment to an excel range

Just out of curiosity ,I am asking you a question which is as below:
Suppose i do have an array A1(6)=(45,25,,36,88),A2(6)=(14,25,11),A3(6)=(11,21,20,25,48).Now can we put those array values with the help of a single statement like single array assignment to a row,as here all the rows to a range of an Excel, Say here "C1:R3" range.
EDIT
If I need to assign them to a row like R1<- A1 + A2 +A3,R2<- A1 + A2 +A3. Can you tell me how to this?
R1<- (45,25,,36,88),14,25,11,,,,11,21,20,25,48,) same for R2.
Thanks,
Dim A(2,5)
For i = 0 to 5
A(0, i) = A1(i)
A(1, i) = A2(i)
A(2, i) = A3(i)
Next i
Range("C1:R3").Value = A
EDIT
For second part, to the best of my understanding:
Dim R(17)
For i = 0 To 2
For j = 0 To 5
R(6 * i + j) = A(i, j)
Next j
Next i
Range("C5:T5").Value = R
EDIT 2
Alternatively:
Dim R
R = Split(Join(A1, ",") & "," & Join(A2, ",") & "," & Join(A3, ","), ",")
Range("C5:T5").Value = R
You can use any delimiter you like (if it's appropriate for your data).

Resources