22

MS Excel 2010 or 2007 or any version for that matter when you copy a cell(not selecting text from the cell) and pasting to any editor adds a line break and sends the cursor to next line. Its annoying to move up the cursor back to last line.

It doesnt look much work to get the cursor back to last line but if you have to keep doing this a lot of times as a programmer i feel really bad. Any suggestions if there is a setting in excel to stop this?

I tried looking online didnt find any thing and also looked up the options of excel, didnt find anything.

4

3 回答 3

16

我在翻译公司的 Excel 电子表格和我们的代码之间为我们的应用程序复制/粘贴了一个大的翻译文件。每次都必须删除换行符让我陷入困境。

最后,我复制了整张表格并将其粘贴到新的 Google 文档电子表格中。然后我用它来满足我的复制需求,因为它不会像 Excel 那样在副本上添加换行符。

希望有帮助!

于 2014-04-17T08:41:51.193 回答
15

You can create your own copy routine, that does not store the whole cell, but only its value in the clipboard:

Sub OwnCopy()
    Dim DataObj As New MSForms.DataObject
    
    DataObj.SetText ActiveCell.Value 'depending what you want, you could also use .Formula here
    DataObj.PutInClipboard
End Sub

To be able to compile the macro, you need to include "Microsoft Forms 2.0 Object Library" as a reference (in the Visual Basic editor, go to Tools->References and browse %windir%\system32 for FM20.dll1).

您现在可以使用默认的运行宏对话框将此宏分配给另一个快捷方式(例如Ctrl- ) ShiftC甚至可以通过执行来覆盖Ctrl。但是,我不建议覆盖-因为除了粘贴到编辑器之外,您很可能希望使用通常与它一起复制的所有其他信息。cApplication.OnKey "^c", "OwnCopy"Ctrlc

要使其永久化,只需将宏存储在您的个人工作簿中。

如果您想要一个更复杂的复制例程,它还可以处理多个单元格/选择区域,请使用以下代码:

Sub OwnCopy2()
    Dim DataObj As New MSForms.DataObject
    Dim lngRow As Long
    Dim intCol As Integer
    Dim c As Range
    Dim strClip As String
    
    Const cStrNextCol As String = vbTab
    Const cStrNextRow As String = vbCrLf
    
    With Selection
        If Not TypeOf Selection Is Range Then
            On Error Resume Next
            .Copy
            Exit Sub
        End If
            
        If .Areas.Count = 1 Then
            For lngRow = 1 To .Rows.Count
                For intCol = 1 To .Columns.Count
                    strClip = strClip & _
                        Selection(lngRow, intCol).Value & cStrNextCol
                Next intCol
                strClip = Left(strClip, Len(strClip) - Len(cStrNextCol)) _
                    & cStrNextRow
            Next lngRow
        Else
            For Each c In .Cells
                strClip = strClip & c.Value & vbCrLf
            Next c
        End If
        strClip = Left(strClip, Len(strClip) - Len(cStrNextRow))
        DataObj.SetText strClip
        DataObj.PutInClipboard
    End With
End Sub

1实际上,此文件存在于%ProgramFiles%\Microsoft Office\root\vfs\System- Office 使用一些虚拟化技巧使其出现在它所在的位置。

于 2013-02-04T21:35:36.863 回答
1

我同意这很烦人,但不确定有什么可做的。

Excel 自动格式化单元格...制表符分隔的列,行分隔的行(单个单元格是 1 列的行)。

验证:在 .NET 中,Clipboard.GetText() 显示格式已在剪贴板中

这里有一些 IronPython 来说明:

# Copy some cells in Excel, then run the following
import sys, clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Clipboard
Clipboard.GetText()
于 2013-02-04T21:37:52.807 回答