Want to copy a string of text and a range of cells

Tonkytonk

New Member
Joined
Apr 9, 2020
Messages
7
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I want to copy a string of text with a range of cells.

My old Macro worked until I have added my new code at the end. I have tried multiple different ways and none have worked.

VBA Code:
Sub Macro2()
'


Dim MyString As String
Dim myrange As Range


MyString = "The following student need to complete a detention. Those that are highlighted, further consequences will apply if not completed today."

    ActiveWorkbook.Worksheets("Detention").ListObjects("Table5").Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Detention").ListObjects("Table5").Sort.SortFields. _
        Add2 Key:=Range("Table5[_studentform]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Detention").ListObjects("Table5").Sort.SortFields. _
        Add2 Key:=Range("Table5[_StatusCaption]"), SortOn:=xlSortOnValues, Order _
        :=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Detention").ListObjects("Table5").Sort.SortFields. _
        Add2 Key:=Range("Table5[_fullname]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Detention").ListObjects("Table5").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("F2:S2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

***New code***
   Set myrange = Selection

    Dim mytext As DataObject
    mytext = MyString & vbNewLine & myrange
    mytext.PutInClipboard
    
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top