VBA脚本与Excel和IE

我试图用列A中的值打开一个.xlsx文件(比方说A1:A1000)。

我需要脚本从A1中select值并将其添加到URL中。 然后需要打开该URL并更改该特定网站上的设置。 然后应该确认更改并继续下一个值A2,直到最后一个值(一个循环)。

我已经拿出他跟随:

Set IE = CreateObject("InternetExplorer.Application") IE.Visible = 1 IE.navigate "http://xxxx/edit_product.php?Prod_ID=45306" Do While (IE.Busy) WScript.Sleep 100 Loop Set Helem = IE.document.getElementByID("Status") Helem.Value = "The change I need" Do While (IE.Busy) WScript.Sleep 100 Loop Set Shell = WScript.CreateObject("WScript.Shell") Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{ENTER}" 

在此代码中,URL中的prod_ID“45306”是来自单元格的值,因此应该是variables值。

在网站上的行动工作,但我如何打开一个Excel文件并循环操作(并使urlvariables)?

你是否想从另一个工作簿运行此代码,然后打开一个不同的工作簿,其中的值?

尝试这个:

  Dim lr As Long, prodID as string Dim wb As Workbook, ws As Worksheet Set wb = Workbooks.Open(Filename:="c:\temp\attachment.xlsx") 'Set path to your file Set ws = wb.Sheets("Sheet1") 'Set name of your worksheet lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'Last row used in column A For i = 1 To lr prodID = ws.Range("A" & i).Value IE.navigate "http://xxxx/edit_product.php?Prod_ID=" & prodID Do While (IE.Busy) WScript.Sleep 100 Loop Set Helem = IE.document.getElementByID("Status") Helem.Value = "The change I need" Do While (IE.Busy) WScript.Sleep 100 Loop Set Shell = WScript.CreateObject("WScript.Shell") Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{ENTER}" Next 

好的,这里用于vbs。

我对vbs不太熟悉,但请尝试下面的代码。 如果遇到任何错误,请告诉我。

 Dim xlApp, wb, ws Dim filename, LastRow, prodID Set xlApp = CreateObject("Excel.Application") Set wb = xlapp.Workbooks.Open("c:\temp\attachment.xls") 'Set path to your file Set ws = wb.Sheets("Sheet1") 'Set name of your worksheet Set IE = CreateObject("InternetExplorer.Application") IE.Visible = 1 For i = 1 To 10 prodID = ws.Range("A" & i).Value IE.navigate "http://xxxx/edit_product.php?Prod_ID=" & prodID Do While (IE.Busy) WScript.Sleep 100 Loop Set Helem = IE.document.getElementByID("Status") Helem.Value = "The change I need" Do While (IE.Busy) WScript.Sleep 100 Loop Set Shell = WScript.CreateObject("WScript.Shell") Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{TAB}" Shell.SendKeys "{ENTER}" Next wb.Close False xlApp.Quit set ws = Nothing Set wb = Nothing Set xlApp = Nothing 

在JazzzyJoop的帮助下,我得到了所需的脚本:D谢谢JazzyJoop。 在任何人需要相同的东西的代码下面

 Dim xlApp, wb, ws Dim filename, LastRow, prodID Set xlApp = CreateObject("Excel.Application") Set wb = xlapp.Workbooks.Open("c:\temp\attachment1.xlsx") 'Set path to your file Set ws = wb.Sheets("Sheet1") 'Set name of your worksheet Set IE = CreateObject("InternetExplorer.Application") IE.Visible = 1 For i = 1 To 4 'Range of values prodID = ws.Range("A" & i).Value IE.navigate "http://xxxx/edit_product.php?Prod_ID=" & prodID Do While (IE.Busy) WScript.Sleep 50 Loop IE.Document.getElementById("Status").Value = "Status Needed" WScript.Sleep 100 Set oInputs = IE.Document.getElementsByTagName("input") For Each elm In oInputs If elm.Value = "Verwerk" Then elm.Click Exit For End If Next Do While IE.Busy Or IE.readyState <> 4 WScript.Sleep 50 Loop Next wb.Close False xlApp.Quit set ws = Nothing Set wb = Nothing Set xlApp = Nothing