简体   繁体   English

Excel VBA循环&每个(按行)

[英]Excel VBA Loops & For each (by row)

I'm currently building a Web Scraper to check patient eligibility using Excel VBA and the Selenium web driver. 我目前正在构建一个Web Scraper,以使用Excel VBA和Selenium Web驱动程序检查患者的资格。 I've gotten stuck try to figure out how reference each row in my data set. 我一直努力找出如何引用数据集中的每一行。 Currently I'm able to reference the first row of my data but not the next: User Input Output Dataset EX. 当前,我能够引用我的数据的第一行,但不能引用下一行: 用户输入输出数据集 EX。

Here's the code: 这是代码:

Sub WebScraper()
Dim driver As New IEDriver
Dim sh As Worksheet
Dim rw As Range
Dim i As Integer
i = 2
website = Elig_Check_Form.Website_CBX.Text
UserName = Elig_Check_Form.Username_TXT.Text
Password = Elig_Check_Form.Password_TXT.Text
LName = Sheets(1).Cells(i, 3).Value
FName = Sheets(1).Cells(i, 2).Value
DOB = Format(Sheets(1).Cells(i, 4).Value, "MM/DD/YYYY") 
DOS = Format(Sheets(1).Cells(i, 5).Value, "MM/DD/YYYY")
EligStart = Sheets(1).Cells(i, 8).Value 
EligEnd = Sheets(1).Cells(i, 9).Value
EligStat = Sheets(1).Cells(i, 7).Value
WebsiteURL = "https://healthid.optum.com/tb/app/index.html?TARGET=https%3A2F%2Fprovider-linkhealth.unitedhealthcareonline.com%2Fb2c%2FLoginAction.do3Ftype%3DTmV0c2NhcGUjNS4wIChXaW5kb3dzIE5UIDYuMTsgV09XNjQ7IFRyaWRlbnQvNy4wOyBTTENDMjsgLk5FVCBDTFIgMi4wLjUwNzI3OyAuTkVUIENMUiAzLjUuMzA3Mjk7IC5ORVQgQ0xSIDMuMC4zMDcyOTsgLk5FVDQuMEM7IC5ORVQ0LjBFOyBydjoxMS4wKSBsaWtlIEdlY2tvI2ZhbHNlIzEx&relyingAppId=UHO60026#/login"
driver.Get WebsiteURL
driver.Window.Maximize
driver.FindElementById("userNameId_input").SendKeys (UserName) 'Reads Username
driver.FindElementById("passwdId_input").SendKeys (Password) 'Reads password
driver.FindElementById("SignIn").Click
Application.Wait (Now + TimeValue("0:00:10"))
On Error GoTo PasswordErr
driver.FindElementByXPath("//*[@id='app']/div/div[1]/div[2]/div[2]/div[5]/div[7]/div/a").Click

On Error Resume Next
driver.FindElementByName("AGREE").Click
'On Error GoTo 0
driver.FindElementByXPath("//*[@id='app']/div/div[1]/div[2]/div[2]/div[5]/div[12]/div/a/div/h3").Click
Application.Wait (Now + TimeValue("0:00:05"))
driver.FindElementByName("save").Click
 On Error GoTo ChooseProvider

'Do Until IsEmpty(Cells(iFib, 1))
 Do While i < 1000 And Format(LName) <> vbNullString
 Set sh = ActiveSheet
 For Each rw In sh.Rows


 driver.FindElementById("NameDateOfBirth-searchoptions").Click
 driver.FindElementById("last-name-[0]").SendKeys (LName) 
 driver.FindElementById("first-name-[0]").SendKeys (FName)
 driver.FindElementById("date-of-birth-[0]").SendKeys (DOB)
 driver.FindElementById("service-start-[0]").SendKeys (DOS)
 driver.FindElementById("search-button").Click
 Application.Wait (Now + TimeValue("0:00:05"))
 On Error GoTo NotFound
 Sheets(1).Cells(i, 12).Value = driver.FindElementByXPath("//*[@id='patient-     provider-information-section']/div/div/div[1]/div[1]/div/div[2]/div[1]/div/div/div[2]/div[1]/p").Text
 Sheets(1).Cells(i, 11).Value = driver.FindElementByXPath("//*[@id='patient-provider-information-section']/div/div/div[1]/div[1]/div/div[2]/div[1]/div/div/div[1]/div/div/span/span[1]/h3").Text
 Sheets(1).Cells(i, 10).Value = driver.FindElementByXPath("//*[@id='insurance-information-section']/div/div[1]/div/div[1]/div/div/div[1]/div/p").Text
 Sheets(1).Cells(i, 8).Value = Left(driver.FindElementByXPath("//*[@id='insurance-information-section']/div/div[1]/div/div[1]/div/div/div[1]/div/p").Text, 10)
 Sheets(1).Cells(i, 9).Value = Right(driver.FindElementByXPath("//*[@id='insurance-information-section']/div/div[1]/div/div[1]/div/div/div[1]/div/p").Text, 10)
 If Sheets(1).Cells(i, 5).Value >= Sheets(1).Cells(i, 8).Value Or Sheets(1).Cells(i, 5).Value < Sheets(1).Cells(i, 9).Value Then
 Sheets(1).Cells(i, 7).Value = "Eligible"
 Else: Sheets(1).Cells(i, 7).Value = "Not Eligible"


 Exit For

 End If
 driver.FindElementByXPath("//*[@id='ng-app']/div[3]/div/div/div/div/div/div/div/div/div[1]/div[1]/div/div/div/div[2]/ul/li[1]/span").Click
 i = i + 1


 Next rw
 Loop

The follow variables needed to be moved to after the do while statement 在do while语句之后需要将跟随变量移至

 LName = Sheets(1).Cells(i, 3).Value
 FName = Sheets(1).Cells(i, 2).Value
 DOB = Format(Sheets(1).Cells(i, 4).Value, "MM/DD/YYYY") 
 DOS = Format(Sheets(1).Cells(i, 5).Value, "MM/DD/YYYY")
 EligStart = Sheets(1).Cells(i, 8).Value 
 EligEnd = Sheets(1).Cells(i, 9).Value
 EligStat = Sheets(1).Cells(i, 7).Value
 i = 2

I also used a For loop instead of a do while loop. 我还使用了For循环而不是do while循环。 I can post the whole code if needed. 如果需要,我可以发布整个代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM