0

I m using excel to calculate some values then inserting the values in AutoCAD drawing block

by writing scripting lines which is more than 2500 lines

Then I need to copy all lines once and paste in AutoCAD once.

so I tried to combine the lines by concat or text join which exceed the limit for one cell

is there is away to exceed or to combine the values in more than one cell.

John Coleman
  • 51,337
  • 7
  • 54
  • 119

1 Answers1

0

This sounds like an XY problem. There is probably a better way, using the scripting capabilities of Autocad directly, to do whatever it is that you are trying to do. Any solution which works by assembling text in one application to copy/paste it into another application is clunky at best.

Nevertheless, it is certainly possible to join multiple cells into a single string which is then copied to the clipboard. The following code does so.

Function Clipboard$(Optional s$)
    'Code from Excel Hero
    'https://stackoverflow.com/a/60896244/4996248
    
    Dim v: v = s  'Cast to variant for 64-bit VBA support
    With CreateObject("htmlfile")
    With .parentWindow.clipboardData
        Select Case True
            Case Len(s): .setData "text", v
            Case Else:   Clipboard = .GetData("text")
        End Select
    End With
    End With
End Function

Sub CopyRangeToClip(R As Range, Optional delimiter As String = "")
    Dim i As Long, n As Long, A As Variant, cell As Range
    n = R.Cells.Count
    ReDim A(0 To n - 1)
    For Each cell In R.Cells
        A(i) = cell.Value
        i = i + 1
    Next cell
    
    Clipboard Join(A, delimiter)
End Sub

You can create a simple sub (which is e.g. assigned to a keyboard shortcut) which applies this last sub to the current selection:

Sub CopyAndJoinSelection()
    CopyRangeToClip Selection
End Sub

To show that this approach can copy large strings split into many cells, the following test sub copies a string of length 45,000 to the clipboad:

Sub Test()
    Dim i As Long
    For i = 1 To 9
        Cells(i, 1).Value = String(5000, Trim(Str(i)))
    Next i
    CopyRangeToClip Range("A1:A9")
End Sub

Yo give credit where credit is due, note that the above code uses this excellent answer for the function which copies the text to the clipboard.

John Coleman
  • 51,337
  • 7
  • 54
  • 119