出于同样的思路,Google表格可能并不适合所有人,该宏可能并不适合所有人,但可能适合某人。
它会在选定范围内运行,并用截断的文本替换溢出的单元格。
标志确定是否:
默认为保留数据并使用两个链接。
Option Explicit
Sub LinkTruncatedCells()
Dim rng As Range: Set rng = Selection
Dim preserveValues As Boolean: preserveValues = True
Dim linkAsFormula As Boolean: linkAsFormula = True
Dim linkAsHyperlink As Boolean: linkAsHyperlink = True
Dim w As Single
Dim c As Range
Dim r As Range
Dim t As Long
Dim l As Long
Dim s As String
Dim ws As Worksheet
Dim ns As Worksheet
Application.ScreenUpdating = False
Set ws = rng.Parent
For Each c In rng.Columns
w = c.ColumnWidth
t = 0
l = 0
For Each r In c.Rows
If Len(r) > l Then
s = r
If CBool(l) Then r = Left(s, l)
Do
r.Columns.AutoFit
If r.ColumnWidth > w And Len(s) > t Then
t = t + 1
r = Left(s, Len(s) - t)
l = Len(r)
End If
Loop Until t = Len(s) Or r.ColumnWidth <= w
r.ColumnWidth = w
If r <> s And preserveValues Then
If ns Is Nothing Then
Set ns = ws.Parent.Worksheets.Add(after:=ws)
End If
ns.Range(r.Address) = s
If linkAsFormula Then r.Formula = "=LEFT(" & ns.Name & "!" & r.Address & "," & l & ")"
If linkAsHyperlink Then ws.Hyperlinks.Add Anchor:=r, Address:="", SubAddress:= _
ns.Range(r.Address).Address(external:=True)
End If
End If
Next r
Next c
ws.Activate
Application.ScreenUpdating = True
End Sub
最后说明:我已将其用于个人项目,并发现它可靠,但是在尝试任何不熟悉的宏之前,请保存并备份您的工作。