2015-12-16 11 views
8

के साथ क्लिपबोर्ड पर एक्सेल क्लिपबोर्ड पर एक्सेल फ़ाइल की सामग्री कॉपी करना चाहते हैं, उसी कॉन्फ़िगरेशन और उपयोगकर्ता कॉन्फ़िगरेशन के बावजूद प्रारूप का उपयोग करना।मैक्रो दशमलव विभाजक

यहाँ मेरी मैक्रो है:

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

एक्सेल को स्थानीय रूप से स्वीकार्य प्रारूप के रूप में एक संख्या/सूत्र को पढ़ने का कोई तरीका नहीं है? मेरे सिर के शीर्ष को याद नहीं किया जा सकता है कि यह कैसे करें। मुख्य सवाल, हालांकि, क्लिपबोर्ड के बाद डेटा जा रहा है? यदि यह एक्सेल में चिपक जाता है ... क्या आप विभाजक को अनदेखा नहीं कर सकते हैं और विश्वास करते हैं कि उसी उपयोगकर्ता द्वारा एक्सेल की दो खुली खिड़कियां उसी स्थानीय प्रारूप सेटिंग्स का उपयोग करती हैं? शायद मैं गलत समझ रहा हूं कि आप ऐसा क्यों करना चाहते हैं। –

+1

डेटा एक अलग एप्लिकेशन में जाएगा। आवेदन वास्तव में परवाह नहीं करता है कि कौन से विभाजक उपयोग किए जाते हैं, जब तक वे लगातार होते हैं। (हर उपयोगकर्ता वही भेजता है)। लोकेल पास करना बोझिल होगा, क्योंकि सीटीआरएल + सी को मैक्रो से अलग होना होगा, सामग्री के अनुसार, न केवल प्रारूप के अनुसार। –

+0

वैसे आपका कोड मेरे लिए पूरी तरह से ठीक काम कर रहा है। परीक्षण के उद्देश्य के लिए, मैंने अपना कोड चलाने से पहले अपने एक्सेल में विभाजक बदल दिए हैं। – ManishChristian

उत्तर

1

समस्या समस्या का हल गलत पर इस सेटिंग

.UseSystemSeparators = True 

था।

+0

यह मेरा पहला सुझाव था (प्रश्न टिप्पणियों में), जब आपने इस पर टिप्पणी नहीं की तो मैंने सोचा कि यह आपके लिए काम नहीं करता है, इसलिए मैंने अपना जवाब पोस्ट किया जो एक और तरीका प्रदान करता है। – Fadi

+1

क्षमा करें फडी, मुझे याद आया। कृपया इसे एक उत्तर के रूप में रखें और मैं बक्षीस का पुरस्कार दूंगा। –

+0

Jakabfi, बिल्कुल कोई समस्या नहीं है, यह स्पष्ट है कि आपको मेरी टिप्पणी देखने से पहले यह जवाब मिला है, तो आप अन्य उपयोगकर्ता को यह जानने के लिए अपना उत्तर स्वीकार कर सकते हैं कि यह इस प्रश्न का सही उत्तर है। – Fadi

2

Cpearson Site कुछ संशोधन के साथ हम Numbers और Dates के लिए कस्टम स्वरूपों के साथ किसी भी श्रेणी कॉपी कर सकते हैं एक्सेल या सिस्टम सेटिंग बदलने की कोई आवश्यकता के साथ क्लिपबोर्ड से। इस मॉड्यूल को "माइक्रोसॉफ्ट फॉर्म 2.0 ऑब्जेक्ट लाइब्रेरी" के संदर्भ की आवश्यकता है, हम वर्कबुक में UserForm जोड़कर यह संदर्भ कर सकते हैं, फिर हम इसे हटा सकते हैं, (यदि पहले से ही वर्कबुक में UserForm है तो हम इस चरण को छोड़ सकते हैं)।

Option Explicit 
Option Compare Text 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' modClipboard 
' By Chip Pearson 
'  chip[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 
संबंधित मुद्दे