2015-12-16 21 views
8

我想將excel文件的內容複製到剪貼板,使用相同的分隔符和格式,無論用戶配置如何。Excel到帶有宏小數點分隔的剪貼板

這裏是我的宏:

Private Sub CommandButton1_Click() 

'save number separators 
Dim d, t, u 
d = Application.DecimalSeparator 
t = Application.ThousandsSeparator 
u = Application.UseSystemSeparators 

'set number separators 
With Application 
     .DecimalSeparator = "." 
     .ThousandsSeparator = "," 
     .UseSystemSeparators = True 
End With 

'create temporary copy 
ActiveSheet.Copy 

'set number format 
ActiveSheet.Range("H2:I150").NumberFormat = "0.0000000000" 

[...] 

'copy sheet to clipboard 
ActiveSheet.Range("A1:O150").Copy 

'disable messages (clipboard) 
Application.DisplayAlerts = False 

'close temporary copy 
ActiveWorkbook.Close SaveChanges:=False 

'reenable messages 
Application.DisplayAlerts = True 

'reset original separators 
With Application 
     .DecimalSeparator = d 
     .ThousandsSeparator = t 
     .UseSystemSeparators = u 
End With 

End Sub 

如果我不重置原始分離到了最後,一切工作正常,但是這是不能接受我。

如果我重置分隔符(如此代碼所示),那麼剪貼板的內容將具有用戶特定的分隔符,而不是我在開始時定義的分隔符。

有關如何解決此問題的任何想法?

+0

是不是有辦法讓Excel讀取數字/公式作爲本地可接受的格式?無法回想起我的頭頂如何做到這一點。但關鍵問題在於數據在剪貼板之後的位置?如果它粘貼到Excel中......你能不能忽略分隔符,並且相信同一用戶的兩個打開的Excel窗口會使用相同的本地格式設置?也許我誤解了你爲什麼要這樣做。 –

+1

數據將進入不同的應用程序。應用程序並不關心使用哪些分隔符,只要它們一致即可。 (每個用戶發送相同的)。傳遞語言環境會很麻煩,因爲CTRL + C必須與宏不同,而且內容明智,不僅格式明智。 –

+0

那麼你的代碼對我來說工作得很好。出於測試目的,我在運行代碼之前更改了Excel中的分隔符。 – ManishChristian

回答

1

問題是

.UseSystemSeparators = True 

此設置爲false,解決了這個問題。

+0

這是我第一次提出(在問題的評論中),當你沒有評論它時,我認爲它不適合你,所以我發佈了我的答案,提供了另一種方式。 – Fadi

+1

對不起Fadi,我錯過了。請把它作爲答案,我會獎勵賞金。 –

+0

Jakabfi,完全沒有問題,很明顯你在看到我的評論之前找到了答案,所以你可以接受你自己的答案來幫助其他用戶知道這是這個問題的正確答案。 – Fadi

2

Cpearson Site經過一些修改,我們可以將NumbersDates的自定義格式的任何範圍複製到剪貼板,無需更改Excel或系統設置。 該模塊需要參考「Microsoft Forms 2.0 Object Library」,我們可以通過將UserForm添加到Workbook中,然後我們可以刪除它(如果已經有工作簿中的任何UserForm,我們可以跳過這一步)來做這個參考。

Option Explicit 
Option Compare Text 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' modClipboard 
' By Chip Pearson 
'  [email protected] 
'  www.cpearson.com/Excel/Clipboard.aspx 
' Date: 15-December-2008 
' 
' This module contains functions for working with text string and 
' the Windows clipboard. 
' This module requires a reference to the "Microsoft Forms 2.0 Object Library". 
' 
' !!!!!!!!!!! 
' Note that in order to retrieve data from the clipboard that was placed 
' in the clipboard via a DataObject, that DataObject object must not be 
' set to Nothing or allowed to go out of scope after adding text to the 
' clipboard and before retrieving data from the clipboard. If the DataObject 
' is destroyed, the data cannot be retrieved from the clipboard. 
' !!!!!!!!!!! 
' 
' Functions In This Module 
' ------------------------- 
' PutInClipboard    Puts a text string in the clipboard. Supprts 
'        clipboard format identifiers. 
' GetFromClipboard   Retrieves whatever text is in the clipboard. 
'        Supports format identifiers. 
' RangeToClipboardString  Converts a Range object into a String that 
'        can then be put in the clipboard and pasted. 
' ArrayToClipboardString  Converts a 1 or 2 dimensional array into 
'        a String that can be put in the clipboard 
'        and pasted. 
' Private Support Functions 
' ------------------------- 
' ArrNumDimensions   Returns the number of dimensions in an array. 
'        Returns 0 if parameter is not an array or 
'        is an unallocated array. 
' IsArrayAllocated   Returns True if the parameter is an allocated 
'        array. Returns False under all other circumstances. 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Private DataObj As MSForms.DataObject 
Public Function PutInClipboard(RR As Range, Optional NmFo As String, Optional DtFo As String) As Boolean 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    ' RangeToClipboardString 
    ' This function changes the cells in RR to a String that can be put in the 
    ' Clipboard. It delimits columns with a vbTab character so that values 
    ' can be pasted in a row of cells. Each row of vbTab delimited strings are 
    ' delimited by vbNewLine characters to allow pasting accross multiple rows. 
    ' The values within a row are delimited by vbTab characters and each row 
    ' is separated by a vbNewLine character. For example, 
    ' T1 vbTab T2 vbTab T3 vbNewLine 
    ' U1 vbTab U2 vbTab U3 vbNewLine 
    ' V1 vtTab V2 vbTab V3 
    ' There is no vbTab after the last item in a row and there 
    ' is no vbNewLine after the last row. 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    Dim R As Long 
    Dim C As Long 
    Dim s As String 
    Dim S1 As String 
    For R = 1 To RR.Rows.Count 
     For C = 1 To RR.Columns.Count 
      If IsNumeric(RR(R, C).Value) And Not IsMissing(NmFo) Then 
      S1 = Format(RR(R, C).Value, NmFo) 
      ElseIf IsDate(RR(R, C).Value) And Not IsMissing(DtFo) Then 
      S1 = Format(RR(R, C).Value, DtFo) 
      End If 
      s = s & S1 & IIf(C < RR.Columns.Count, vbTab, vbNullString) 
     Next C 
     s = s & IIf(R < RR.Rows.Count, vbNewLine, vbNullString) 
    Next R 

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    ' PutInClipboard 
    ' This function puts the text string S in the Windows clipboard, using 
    ' FormatID if it is provided. 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

    On Error GoTo ErrH: 
    If DataObj Is Nothing Then 
     Set DataObj = New MSForms.DataObject 
    End If 

    DataObj.SetText s 
    DataObj.PutInClipboard 
    PutInClipboard = True 
    Exit Function 
ErrH: 
    PutInClipboard = False 
    Exit Function 
End Function 



' How to use this: 

Sub Test() 
Dim Rng As Range 
Set Rng = ActiveSheet.Range("H2:I150") ' change this to your range 

Call PutInClipboard(Rng, "##,#0.0000000000") ' change the formats as you need 
'or 
'Call PutInClipboard(Rng, "##,#0.0000000000", "m/dd/yyyy") 
End Sub 
相關問題