
Function lines(r) 'r 为某单元格,如A1
rWrapText = False
x = rHeight
rWrapText = True
y = rHeight
lines = y / x
End Function
使用方法 lines(range("A1"))
思路:设置该单元格不自动换行,得到此时的高度x,恢复自动换行,得到此时的高度y,行数=y/x
我把我以前用excel做的类似的module改了下发给你
你可能需要在工具引用那里添加 microsoft word 140 object library
以及 microsoft scripting runtime。
你可以改下用word vba,然后txt那部分的处理用别的方法做就不用添加引用了
Sub CountTxtWord()
Dim fName As String, fPath As String
Dim s As String
Dim i As Integer
Dim WrdApp As Object
Set WrdApp = CreateObject("wordapplication")
ActiveSheetCellsClear
fPath = "D:\test6\"
fName = Dir(fPath)
i = 2
Do Until fName = ""
If Right(fName, 4) = "txt" Then
With New ScriptingFileSystemObject
With OpenTextFile(fPath & fName)
s = ReadAll
s = Replace(s, " ", "")
Cells(i, 1) = fPath & fName
Cells(i, 2) = Len(s)
i = i + 1
End With
End With
Else
WrdAppDocumentsOpen (fPath & fName)
s = WrdAppActiveDocumentRangeText
WrdAppActiveDocumentClose False
s = Replace(s, " ", "")
Cells(i, 1) = fPath & fName
Cells(i, 2) = Len(s)
i = i + 1
End If
fName = Dir
Loop
Columns(1)AutoFit
WrdAppQuit
End Sub
百度了下就找到了:
这是行数:
其它你想要的值:
字符数:
代码,方便你复制测试:
ActiveDocumentComputeStatistics(wdStatisticCharacters)
百度这样搜索,限定在clubexcelhomenet网站内搜索,这个是国内最历史悠久的VBA论坛,几乎没有找不到的代码:
OK,授人以鱼并授之以渔,Over。
按alt+f11 输入以下代码:
Public Sub findtxt()
Dim n, f
n = 0
f = InputBox("请输入查找的文本", vbOKCancel, "请输入查找文本")
ActiveDocumentRangeSelect
SelectionFindClearFormatting
With SelectionFind
Text = f
Forward = True
End With
Do While True
SelectionFindExecute
If SelectionFindFound Then
n = n + 1
Else
Exit Do
End If
Loop
MsgBox "共查找到" & n & "次" & f & " 在这个文章中"
End Sub
按F5或运行宏findtxt执行
iRows=activesheetusedrangerowscount\x0d\iColumns=activesheetusedrangeColumnscount\x0d\\x0d\'如果表格前面的几行或几列可能是空的,需要获得最下面的行数和最右面的列数:\x0d\with activesheetusedrange\x0d\ iEndRow=rowscount+row-1\x0d\ iEndColumn=Columnscount+column-1\x0d\end with
有的时候在 *** 作excel的时候,涉及到循环什么时候结束,就非常需要知道 *** 作区域的最后一行是第几行,或者最后一列是第几列。
因此这边使用下面两个函数,可以比较快速的获取整个区域的行数和列数(不是指定的的区域)
主要就是使用了CountA来统计非空单元格的数量,然后这边传的参数是一整行或者一整列,计算这行和这列有多少个非空单元格,就知道这个表格的行数和列数了。
Sub AAA()
Dim FilePath As String '要读取的文件路径
Dim S1 As String '文档的内容
Dim S2 As String '提取到的内容
Dim Ar As Variant '用于保存最终结果
Dim L1 As Long '记录当前查找到的字符位置
FilePath = ApplicationGetSaveAsFilename(fileFilter:="Word文档,doc;docx")
If FilePath = "False" Then MsgBox "您没有选择文件,将退出程序。": Exit Sub
With CreateObject("wordapplication")
With DocumentsOpen(FilePath, True, True)
S1 = Content
Close False
End With
Quit
End With
L1 = InStr(S1, "<") '第一个 < 位置
Do Until L1 = 0
If Len(S2) <> 0 Then
S2 = S2 & "Crazy0qwer" & Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1)
Else
S2 = Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1)
End If
L1 = InStr(L1 + 1, S1, "<")
Loop
Ar = Split(S2, "Crazy0qwer")
Range("A1")Resize(UBound(Ar) + 1) = ApplicationTranspose(Ar)
End Sub
以上就是关于请问如何用vba获取excel某一单元格中文字的行数(单元格是自动换行格式)全部的内容,包括:请问如何用vba获取excel某一单元格中文字的行数(单元格是自动换行格式)、如何写VBA批量统计word和txt的字符数(不含空格)、office2016如何把docx文件的字数统计信息提取出来等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)