- ·上一篇:签名怎么放进excel表格
- ·下一篇:微信怎么同时编辑excel表格
excel表格怎么读写vba文件
1.如何在Excel VBA 中读写word文档 步骤
所以需要先引入word库,操作步骤如下:
1.1 打开excel vba 界面
1.2 选中其中的一个Module
1.3 选择菜单, Tools --> References
在打开的对话框中选择类似 "Microsoft Word 14.0 Object Library".
1.4 点击OK保存配置。
2. 打开文档
Set wordApplication = CreateObject("Word.Application")
wordApplication.Visible = False
Dim hasOpenDoc As Boolean
hasOpenDoc = IsOpen(filePath) ' is a self-defined function to check file is opend
If hasOpenDoc = True then
Set wordDoc = GetObject(filePath)
End if
If hasOpenDoc = False Then
Set wordDoc = wordApplication.Documents.Open(filePath)
End if
wordDoc.Active
With wordApplication
Dim aParagraph As Word.Paragraph
For Each aParagraph In wordDoc.Paragraphs
' do some thing to every paragraph.
Next aParagraph
End with
wordDoc.Close
Set wordDoc = nothing
' 如下这段代码引用某位牛人的,非常感谢他。由于路径丢失,不能给出链接, 抱歉
' 如下的找寻方式,能够正确的找出文件是否被打开
Function IsOpen(fileName As String) As Boolean
IsOpen = False
Dim findFile As Integer
findFile = FreeFile()
On Error GoTo ErrOpen
Open fileName For Binary Lock Read Write As findFile
Close findFile
Exit Function
ErrOpen:
If Err.Number <> 70 Then
Msg = "Error # " & Str(Err.Number) & "was generated by " & Err.Source & Chr(13) & Err.Description
MsgBox Msg, "Error", Err.HelpFile, Err.HelpContext
Else
IsOpen = True
End If
End Function
2.如何通过VBA读写文件
举例如下3.2 VBA中与文件读取的相关方法首先我们来介绍一下使用到的几个方法:Open方法。
该方法用于打开文件,打开后可以得到一个特殊的编号,之后再读取文件的数据都需要该编号。FreeFile函数。
Get方法。使用Open方法打开文件后,该方法可以按字节读取数据。
Seek方法。使用Open方法打开文件后,该方法可以用于定位指定的文件位置(即地址)。
Sub OpenFileTesting() Dim FS As Integer 'File No. Dim Val1 As Integer Dim Val2(1) As Byte Dim Val3(2) As Integer FS = FreeFile '获取一个文件流 '打开文件 Open ThisWorkbook.Path & "\test.xls" For Binary Access Read As FS '顺序读取数据至3个变量 Get FS, , Val1 Get FS, , Val2 Get FS, , Val3 Close FS '关闭文件流 StopEnd Sub。
3.vba读取excel文件数据
这个很简单,你可以私信我,需要点时间.你的excel什么版本,我的是2010, 还有个问题"读取某一个指定路径的文件夹下,所有excel文件的sheet1工作表里 (B2-B15)(D2-D9)的数据",共14+8个数据,"写入到当前工作表 的(F12列)和(H12列)",如何排布? 而且读完第一个文件后,再读下一个文件,所得数据放在哪里,放在第一个文件的数据下方吗?如何对齐? Sub _ForOldFormat() Dim Str1 As String, Str2 As String, xPath As String, xF() As String, exe As String, xI As Integer, xStart As Integer, xEnd As Integer, temp As String, i As Integer Dim xLong As Long, xTemp As String Dim xCup(1 To 22) As String exe = ActiveWorkbook.Name xPath = InputBox("Please input the folder you want to have a list:", "Target Folder", "D:\readexcelfile\1\") If xPath = "" Then Exit Sub If Right(xPath, 1) "\" Then xPath = xPath & "\"'xPath = "P:\Public\Product development\approved IPS\Automotive\" Str1 = Dir(xPath, vbHidden + vbSystem) ReDim Preserve xF(1) xF(1) = Str1 ' look at me i = 2 ReDim Preserve xF(2) If Str1 "" Then Do Str2 = Dir() If Str2 "" Then xF(i) = Str2 i = i + 1 ReDim Preserve xF(i) Else Exit Do End If Loop End If'Have a big cycle to write and remove package type Application.DisplayAlerts = False For xI = 1 To UBound(xF) - 1 DoEvents Workbooks.Open xPath & xF(xI), UpdateLinks:=0'ActiveWorkbook.Sheet1.Activate For i = 1 To 14 xCup(i) = ActiveWorkbook.ActiveSheet.Cells(i + 1, 2) Next For i = 15 To 22 xCup(i) = ActiveWorkbook.ActiveSheet.Cells(i - 13, 4) Next Workbooks(xF(xI)).Close (0) ' savechanges:=False Application.DisplayAlerts = True Windows(exe).Activate'ActiveWorkbook.ActiveSheet.Select xStart = 2 Do Until Cells(xStart, 6) = "" xStart = xStart + 1 Loop For i = xStart To xStart + 13 Cells(i, 6) = xCup(i - xStart + 1) Next For i = xStart To xStart + 7 Cells(i, 8) = xCup(i - xStart + 14) Next Next End Sub 放在ThisWorkBook代码区。
4.vba 读取excel文件
建一个用户窗体,代码如下。
============
Private Sub Form_load()
Dim a(1 To 1000) As String, b(1 To 1000) As String, c(1 To 1000) As String, i As Integer '。。依次类推
For i = 1 To 1000 '这两处的1000改成你的最大行号,足够大
a(i) = Sheet1.Cells(i, "a").Value
b(i) = Sheet1.Cells(i, "b").Value
c(i) = Sheet1.Cells(i, "c").Value
Next
End Sub
这样之后,a、b、c列的内容便存在a(),b(),c()数组中了。
5.如何在excel vba 中读写word文档 步骤
vba读写word'此过程用于把查询到的结果写入一个WORD文档中Private Sub insert_word(ByVal str As String, ByVal path As String) Dim wd As Variant Set wd = CreateObject("word.application") With wd .Visible = True .documents.Open path .Selection.EndKey Unit:=wdStory .Selection.TypeText Text:=str & vbCrLf .ActiveDocument.Save .ActiveDocument.Close .Quit End With Set wd = NothingEnd Sub。