Advertisement
AlanElston

YassersExtraCodes

Dec 10th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ( XL2020alsm.xlsb https://app.box.com/s/26frr0zzc93q6zsraktove3qypqj714p )
  2.  
  3.  
  4.  
  5.  
  6. Option Explicit '   http://eileenslounge.com/viewtopic.php?f=30&t=31395#p242964    ( XL2020alsm.xlsb https://app.box.com/s/26frr0zzc93q6zsraktove3qypqj714p )
  7. Private Sub Publics_Probably_Let_RngAsString__() ' Input of range to Private Properties storage
  8. Rem 0 test data range is selection. Select a range before running this code
  9. Dim rngSel As Range: Set rngSel = Selection ' selected range for later reference
  10. Rem 1 Copy range to clipbored
  11.  rngSel.Copy
  12. Rem 2 put data currently in clipboard into a string
  13. Dim objDataObject As Object ' DataObject  ' This will be for an an Object from the class MS Forms. This will be a Data Object of what we "send" to the Clipboard. But it is a DataObject. It has the Methods I need to send to and get text to the Clipboard. ' http://excelmatters.com/2013/09/23/vba-references-and-early-binding-vs-late-binding/     http://www.excelfox.com/forum/showthread.php/2240-VBA-referring-to-external-shared-Libraries-1)-Early-1-5)-Laterly-Early-and-2)-Late-Binding-Techniques
  14. Set objDataObject = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") ' http://excelmatters.com/2013/10/04/late-bound-msforms-dataobject/
  15. objDataObject.GetFromClipboard ' The data object has the long text string from the clipboard at this point
  16. 'rngSel.ClearContents ' we can't do this here, not sure why??
  17. Dim strIn As String: Let strIn = objDataObject.GetText() ' The string variable, strIn, is given the long string
  18. rngSel.ClearContents ' do this now. (If we did it before, the contents of the clipboard are typically emptied, so that would be poo. I don't know why the clipboard needs to be full still fir the last code line??
  19. Rem 3 manipulate string to substitute vbTab with arbritrary character combination - in next code this will be replaced. We do this because the vbTab is lost when pasting into a code module
  20.  Let strIn = Replace(strIn, vbTab, " | ", 1, -1, vbBinaryCompare) ' replacing( in the string , replace vbTab  , with " | " , start at first position ,  replace all occurances , look for an excact case sensitive match as this is qiucker if we don't need to be case insensitive as with option vbTextCompare )
  21. Let strIn = "'_-" & Replace(strIn, vbLf, vbLf & "'_-", 1, -1, vbBinaryCompare) ' add some comment bits to prevent red error in code window
  22. Rem 4 add range data
  23.  Let strIn = "'_-Worksheets(""" & rngSel.Parent.Name & """).Range(""" & rngSel.Address & """)" & vbCrLf & strIn ' Add an extra first header line to indicate the worksheet and range used
  24. On Error Resume Next ' I am not quite sure why this is needed
  25. ThisWorkbook.VBProject.VBComponents("Tabelle1").CodeModule.AddFromString strIn ' As far as i know, this adds from the start of the module.
  26. Set objDataObject = Nothing ' This probably is not needed.                                                      It upsets Kyle when i do it, but he can take it :-)
  27. End Sub
  28.  
  29. Private Sub Publics_Probably_Get_Rng__AsString() ' Output of range from Private Properties Storage
  30. Rem 2 get string data form code module Private properties storage
  31. Dim strVonCodMod As String
  32. '2a Range infomation first line
  33. Dim Ws As Worksheet, Rng As Range ' These will be used for the range identification infomation which the next code line gets from the first line in the code module used for the
  34. Let strVonCodMod = ThisWorkbook.VBProject.VBComponents("Tabelle1").CodeModule.Lines(Startline:=1, Count:=1) ' First line has the
  35. Let strVonCodMod = Replace(Replace(Replace(strVonCodMod, "'_-Worksheets(""", ""), """).Range(""", " "), """)", "") ' we want to reduce and change like  "Worksheets("Sht").Range("A1")"  to   "Sht A1"    so that we can use split to get the Sheet name and the range address   strVonCodMod = Replace(strVonCodMod, "'_-Worksheets(""", "") :  strVonCodMod = Replace(strVonCodMod, """).Range(""", " ") :  strVonCodMod = Replace(strVonCodMod, """)", "")
  36. Set Ws = Worksheets(Split(strVonCodMod)(0)): Set Rng = Ws.Range(Split(strVonCodMod)(1)) ' The returned array from spliting by the space , " " ,  will have first element (indicie(0)) of like  "Sht"  and the second element (indicie(1))  of like  "A1"
  37. '2b get range data
  38. Let strVonCodMod = ThisWorkbook.VBProject.VBComponents("Tabelle1").CodeModule.Lines(Startline:=2, Count:=Rng.Rows.Count + 1) ' We need rows count+1 because there seems to be a last  & vbCr & vbLf    http://eileenslounge.com/viewtopic.php?f=30&t=31395#p242941
  39. Let strVonCodMod = Replace(strVonCodMod, "'_-", "", 1, -1, vbBinaryCompare) ' remove the '_- Comment bits
  40. Let strVonCodMod = Replace(strVonCodMod, " | ", vbTab, 1, -1, vbBinaryCompare) ' Replace the " | " with a carriage return
  41. Rem 3 Put the string into the clipboard
  42. Dim objDataObject As Object '
  43. Set objDataObject = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
  44.  objDataObject.SetText strVonCodMod
  45.  objDataObject.PutInClipboard
  46.  Set objDataObject = Nothing
  47. Rem 4 Output range data values to spreadsheet
  48.  Ws.Paste Destination:=Rng
  49. Rem 5
  50.  On Error Resume Next
  51.  ThisWorkbook.VBProject.VBComponents("Tabelle1").CodeModule.DeleteLines Startline:=1, Count:=Rng.Rows.Count + 1 + 1 ' remove the first header row and all data and the extra last row caused by the extra  & vbCr & vbLf
  52. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement