Excel 将20个元素作为一个输入
我试图通过一次插入20个元素来自动化输入。我需要使用的textarea不允许我使用(“A2:A21”)作为范围。我需要以某种方式创建一个变量,一次存储20个元素,但我还有第二个问题,如果我总共有65个元素,那么当它完成所有20个元素组时,如何创建变量,以创建一个5个组Excel 将20个元素作为一个输入,excel,vba,web-scraping,Excel,Vba,Web Scraping,我试图通过一次插入20个元素来自动化输入。我需要使用的textarea不允许我使用(“A2:A21”)作为范围。我需要以某种方式创建一个变量,一次存储20个元素,但我还有第二个问题,如果我总共有65个元素,那么当它完成所有20个元素组时,如何创建变量,以创建一个5个组 Sub test() Set thisWbs = ActiveWorkbook.ActiveSheet Set ie = CreateObject("InternetExplorer.Application") i =
Sub test()
Set thisWbs = ActiveWorkbook.ActiveSheet
Set ie = CreateObject("InternetExplorer.Application")
i = i
x = i + 19
ie.Visible = True
Set Rng = thisWbs.Range("A2:A" & x)
my_url = "index.html"
ie.navigate my_url
While ie.readyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getElementsByTagName("input")(2).Focus
ie.document.getElementsByTagName("input")(2).Click
ie.document.getElementsByName("fnsku")(1).Focus
ie.document.getElementsByName("fnsku")(1).Click
ie.document.getElementsByName("fnsku")(1).innerText = Range("A2" & x)
i = i + 1
Next x
End Sub
子测试()
设置thisWbs=ActiveWorkbook.ActiveSheet
设置ie=CreateObject(“InternetExplorer.Application”)
i=i
x=i+19
可见=真实
设置Rng=此WBS.范围(“A2:A”和x)
my_url=“index.html”
浏览我的网址
当ie.readyState 4或ie.Busy:DoEvents:Wend
ie.document.getElementsByTagName(“输入”)(2).焦点
ie.document.getElementsByTagName(“输入”)(2)。单击
ie.document.getElementsByName(“fnsku”)(1).焦点
ie.document.getElementsByName(“fnsku”)(1)。单击
ie.document.getElementsByName(“fnsku”)(1.innerText=范围(“A2”和x)
i=i+1
下一个x
端接头
我将在此附上一个有效的代码:
Sub test()
Set thisWbs = ActiveWorkbook.ActiveSheet
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
my_url = "index.html"
ie.navigate my_url
While ie.readyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getElementsByTagName("input")(2).Focus
ie.document.getElementsByTagName("input")(2).Click
ie.document.getElementsByName("fnsku")(1).Focus
ie.document.getElementsByName("fnsku")(1).Click
ie.document.getElementsByName("fnsku")(1).innerText = Range("A1")
End Sub
子测试()
设置thisWbs=ActiveWorkbook.ActiveSheet
设置ie=CreateObject(“InternetExplorer.Application”)
可见=真实
my_url=“index.html”
浏览我的网址
当ie.readyState 4或ie.Busy:DoEvents:Wend
ie.document.getElementsByTagName(“输入”)(2).焦点
ie.document.getElementsByTagName(“输入”)(2)。单击
ie.document.getElementsByName(“fnsku”)(1).焦点
ie.document.getElementsByName(“fnsku”)(1)。单击
ie.document.getElementsByName(“fnsku”)(1.innerText=范围(“A1”)
端接头
使用上面提到的代码,它用A1中的值填充textarea,但我希望它用A1到A19的值填充它,然后用A20到A39和A40到A45的值填充它(如果可能,则用20个项目,如果不使用剩余的项目)
我所有的数据都在A列中,如下所示:A2=1,A3=2,A4=3…A64=63,A65=64。你的意思是这样的吗
Const WINDOW_SIZE As Long = 20
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'your worksheet name
Dim LastRow As Long 'find last data row in column A
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Dim iRow As Long
For iRow = 2 To LastRow Step WINDOW_SIZE 'pick window sizes of 20 rows
Dim ConcatValues As String
ConcatValues = vbNullString 'initialize
'concatenate the 20 values to one
Dim sRow As Long
For sRow = iRow To WorksheetFunction.Min(iRow + WINDOW_SIZE - 1, LastRow)
ConcatValues = ConcatValues & ws.Range("A" & sRow).Value & vbCrLf
Next sRow
'put that concatenated value in your element
ie.document.getElementsByName("fnsku")(1).innerText = ie.document.getElementsByName("fnsku")(1).innerText & ConcatValues & vbCrLf
Debug.Print ConcatValues & vbCrLf 'this can be removed in productive code later
'this has to be adjusted by your needs
Next iRow
编辑:和p一样的想法ᴇʜ 你可以通过循环来完成
Sub test()
Dim thisWbs As Worksheet
Dim ie As Object
Dim my_url As String
Dim upToTwenty As String
Dim upToTwentyCounter As Long
Dim currentRow As Long
Set thisWbs = ActiveWorkbook.ActiveSheet
currentRow = 2
my_url = "index.html"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate my_url
While ie.readyState <> 4 Or ie.Busy: DoEvents: Wend
'What happens after each click?
'It is possible that a small pause is necessary to
'execute the action behind a click before continuing
ie.document.getElementsByTagName("input")(2).Click
ie.document.getElementsByName("fnsku")(1).Click
'Combine up to 20 values from column A
'If a separator is required, it must be inserted
Do While thisWbs.Cells(currentRow, 1).Value <> "" Or upToTwentyCounter > 20
upToTwenty = upToTwenty & thisWbs.Cells(currentRow, 1).Value
upToTwentyCounter = upToTwentyCounter + 1
currentRow = currentRow + 1
Loop
ie.document.getElementsByName("fnsku")(1).innerText = upToTwenty
'Reset variables
upToTwenty = ""
upToTwentyCounter = 0
End Sub
子测试()
将此WBS设置为工作表
模糊的物体
将我的url设置为字符串
把二十当作线
暗到二十个计数器一样长
与当前行一样长
设置thisWbs=ActiveWorkbook.ActiveSheet
currentRow=2
my_url=“index.html”
设置ie=CreateObject(“InternetExplorer.Application”)
可见=真实
浏览我的网址
当ie.readyState 4或ie.Busy:DoEvents:Wend
'每次单击后会发生什么?
“有可能需要稍作停顿,以便
'在继续之前,在单击后执行操作
ie.document.getElementsByTagName(“输入”)(2)。单击
ie.document.getElementsByName(“fnsku”)(1)。单击
'组合A列中最多20个值
'如果需要分隔符,则必须插入分隔符
当此WBS.Cells(currentRow,1).Value“”或upToTwentyCounter>20时执行此操作
UptoThous=UptoThous和thisWbs.Cells(当前行,1).Value
UptoTwenty计数器=UptoTwenty计数器+1
currentRow=currentRow+1
环
ie.document.getElementsByName(“fnsku”)(1.innerText=Upto20
'重置变量
多达二十个=“”
最多20个计数器=0
端接头
你到底想做什么还不清楚。您的下一个x
也没有的。i=i
的目的是什么?在这一点上没有任何意义i
是0
,它将保持0
。请改进你的问题,告诉你的代码有什么问题,并给出一个你想要实现的好例子。阅读[mvce]有助于改进您的问题。我已编辑了我的问题。当thisWbs.Cells(currentRow,1).Value“”或upToTwentyCounter>20时,Do上的应用程序定义或对象定义错误,因为currentRow
未初始化,它是0
,并且一行0
不存在。在运行循环之前,用currentRow=2
初始化currentRow
。@Psko是的,我忘了初始化它。只需像P一样插入顶部ᴇʜ写道。PeH的方法可行,但似乎这个方法在初始化之后仍然不起作用。@Psko我只是想告诉你如何在代码中组合值。如果您可以使用P的解决方案ᴇʜ,太好了:-)它可以工作,也可以不工作,它只连接最后4个元素(因此排除20个元素的集合),但我不需要连接,我需要它们彼此重叠。@Psko请编辑您的原始问题,并添加一些示例源数据和所需结果,这些数据和结果应该插入.innerText
。否则就不清楚你的意思了。截图可能也有帮助请注意,这段代码也必须根据您的需要进行调整,然后它将按需要工作。签出我的编辑