ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: witboy

[已解决] 如何在单元格里输入“Tab”键?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2009-12-7 22:08 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
原帖由 nevinslee 于 2009-12-7 21:59 发表
有的人,WORD用多了,EXCEL也成WORD了;
更有人,EXCEL用多了,WORD也整成EXCEL了!


TA的精华主题

TA的得分主题

发表于 2011-12-15 15:18 | 显示全部楼层
本帖最后由 九台电脑(公) 于 2011-12-15 15:25 编辑

这些过客都是大傻,只有哥明白你的意思。
楼主的意思是单元格的字符串中有一个字符是TAB字符,是控制字符,看不见的。由于TAB键已经被EXCEL作为它用,所以直接输入不能,复制粘贴也不行,最后我是用VBA也就是宏解决的。
我有2个需求,都解决了

1)由本表单的数据直接生成用TAB分隔的字符串并且保存在相同表单中;
2)从其它程序获取一个字符串,这个字符串中本身已经有TAB键,我要保存到1)的结果的下面,最后还做个比较(比较就是给单元格写一个公式,例如=IF(C1=C2,"OK","NG"))

哥是个程序员,正在做测试,所以才有这个需求。

以下只列出你最关心的部分代码:
1)
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim action As String
    Dim s As String
    Dim sheet As Worksheet
   
    On Error Resume Next
    action = GetRangeData(Target)
    On Error GoTo 0
    If Err.Number <> 0 Then
        Debug.Print "Error(" & Err.Number & "):" & Err.Description & VBA.vbCrLf & Err.Source
        Exit Sub
    End If
   
    Set sheet = Sh
    If action = "期望電文を生成" Then
        sheet.Cells(Target.row, Target.Column + 1).Value = "[" & makeMessage(sheet, 5, 1) & "]"
    ElseIf action = "実際電文を取得" Then
        frmResult.Show VBA.vbModal
        If myCliperBoard.GetFormat(1) Then     'TEXT
            s = myCliperBoard.GetText()
            If Not IsEmpty(s) Then
                sheet.Cells(Target.row, Target.Column + 1).Value = "[" & s & "]"
            End If
        End If
    End If
End Sub

为了从其它程序获取数据,还添加了一个窗口。其实这个窗口可以一闪而过直接关闭的,但是我还是让它显示出来,让用户手动关闭。
Option Explicit

Private Sub cmdOK_Click()
    myCliperBoard.SetText (Me.txtResult.Value)
    Unload Me
End Sub

Private Sub cmdCancel_Click()
    myCliperBoard.Clear
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Me.txtResult.SetFocus
    VBA.SendKeys "^{V}", True   ' to paste
End Sub

TA的精华主题

TA的得分主题

发表于 2011-12-15 15:22 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
九台电脑(公) 发表于 2011-12-15 15:18
这些过客都是大傻,只有哥明白你的意思。
楼主的意思是单元格的字符串中有一个字符是TAB字符,是控制字符, ...

其实核心的代码没有那么长的。我写的是程序,所以有很多其它代码。这里把其它代码也附上。
Option Explicit

Private Const MAX_EMPTY_ROW As Integer = 5
Public myCliperBoard As New DataObject

'to get the data of a cell
Public Function GetData(sheet As Worksheet, ByVal iRow As Integer, ByVal iCol As Integer) As String
    Dim s As String
    Dim r As Range
   
    Set r = sheet.Cells(iRow, iCol)
    s = r.Value
   
    GetData = s
End Function

'to get the data of a range
Public Function GetRangeData(r As Range) As String
    Dim s As String
   
    On Error Resume Next
    s = r.Value
    If Err.Number <> 0 Then
        Debug.Print "Error(" & Err.Number & "):" & Err.Description & VBA.vbCrLf & Err.Source
    End If
    On Error GoTo 0
   
    GetRangeData = s
End Function

'to make message
Public Function makeMessage(sheet As Worksheet, ByVal iRow As Integer, ByVal iCol As Integer) As String
    Dim s As String
    Dim sName As String
    Dim sExpectedValue As String
    Dim c As String
    Dim iSkipCounter As Integer
   
    '番号----列名----列----byte数----パディング----セパレータ----期望列値
    Do While (True) 'loop for fields
       sName = GetData(sheet, iRow, iCol + 1)
       sExpectedValue = GetData(sheet, iRow, iCol + 6)
       c = GetData(sheet, iRow, iCol + 5)
      
       If isEmptyValue(sName) Then
            iSkipCounter = iSkipCounter + 1
            If isToEnd(iSkipCounter) Then
                Exit Do
            End If
        Else
            iSkipCounter = 0
            
            s = s & sExpectedValue & transferSeperator(c)
        End If
        
        iRow = iRow + 1
    Loop
   
    makeMessage = s
End Function

'to transfer seperator
Public Function transferSeperator(ByVal c As String) As String
    Dim s As String
   
    If VBA.UCase(VBA.Trim(c)) = "TAB" Then
        s = VBA.vbTab
    ElseIf VBA.UCase(VBA.Trim(c)) = "SPACE" Then
        s = VBA.Space(1)
    Else
        s = ""
    End If
   
    transferSeperator = s
End Function

'to determine whether it is empty
Public Function isEmptyValue(ByVal sValue As String) As Boolean
    Dim ok As Boolean
   
    If VBA.Trim(sValue) = "" Then
        ok = True
    Else
        ok = False
    End If
   
    isEmptyValue = ok
End Function

'to determine whether it is time to end the process
Public Function isToEnd(ByVal iSkipCounter As Integer) As Boolean
    Dim ok As Boolean
   
    If iSkipCounter > MAX_EMPTY_ROW Then
        ok = True
    Else
        ok = False
    End If

    isToEnd = ok
End Function


总归一句话:VBA后台可以做,但是你要把VBA放到前台,你需要写一个宏,然后给这个宏加一个按钮,它就跑到前台界面来了。如果你不会写程序,可以录制一个宏,然后学习一下。虽然有一定难度,但是会让你受益非浅。

TA的精华主题

TA的得分主题

发表于 2021-9-1 11:01 | 显示全部楼层
在任意一个空单元格中打入16个空格,复制后,
就可以在想要增加TAB制表符的位置黏贴,让排版更加好看了;
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-19 16:12 , Processed in 0.027783 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表