Turn Excel range into VBA string

前端 未结 7 1826
野性不改
野性不改 2020-11-30 11:51

I would like to turn values in given range into VBA string where original cell values are separated by any chosen column delimiter and row delimiter. Delimiters could be one

7条回答
  •  旧巷少年郎
    2020-11-30 12:14

    This solution will require either a reference to the Microsoft Forms 2.0 Object Library in your project or some other way of fetching the contents of the clipboard (like through an API call).

    Function TurnExcelRangeIntoVBAString(Optional cellDelimiter As String = ",", _
                                         Optional rowDelimiter As String = "@") _
             As String
    
        Dim rng As Range
        Set rng = ActiveSheet.UsedRange
        rng.Copy
    
        Dim clip As New MSForms.DataObject
        Dim txt As String
        clip.GetFromClipboard
        txt = clip.GetText()
        txt = Replace(Replace(txt, vbTab, cellDelimiter), vbCrLf, rowDelimiter)
    
        TurnExcelRangeIntoVBAString = txt
    End Function
    

提交回复
热议问题