何兄,想了也做了一上午。本以为快摆平了。没想到, 这个被空格愚了一下。发现半角空格所占用的位置不好判断。 例如,同是25个长度,就是对不齐。就是一个半角空格与大写的字母占用多少不是一样的,也不是1/2。 下同给出我的不完美的解决方案。 注意最后一个宏,就是测试选中区域的长度的。
没想到,一个小东西,居然想了上午,用了60多行代码。 Sub 表格() Dim atable As Table Dim acolumn As Long, arow As Long, i As Long, i1 As Long Dim arr Dim a As String, b As String Dim arange As Range, astring As String If Selection.Information(wdWithInTable) = True And Selection.Type = wdSelectionIP Then '判断是否在表格中 Set atable = Selection.Tables(1) arow = atable.Rows.Count - 1 acolumn = atable.Columns.Count - 1 '数组的位数 Set arange = atable.Range astring = Replace(arange, Chr(13), "") '把类似于回车的替换掉 astring = Replace(astring, Chr(7) & Chr(7), Chr(7)) '把连续的两个chr(7)替换掉 astring = Mid(astring, 1, Len(astring) - 1) '最后一个也有一个chr(7),把其去掉 arr = Split(astring, Chr(7)) For i1 = 0 To acolumn - 1 '最后一位不要循环,所以,减1 For i = i1 To UBound(arr) Step acolumn + 1 a = arr(i) b = max(b, a) '取得最长的单元格 Next 'MsgBox b '添加空格 Dim c As String c = b & Space(4) 'MsgBox Len(c) For i = i1 To UBound(arr) Step acolumn + 1 If arr(i) = b Then arr(i) = c Else 'MsgBox Len(arr(i)) arr(i) = arr(i) & Space(Len(c) - Len(arr(i))) ' MsgBox Len(arr(i)) End If Next b = "" Next '开始写出字符串。 astring = "" For i = 1 To UBound(arr) + 1 If i Mod (acolumn + 1) = 0 Then astring = astring + arr(i - 1) + Chr(13) Else astring = astring & arr(i - 1) End If Next '在数组中摆平 Else MsgBox "出错的情况可错是:" & Chr(13) & Chr(13) & _ " 1.不合要求!,只要把光标放在表格即可,不要选中表格" & Chr(13) _ & " 2.或者你的光标不在表格中", vbOKOnly, "出错了!" Exit Sub End If atable.Delete Selection.InsertAfter astring End Sub Function max(b, a) If Len(b) > Len(a) Then max = b Else max = a End If End Function Sub 长度() MsgBox Len(Selection.Text) End Sub
[此贴子已经被作者于2006-6-17 10:38:55编辑过] |