Copy excel range to clipboard

Leo_Polla_Psemata

Registered User.
Local time
Today, 09:26
Joined
Mar 24, 2014
Messages
364
Hi, i want to make a macro in excel, copy range to clipboard
Code:
Sub Copy_Range_To_Clipboard()

Range("A2:D10").Copy

End Sub
The problem is that the one excel might have just 10 lines while another 100.
How could I tell VBA to copy all cells with data?
 
Code:
' note WS is a Worksheet object
' used as late binding
Public Function fnLastRow(ByVal strColumn As String, Optional ByRef WS As Object) As Long
    If WS Is Nothing Then Set WS = ActiveSheet
    fnLastRow = WS.Cells(WS.Rows.count, ToColNum(strColumn, WS)).End(-4162).row
End Function

Public Function fnLastColumn(ByVal lngrow As Long, Optional ByRef WS As Object) As String
    If WS Is Nothing Then Set WS = ActiveSheet
    fnLastColumn = ToColLetter(WS.Cells(lngrow, WS.Columns.count).End(-4159).Column)
End Function


' https://www.extendoffice.com/documents/excel/3765-excel-convert-column-label-to-number.html
Public Function ToColNum&(ByVal ColName As String, Optional ByRef WS As Object)
    If WS Is Nothing Then Set WS = ActiveSheet
    ToColNum = WS.Range(ColName & 1).Column
End Function

' https://www.extendoffice.com/documents/excel/3765-excel-convert-column-label-to-number.html
Public Function ToColLetter$(ByVal ColNumber As Integer, Optional ByRef WS As Object)
    If WS Is Nothing Then Set WS = ActiveSheet
    ToColLetter = Split(WS.Cells(1, ColNumber).Address, "$")(1)
End Function

the code:
Code:
dim lastrow as long
lastrow = fnLastRow("A")
Range("A2:D" & lastrow).Copy
 

Users who are viewing this thread

Back
Top Bottom