'引用:
'添加对该对象的引用 工程菜单-->引用,找到 Microsoft Scripting Runtime
'引用microsoft Excel 14.0 object library
'
Dim elApp As Excel.Application
Dim elBooks As Excel.Workbook
Dim ekSheet As Excel.Worksheet
Dim TblMap_Card '创建一个变量
Private Sub Command1_Click()
Dim i As Integer
openEl
Set dic = CreateObject("Scripting.Dictionary")
'MsgBox ekSheet.Cells(Rows.Count, 1).End(3).Row
For i = 2 To ekSheet.Cells(Rows.Count, 1).End(3).Row
If dic.Exists(ekSheet.Cells(i, 2).Value) Then
dic(ekSheet.Cells(i, 1).Value) = dic(ekSheet.Cells(i, 1).Value) + ekSheet.Cells(i, 2).Value
Else
dic(ekSheet.Cells(i, 1).Value) = ekSheet.Cells(i, 2).Value
End If
Next i
ekSheet.Range("H:J").Clear
'ekSheet.Cells(1, 9).Resize(1, 2) = Array("商品", "售量")
ekSheet.Cells(2, 9).Resize(dic.Count, 1) = Application.Transpose(dic.Keys)
ekSheet.Cells(2, 10).Resize(dic.Count, 1) = Application.Transpose(dic.Items)
End Sub
Private Sub openEl()
Dim myPath As String
myPath = "\week.xlsx"
Set elApp = CreateObject("Excel.Application")
Set elBooks = elApp.Workbooks.Open(App.Path & myPath)
Set ekSheet = elBooks.Worksheets("Sheet1")
'Set ekSheet = elBooks.Worksheets(1)
elApp.Visible = True
End Sub
张志晨:VB6的,测试通过。界面上只有一个按钮。
你想知道什么?不太明白你的问题。要是想用VB实现上述业务的话,可以这样来做:
1,一个画面追加一个textbox,一个按钮。
2,按钮事件中追加读取Excel该列值的处理。将该列值保存到动态数组中。
3,然后将你输入的助记码到数组中对比。找到匹配的值后,再加入你自己所说的累加运算。
这是我写的读写excel的代码,你可以参考一下:
Dim EXCEL对象 As Excel.Application
Dim 工作薄 As Excel.Workbook
Dim 工作表 As Excel.Worksheet
Dim i As Integer
Dim j As Integer
Private Sub Form_Load()
Set EXCEL对象 = CreateObject("Excel.Application")
Set 工作薄 = EXCEL对象.Workbooks.Open(App.Path & "\123.xls")
Set 工作表 = 工作薄.Worksheets(1)
EXCEL对象.Visible = False
i = 1
j = 1
End Sub
Private Sub 开始_Click()
Do
Do While (j < 10)
工作表.Cells(i, j) = i * j
j = j + 1
Loop
j = 1
i = i + 1
Loop Until (i >= 10)
工作薄.Save
工作薄.Close
EXCEL对象.Quit
End Sub
你这个代码不是读的,是写的,并且按钮不能连按两次