|
本帖最后由 yy6831817 于 2024-6-5 14:38 编辑
老师帮忙修改一下代码,下以代码运行时结果不会去除空行
Sub 粘贴不含隐藏行的内容1()
Dim rng As Range
Dim cell As Range
Dim visibleText As String
Dim startCell As Range
' 选择要粘贴的单元格范围
Set rng = Application.Selection
' 获取开始粘贴的单元格位置
On Error Resume Next
Set startCell = Application.InputBox("请选择要粘贴的单元格开始位置", Type:=8)
On Error GoTo 0
' 如果用户取消了输入框,则退出程序
If startCell Is Nothing Then
Exit Sub
End If
' 遍历选定范围内的每个单元格
For Each cell In rng
' 检查单元格所在行是否隐藏,如果是则跳过
If cell.EntireRow.Hidden = False Then
' 获取单元格中的显示文本
visibleText = cell.Text
' 去除文本中的空格、换行符
visibleText = Replace(visibleText, " ", "")
visibleText = Replace(visibleText, vbCrLf, "")
visibleText = Replace(visibleText, vbLf, "")
visibleText = Replace(visibleText, vbCr, "")
visibleText = Replace(visibleText, Chr(10), "")
' 将处理后的文本粘贴到当前单元格
startCell.Offset(cell.Row - rng.Cells(1).Row, cell.Column - rng.Cells(1).Column).Value = visibleText
End If
Next cell
End Sub
|
|