首先需要知道你要引用的数据在Excel中的位置,然后在VB里面引用Excel,
然后打开Excel文件读数据
Dim scxls As ExcelApplication
Dim scbook As ExcelWorkbook
Dim scsheet As ExcelWorksheet
‘Set scxls = CreateObject("excelapplication")
‘Set scbook = scxlsWorkbooksAdd
Set scbook = scxlsWorkbooksopen("c:\1xls")
Set scsheet = scbookWorksheets(1)
‘scsheetCells(1, 2) = "1111" ’写入数据
a=scsheetCells(1, 2) '读取数据
Set scbook = Nothing
Set scxls = Nothing
‘scxlsVisible = True ’显示Excle
数据较多,且有规律可循时,请使用循环语句
'以下是EXCEL中的放置 ,excel文件是:E:\数据xls
红桃 黑桃 梅花 方块
A 探囊取物 休养生息1 休养生息2 休养生息3
1
2
3
…………
K
'以下是程序,在窗体中建一下COMMAND1
Private Sub Command1_Click()
dim 汉字(12,3) as string '定义一个二维数组,为字符型
dim ii as integer
dim jj as integer
Set oleExcel = CreateObject("ExcelApplication")
oleExcelVisible = True
oleExcelWorkbooksOpen FileName:="E:\数据xls"
For ii= 0 To 12
for jj=0 to 3
汉字(ii,jj) == oleExcelWorksheets("Sheet1")Range("A1")Cells(ii+2, jj+2)
next jj
next ii
oleExcelSave
oleExceldisplayalerts = False
oleExcelQuit
End Sub
'另: 需要引用microsoft activex data objects 25 library
microsoft DAO 351 object library
sub test()
Set xlsApp=GetObject(,"excelapplication")
if xlsApp Is Nothing then exit sub
msgbox xlsAppActiveCellValue
end sub
用Set xlsApp=GetObject(,"excelapplication")可以获得当前打开的excel程序
可以用xlsAppworkbooks(i)表示第i个打开的工作簿(也就是xls文件)
再往下是xlsAppworkbooks(i)sheets(j)表示上述工作簿的第j个工作表
再往下是xlsAppworkbooks(i)sheets(j)cells(rowIndex, ColIndex)表示上述工作表rowIndex行,ColIndex列的单元格,或者用类似xlsAppworkbooks(i)sheets(j)range("A1:B10")取得上述工作表中的A1:B10区域
如果在装office的时候选上“VBA帮助文件”,可以进excel后按alt + F11进入“Microsoft Visual basic”编辑器,然后按F1查看帮助中关于Excel对象模型以及相关对象、属性、方法、事件的介绍
'引用:
'添加对该对象的引用 工程菜单-->引用,找到 Microsoft Scripting Runtime
'引用microsoft Excel 140 object library
'
Dim elApp As ExcelApplication
Dim elBooks As ExcelWorkbook
Dim ekSheet As ExcelWorksheet
Dim TblMap_Card '创建一个变量
Private Sub Command1_Click()
Dim i As Integer
openEl
Set dic = CreateObject("ScriptingDictionary")
'MsgBox ekSheetCells(RowsCount, 1)End(3)Row
For i = 2 To ekSheetCells(RowsCount, 1)End(3)Row
If dicExists(ekSheetCells(i, 2)Value) Then
dic(ekSheetCells(i, 1)Value) = dic(ekSheetCells(i, 1)Value) + ekSheetCells(i, 2)Value
Else
dic(ekSheetCells(i, 1)Value) = ekSheetCells(i, 2)Value
End If
Next i
ekSheetRange("H:J")Clear
'ekSheetCells(1, 9)Resize(1, 2) = Array("商品", "售量")
ekSheetCells(2, 9)Resize(dicCount, 1) = ApplicationTranspose(dicKeys)
ekSheetCells(2, 10)Resize(dicCount, 1) = ApplicationTranspose(dicItems)
End Sub
Private Sub openEl()
Dim myPath As String
myPath = "\weekxlsx"
Set elApp = CreateObject("ExcelApplication")
Set elBooks = elAppWorkbooksOpen(AppPath & myPath)
Set ekSheet = elBooksWorksheets("Sheet1")
'Set ekSheet = elBooksWorksheets(1)
elAppVisible = True
End Sub
VB6的,测试通过。界面上只有一个按钮。
dim
a(3,
3)
as
intege
for
i
=
1
to
3
for
j
=
1
to
3
'这步是利用a到c之间的ascii码来处理,如果是i循环1,5的话,那就会是a,b,c,d,e,内循环j管a3~a5,
a(i,
j)
=
int(xlsheetrange(chr(asc("a")+i-1))
&
cstr(j+2))
next
j,
i
Dim appexcel As Object '定义Excel应用程序对象
Dim wbmybook As Object '定义工作簿对象
Dim wsmysheet As Object '定义工作表对象
Set appexcel = CreateObject("excelapplication") '创建Excel应用程序对象
Set wbmybook = appexcelworkbooksAdd '添加工作簿
Set wsmysheet = appexcelworksheetsAdd '添加工作表
wsmysheetcells(1, 1) = "123"'向EXCEL里写数据
appexcelVisible = True '应用程序Excel可见
Set wbmybook = Nothing
Set wsmysheet = Nothing
Set appexcel = Nothing
dim
a(3,
3)
as
intege
for
i
=
1
to
3
for
j
=
1
to
3
'这步是利用a到c之间的ascii码来处理,如果是i循环1,5的话,那就会是a,b,c,d,e,内循环j管a3~a5,
a(i,
j)
=
int(xlsheetrange(chr(asc("a")+i-1))
&
cstr(j+2))
next
j,
i
以上就是关于请教请问如何用vb提取excel表格中的数据全部的内容,包括:请教请问如何用vb提取excel表格中的数据、怎样用vb 读取excel 的数据、vb如何读取当前活动的excel单元格的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)