VB,以及VB.NET自动化EXCEL

前端之家收集整理的这篇文章主要介绍了VB,以及VB.NET自动化EXCEL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近由于工作关系,要从网站下载数据,填一个EXCEL,我一下子头大了。数据量太大。

最后写了个程序,发现EXCEL调用起来,挺方便,感觉很爽,thrilling!

vb6.0代码

Dim xls As Excel.Application
Set xls = CreateObject("excel.application")
Dim xlbook As Excel.Workbook
Dim xlbooksource As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlbook = xls.Workbooks.Open("C:/end.xls")
Set xlbooksource = xls.Workbooks.Open("C:/book1.xls")
Dim sheet2 As Excel.Worksheet
Dim Num As Integer
'Set xlsheet = xlbook.Worksheets("户县")
Set xlsheet = xlbook.Worksheets("市区")
xlsheet.Activate
Dim column As Integer
For column = 0 To 4
Set sheet2 = xlbooksource.Worksheets(1 + column)
Dim i As Byte
Dim j As Byte
Dim station As String
Dim count As Integer
Dim sum As Double
sum = 0
count = 0
For i = 3 To xlsheet.UsedRange.Rows.count
station = CStr(xlsheet.Cells(i,1))
station = Trim(station)
Debug.Print (station)
For j = 1 To sheet2.UsedRange.Rows.count
If Trim(CStr(sheet2.Cells(j,4))) = station Then
xlsheet.Cells(i,3 + column) = sheet2.Cells(j,8)
sum = sum + xlsheet.Cells(i,3 + column)
count = count + 1
Exit For
End If
Next
If station = "平均" Then
xlsheet.Cells(i,3 + column) = sum / count
Exit For
End If
Next
Next
xlbook.SaveAs ("C:/test.xls")
xls.Quit

VB.NET 下面代码

.NET下面没有的CELL不能直接赋值,应该

把:cell(i,j)=xxx

改成:cell(i,j).Value= xxx

才成!

其它再无什么大的变化。

直接移值即可。

原文链接:https://www.f2er.com/vb/261544.html

猜你在找的VB相关文章