我已经在Excel VBA中编写了一个宏,该宏基本上将1440行复制粘贴了53行,一次又一次地粘贴,以便填充〜70000行表中的两列。该宏有效,但是完全运行大约需要五分钟。如果我不必在〜1000个其他文件上运行此文件,那就很好。我正在寻找任何方法来加快此过程,以使它不需要5天就能运行。
我尝试使用范围复制方法:
resp = requests.get('https://xlnindia.gov.in/frm_G_Cold_S_Query.aspx',verify=False)
soup = BeautifulSoup(resp.text,"lxml")
dictinfo = {i['name']:i.get('value','') for i in soup.select('input[name]')}
dictinfo['ddlState']='Delhi'
dictinfo['ddldistrict']='Adarsh Nagar (4)'
dictinfo['__EVENTTARGET']='btnSearch'
dictinfo = {k:(None,str(v)) for k,v in dictinfo.items()}
r=requests.post('https://xlnindia.gov.in/frm_G_Cold_S_Query.aspx',verify=False,files=dictinfo)
r
,但是花费了甚至更长的时间。
这是我当前的代码:
Set range1 = {the table I'm copying}
Set range2 = {the cells I want to paste into}
range1.Copy range2
我正在考虑该解决方案可能与在VBA中使用sql有关,但是我尚未学习该语法。无论哪种方式,任何建议都将不胜感激。谢谢您的阅读!
答案 0 :(得分:2)
将所有内容加载到一个数组中,然后最后输出整个数组。重构代码以避免使用激活/选择
Sub tgr()
Dim wbDest As Workbook
Dim wbData As Workbook
Dim wsDest As Worksheet
Dim wsData As Worksheet
Dim aTemp() As Variant
Dim aData() As Variant
Dim SiteName As String
Dim RepeatData As Long
Dim ixTemp As Long
Dim ixData As Long
Dim ixCol As Long
SiteName = "SiteName1"
RepeatData = 1440
Set wbDest = ThisWorkbook
Set wbData = Workbooks("as_built_comp.xlsm")
Set wsDest = wbDest.Worksheets(1)
Set wsData = wbData.Worksheets(SiteName)
With wsData.Range("C2:D" & wsData.Cells(wsData.Rows.Count, "C").End(xlUp).Row)
If .Row < 2 Then Exit Sub 'No data
aTemp = .Value
ReDim aData(1 To .Rows.Count * RepeatData, 1 To .Columns.Count)
End With
For ixData = 1 To UBound(aData, 1)
ixTemp = ((ixData - 1) Mod UBound(aTemp, 1)) + 1
For ixCol = 1 To UBound(aTemp, 2)
aData(ixData, ixCol) = aTemp(ixTemp, ixCol)
Next ixCol
Next ixData
wsDest.Range("I12").Resize(UBound(aData, 1), UBound(aData, 2)).Value = aData
End Sub