2012-01-19 11 views
6

बनाएं मैं रेंज A1:A400 से कॉमा सीमांकित स्ट्रिंग बनाना चाहता हूं।एक कॉमा डिलीमिटेड स्ट्रिंग

ऐसा करने का सबसे अच्छा तरीका क्या है? क्या मुझे For लूप का उपयोग करना चाहिए?

+0

आप चिप पियर्सन द्वारा बनाई StringConcat समारोह का उपयोग कर सकते के लिए बाहर देखते हैं। कृपया नीचे दिए गए लिंक को देखें :) ** विषय: स्ट्रिंग कंसटेनेशन ** ** लिंक **: [http://www.cpearson.com/Excel/StringConcatenation.aspx ](http://www.cpearson.com/Excel /StringConcatenation.aspx) –

उत्तर

16

laziest तरीका

s = join(Application.WorksheetFunction.Transpose([a1:a400]), ",") 

यह काम करता है क्योंकि एक multicell रेंज के .Value संपत्ति एक 2D सरणी देता है, और Join -1 डी सरणी की उम्मीद है, और Transpose इसलिए जब यह एक 2 डी का पता लगाता है, भी मददगार बनने की कोशिश कर रहा है सरणी केवल एक कॉलम के साथ, यह इसे 1 डी सरणी में परिवर्तित करता है।

उत्पादन में यह कम से कम एक छोटा सा कम आलसी विकल्प का उपयोग करने की सलाह दी है,

s = join(Application.WorksheetFunction.Transpose(Worksheets(someIndex).Range("A1:A400").Value), ",") 

अन्यथा सक्रिय पत्रक हमेशा इस्तेमाल किया जाएगा।

+4

यह तीनों भ्रमित व्यवहारों की एक सुंदर संक्षिप्त व्याख्या है जिसे मैंने हमेशा अर्ध-समझ लिया है। अब मैं लगभग तीन-चौथाई तक हूं। –

+0

+1, मेरे लिए कुछ भी साफ़ कर दिया। –

+0

@GSerg मैं रेंज ए 1 के लिए Z1 के लिए एक ही स्ट्रिंग कैसे बनाऊंगा? – user793468

1

आप चिप पियरसन द्वारा बनाए गए स्ट्रिंगकोनकैट फ़ंक्शन का उपयोग कर सकते हैं। मामले में http://www.cpearson.com/Excel/StringConcatenation.aspx

उद्धरण लिंक से लिंक कभी मर जाता है

यह पृष्ठ: स्ट्रिंग कड़ी

लिंक: कृपया नीचे दिए गए लिंक :)

विषय को देखने के एक वीबीए फ़ंक्शन का वर्णन करता है जिसका उपयोग आप सरणी सूत्र में स्ट्रिंग मानों को संयोजित करने के लिए कर सकते हैं।

StringConcat समारोह

आदेश CONCATENATE फ़ंक्शन के इन कमियों को दूर करने के अलावा, यह हमारे अपने VBA कि CONCATENATE की समस्याओं का समाधान होगा में लिखा समारोह का निर्माण करने के लिए आवश्यक है। इस पृष्ठ का शेष स्ट्रिंगकोनकैट नामक ऐसे फ़ंक्शन का वर्णन करता है। यह कार्य CONCATENATE की सभी कमियों पर विजय प्राप्त करता है। इसका उपयोग अलग-अलग स्ट्रिंग मानों को जोड़ना, मूल्यों को एक या अधिक वर्कशीट श्रेणियां, शाब्दिक सरणी, और सरणी सूत्र ऑपरेशन के परिणाम के लिए उपयोग किया जा सकता है। (स्ट्रिंग, ParamArray args() के रूप में सितम्बर)

समारोह StringConcat स्ट्रिंग

के रूप में सितम्बर पैरामीटर एक चरित्र या अक्षर हैं जो अलग तार श्रेणीबद्ध किया जा रहा है:

StringConcat के समारोह घोषणा इस प्रकार है। यह 0 या अधिक वर्ण हो सकता है। सितंबर पैरामीटर आवश्यक है। यदि आप परिणाम स्ट्रिंग में कोई विभाजक नहीं चाहते हैं, तो सितंबर के मान के लिए खाली स्ट्रिंग का उपयोग करें। प्रत्येक स्ट्रिंग को समेकित होने के बीच सितंबर मान दिखाई देता है, लेकिन परिणाम स्ट्रिंग की शुरुआत या अंत में दिखाई नहीं देता है। ParamArray Args पैरामीटर एक श्रृंखला मान को संयोजित करने के लिए है। ParamArray में प्रत्येक तत्व निम्न में से कोई भी हो सकता है:

"ए" जैसे एक शाब्दिक स्ट्रिंग, या तो पते या श्रेणी नाम द्वारा निर्दिष्ट कक्षों की एक श्रृंखला। जब दो आयामी सीमा के तत्वों को संयोजित किया जाता है, तो समावेशन का क्रम एक पंक्ति में होता है और फिर अगली पंक्ति तक होता है। एक शाब्दिक सरणी।उदाहरण के लिए, { 'ए', 'बी', 'सी'} या {; 'बी', 'ए' 'सी'} के लिए

समारोह

Function StringConcat(Sep As String, ParamArray Args()) As Variant 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' StringConcat 
' By Chip Pearson, [email protected], www.cpearson.com 
'     www.cpearson.com/Excel/stringconcatenation.aspx 
' This function concatenates all the elements in the Args array, 
' delimited by the Sep character, into a single string. This function 
' can be used in an array formula. There is a VBA imposed limit that 
' a string in a passed in array (e.g., calling this function from 
' an array formula in a worksheet cell) must be less than 256 characters. 
' See the comments at STRING TOO LONG HANDLING for details. 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Dim S As String 
Dim N As Long 
Dim M As Long 
Dim R As Range 
Dim NumDims As Long 
Dim LB As Long 
Dim IsArrayAlloc As Boolean 

''''''''''''''''''''''''''''''''''''''''''' 
' If no parameters were passed in, return 
' vbNullString. 
''''''''''''''''''''''''''''''''''''''''''' 
If UBound(Args) - LBound(Args) + 1 = 0 Then 
    StringConcat = vbNullString 
    Exit Function 
End If 

For N = LBound(Args) To UBound(Args) 
    '''''''''''''''''''''''''''''''''''''''''''''''' 
    ' Loop through the Args 
    '''''''''''''''''''''''''''''''''''''''''''''''' 
    If IsObject(Args(N)) = True Then 
     ''''''''''''''''''''''''''''''''''''' 
     ' OBJECT 
     ' If we have an object, ensure it 
     ' it a Range. The Range object 
     ' is the only type of object we'll 
     ' work with. Anything else causes 
     ' a #VALUE error. 
     '''''''''''''''''''''''''''''''''''' 
     If TypeOf Args(N) Is Excel.Range Then 
      ''''''''''''''''''''''''''''''''''''''''' 
      ' If it is a Range, loop through the 
      ' cells and create append the elements 
      ' to the string S. 
      ''''''''''''''''''''''''''''''''''''''''' 
      For Each R In Args(N).Cells 
       If Len(R.Text) > 0 Then 
        S = S & R.Text & Sep 
       End If 
      Next R 
     Else 
      ''''''''''''''''''''''''''''''''' 
      ' Unsupported object type. Return 
      ' a #VALUE error. 
      ''''''''''''''''''''''''''''''''' 
      StringConcat = CVErr(xlErrValue) 
      Exit Function 
     End If 

    ElseIf IsArray(Args(N)) = True Then 
     ''''''''''''''''''''''''''''''''''''' 
     ' ARRAY 
     ' If Args(N) is an array, ensure it 
     ' is an allocated array. 
     ''''''''''''''''''''''''''''''''''''' 
     IsArrayAlloc = (Not IsError(LBound(Args(N))) And _ 
      (LBound(Args(N)) <= UBound(Args(N)))) 
     If IsArrayAlloc = True Then 
      '''''''''''''''''''''''''''''''''''' 
      ' The array is allocated. Determine 
      ' the number of dimensions of the 
      ' array. 
      ''''''''''''''''''''''''''''''''''''' 
      NumDims = 1 
      On Error Resume Next 
      Err.Clear 
      NumDims = 1 
      Do Until Err.Number <> 0 
       LB = LBound(Args(N), NumDims) 
       If Err.Number = 0 Then 
        NumDims = NumDims + 1 
       Else 
        NumDims = NumDims - 1 
       End If 
      Loop 
      On Error GoTo 0 
      Err.Clear 
      '''''''''''''''''''''''''''''''''' 
      ' The array must have either 
      ' one or two dimensions. Greater 
      ' that two caues a #VALUE error. 
      '''''''''''''''''''''''''''''''''' 
      If NumDims > 2 Then 
       StringConcat = CVErr(xlErrValue) 
       Exit Function 
      End If 
      If NumDims = 1 Then 
       For M = LBound(Args(N)) To UBound(Args(N)) 
        If Args(N)(M) <> vbNullString Then 
         S = S & Args(N)(M) & Sep 
        End If 
       Next M 

      Else 
       '''''''''''''''''''''''''''''''''''''''''''''''' 
       ' STRING TOO LONG HANDLING 
       ' Here, the error handler must be set to either 
       ' On Error GoTo ContinueLoop 
       ' or 
       ' On Error GoTo ErrH 
       ' If you use ErrH, then any error, including 
       ' a string too long error, will cause the function 
       ' to return #VALUE and quit. If you use ContinueLoop, 
       ' the problematic value is ignored and not included 
       ' in the result, and the result is the concatenation 
       ' of all non-error values in the input. This code is 
       ' used in the case that an input string is longer than 
       ' 255 characters. 
       '''''''''''''''''''''''''''''''''''''''''''''''' 
       On Error GoTo ContinueLoop 
       'On Error GoTo ErrH 
       Err.Clear 
       For M = LBound(Args(N), 1) To UBound(Args(N), 1) 
        If Args(N)(M, 1) <> vbNullString Then 
         S = S & Args(N)(M, 1) & Sep 
        End If 
       Next M 
       Err.Clear 
       M = LBound(Args(N), 2) 
       If Err.Number = 0 Then 
        For M = LBound(Args(N), 2) To UBound(Args(N), 2) 
         If Args(N)(M, 2) <> vbNullString Then 
          S = S & Args(N)(M, 2) & Sep 
         End If 
        Next M 
       End If 
       On Error GoTo ErrH: 
      End If 
     Else 
      If Args(N) <> vbNullString Then 
       S = S & Args(N) & Sep 
      End If 
     End If 
     Else 
     On Error Resume Next 
     If Args(N) <> vbNullString Then 
      S = S & Args(N) & Sep 
     End If 
     On Error GoTo 0 
    End If 
ContinueLoop: 
Next N 

''''''''''''''''''''''''''''' 
' Remove the trailing Sep 
''''''''''''''''''''''''''''' 
If Len(Sep) > 0 Then 
    If Len(S) > 0 Then 
     S = Left(S, Len(S) - Len(Sep)) 
    End If 
End If 

StringConcat = S 
''''''''''''''''''''''''''''' 
' Success. Get out. 
''''''''''''''''''''''''''''' 
Exit Function 
ErrH: 
''''''''''''''''''''''''''''' 
' Error. Return #VALUE 
''''''''''''''''''''''''''''' 
StringConcat = CVErr(xlErrValue) 
End Function 
+1

मैं चिप पियरसन द्वारा लिखे गए किसी भी कोड की आलोचना करने में अनिच्छुक हूं - वह वीबीए और एक्सेल विकास की कला का एक स्वीकृत मास्टर है - लेकिन ऐसा नहीं है कि आप वीबीए में स्ट्रिंग कॉन्सटेनेशन कैसे करते हैं। बुनियादी तकनीक आवंटन और समामेलन से बचने के लिए है (यहां क्यों है: http://www.aivosto.com/vbtips/stringopt2.html#huge) - मैं इसमें शामिल होने, विभाजित करने और प्रतिस्थापित करने का उपयोग करता हूं - और अधिक उन्नत तकनीकें सूचीबद्ध हैं इस वेब आलेख के भाग I, II और II: http://www.aivosto.com/vbtips/stringopt3.html –

+1

यह भी ... कि Concatenate फ़ंक्शन 255 से अधिक वर्ण वाले कक्षों से डेटा पढ़ने पर परिचित सीमाओं से बाधित है । 2-आयामी 'जॉइन' फ़ंक्शन के साथ नीचे कोड नमूना देखें। –

4

मैं सम्मान दिया है @ GSerg के अपने प्रश्न के निश्चित उत्तर के रूप में जवाब दें।

पूर्णता के लिए - और अन्य उत्तर में कुछ सीमाएं पता करने के लिए - मैं सुझाव है कि आप एक 'शामिल' समारोह है कि 2-आयामी सरणी का समर्थन करता है का उपयोग करें:

 
s = Join2d(Worksheets(someIndex).Range("A1:A400").Value) 

यहां मुद्दा यह है कि है एक सीमा की मूल्य संपत्ति (यह एक एकल सेल नहीं है) हमेशा एक 2-आयामी सरणी है।

ध्यान दें कि Join2d फ़ंक्शन में पंक्ति डिलीमीटर केवल तभी मौजूद होता है जब पंक्तियां (बहुवचन) को सीमित करने के लिए होते हैं: आप इसे सिंगल-पंक्ति श्रेणी से संयोजित स्ट्रिंग में नहीं देख पाएंगे।

Join2d: एक 2-आयामी अनुकूलित स्ट्रिंग से निपटने

कोडिंग नोटों के साथ VBA में समारोह में शामिल हों:

  1. यह Join समारोह है कि सबसे अधिक प्रभावित करता है 255-चार सीमा से नहीं तो कष्ट नहीं होता है (सभी) एक्सेल में देशी कॉन्सटेनेट कार्यों का, और रेंज। ऊपर दिए गए वॉल्यूम कोड नमूने डेटा में, लंबे तारों वाली कोशिकाओं से पूरी तरह से पास हो जाएंगे।
  2. यह बहुत अनुकूलित है: हम जितना संभव हो सके स्ट्रिंग-कॉन्सटेनेशन का उपयोग करते हैं, क्योंकि देशी वीबीए स्ट्रिंग-कॉन्सटेनेशन धीमे होते हैं और लंबे स्ट्रिंग के रूप में धीरे-धीरे धीमे हो जाते हैं।

    Split2d:

 
    Public Function Join2d(ByRef InputArray As Variant, _ 
          Optional RowDelimiter As String = vbCr, _ 
          Optional FieldDelimiter = vbTab,_ 
          Optional SkipBlankRows As Boolean = False) As String

' Join up a 2-dimensional array into a string. Works like VBA.Strings.Join, for a 2-dimensional array. 
' Note that the default delimiters are those inserted into the string returned by ADODB.Recordset.GetString 
On Error Resume Next 

' Coding note: we're not doing any string-handling in VBA.Strings - allocating, deallocating and (especially!) concatenating are SLOW. 
' We're using the VBA Join & Split functions ONLY. The VBA Join, Split, & Replace functions are linked directly to fast (by VBA standards) 
' functions in the native Windows code. Feel free to optimise further by declaring and using the Kernel string functions if you want to. 

' **** THIS CODE IS IN THE PUBLIC DOMAIN **** Nigel Heffernan Excellerando.Blogspot.com 

Dim i As Long 
Dim j As Long 
Dim i_lBound As Long 
Dim i_uBound As Long 
Dim j_lBound As Long 
Dim j_uBound As Long 
Dim arrTemp1() As String 
Dim arrTemp2() As String 
Dim strBlankRow As String 

i_lBound = LBound(InputArray, 1) 
i_uBound = UBound(InputArray, 1) 
j_lBound = LBound(InputArray, 2) 
j_uBound = UBound(InputArray, 2) 

ReDim arrTemp1(i_lBound To i_uBound) 
ReDim arrTemp2(j_lBound To j_uBound) 

For i = i_lBound To i_uBound 

    For j = j_lBound To j_uBound 
     arrTemp2(j) = InputArray(i, j) 
    Next j 
    arrTemp1(i) = Join(arrTemp2, FieldDelimiter) 
Next i 

If SkipBlankRows Then 
    If Len(FieldDelimiter) = 1 Then 
     strBlankRow = String(j_uBound - j_lBound, FieldDelimiter) 
    Else 
     For j = j_lBound To j_uBound 
      strBlankRow = strBlankRow & FieldDelimiter 
     Next j 
    End If 

    Join2d = Replace(Join(arrTemp1, RowDelimiter), strBlankRow & RowDelimiter, "") 
    i = Len(strBlankRow & RowDelimiter) 

    If Left(Join2d, i) = strBlankRow & RowDelimiter Then 
     Mid$(Join2d, 1, i) = "" 
    End If 
Else 
    Join2d = Join(arrTemp1, RowDelimiter) 
End If 
Erase arrTemp1 
End Function 

पूर्णता के लिए, यहाँ इसी 2-डी विभाजित करें फ़ंक्शन के साथ VBA में एक 2-आयामी विभाजित करें फ़ंक्शन अनुकूलित स्ट्रिंग से निपटने

Public Function Split2d(ByRef strInput As String, _ 
         Optional RowDelimiter As String = vbCr, _ 
         Optional FieldDelimiter = vbTab, _ 
         Optional CoerceLowerBound As Long = 0) As Variant 

' Split up a string into a 2-dimensional array. Works like VBA.Strings.Split, for a 2-dimensional array. 
' Check your lower bounds on return: never assume that any array in VBA is zero-based, even if you've set Option Base 0 
' If in doubt, coerce the lower bounds to 0 or 1 by setting CoerceLowerBound 
' Note that the default delimiters are those inserted into the string returned by ADODB.Recordset.GetString 
On Error Resume Next 

' Coding note: we're not doing any string-handling in VBA.Strings - allocating, deallocating and (especially!) concatenating are SLOW. 
' We're using the VBA Join & Split functions ONLY. The VBA Join, Split, & Replace functions are linked directly to fast (by VBA standards) 
' functions in the native Windows code. Feel free to optimise further by declaring and using the Kernel string functions if you want to. 


' **** THIS CODE IS IN THE PUBLIC DOMAIN **** Nigel Heffernan Excellerando.Blogspot.com 

Dim i As Long 
Dim j As Long 
Dim i_n As Long 
Dim j_n As Long 
Dim i_lBound As Long 
Dim i_uBound As Long 
Dim j_lBound As Long 
Dim j_uBound As Long 
Dim arrTemp1 As Variant 
Dim arrTemp2 As Variant 

arrTemp1 = Split(strInput, RowDelimiter) 

i_lBound = LBound(arrTemp1) 
i_uBound = UBound(arrTemp1) 

If VBA.LenB(arrTemp1(i_uBound)) <= 0 Then ' clip out empty last row: common artifact data loaded from files with a terminating row delimiter 
    i_uBound = i_uBound - 1 
End If 

i = i_lBound 
arrTemp2 = Split(arrTemp1(i), FieldDelimiter) 

j_lBound = LBound(arrTemp2) 
j_uBound = UBound(arrTemp2) 

If VBA.LenB(arrTemp2(j_uBound)) <= 0 Then ' ! potential error: first row with an empty last field... 
    j_uBound = j_uBound - 1 
End If 

i_n = CoerceLowerBound - i_lBound 
j_n = CoerceLowerBound - j_lBound 

ReDim arrData(i_lBound + i_n To i_uBound + i_n, j_lBound + j_n To j_uBound + j_n) 

' As we've got the first row already... populate it here, and start the main loop from lbound+1 

For j = j_lBound To j_uBound 
    arrData(i_lBound + i_n, j + j_n) = arrTemp2(j) 
Next j 

For i = i_lBound + 1 To i_uBound Step 1 
    arrTemp2 = Split(arrTemp1(i), FieldDelimiter) 
    For j = j_lBound To j_uBound Step 1  
     arrData(i + i_n, j + j_n) = arrTemp2(j)  
    Next j  
    Erase arrTemp2 
Next i 

Erase arrTemp1 

Application.StatusBar = False 

Split2d = arrData 
End Function 

साझा करें और आनंद लें ... और कोड में अवांछित पंक्ति विराम, आपके ब्राउज़र (या StackOverflow के सहायक स्वरूपण कार्यों के द्वारा) द्वारा डाला

+1

+1 ग्रेट पोस्ट! यहां तक ​​कि बाईं ओर 'मिड $ 'और' लेनबी 'में भी चुपके! एकमात्र बहुत मामूली नाइटपिक सुझाव '' '' की बजाय 'VbNullstring' है .... इसलिए मुझे लगता है कि आप निगेल एच हैं जो कभी-कभी डिक्स ब्लॉग पर पोस्ट करते हैं। मैं आपके काम का आनंद लेता हूं – brettdj

+0

... आपने सभी कोड व्हाइटस्पेस को वापस जोड़ा है। – brettdj

+0

क्या यह मुझे है या क्या यह असंभव है कि vb संपादक में कॉपी और पेस्ट करना सही है? ठीक है [revision3 काम करता है] (https://stackoverflow.com/revisions/12054533/3) प्रतिलिपि बनाने और पेस्ट करने के लिए – Vijay

संबंधित मुद्दे