Not really sure what your question is. Are you saying that the NUMBER of rows change? If so, then here's your answer. I'm making assumptions here that the first row is always row 1; so you'll need to modify that isn't the case. I'm also assuming that column 1 contains data for every row. You'll need to modify if that isn't the case either.
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'
iLastRow = iLastRowFilledInColumn(Me, 1) ' see my function below
stRange = "A1:C" & iLastRow
Range(stRange).Select
Selection.Copy
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Function iLastRowFilledInColumn(ws As Excel.Worksheet, iColumn As Long) As Long
Dim iLastRow As Long
iLastRow = ws.Cells(ws.Rows.Count, iColumn).End(xlUp).Row
'
' If the entire column is empty, Excel still returns 1 even though
' there is no data in row 1. Therefore, check to see if the
' cell is empty; if it is, return 0 instead of 1.
'
If iLastRow = 1& Then
If Trim(ws.Cells(iLastRow, iColumn)) = "" Then
iLastRow = 0&
End If
End If
This code worked perfectly. The previous post gave me errors. Thanks to both for the assistance. I will have another question soon. Similar to this one.
Microsoft Excel
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'
iLastRow = iLastRowFilledInColumn(Me,
stRange = "A1:C" & iLastRow
Range(stRange).Select
Selection.Copy
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Function iLastRowFilledInColumn(ws As Excel.Worksheet, iColumn As Long) As Long
Dim iLastRow As Long
iLastRow = ws.Cells(ws.Rows.Count, iColumn).End(xlUp).Row
'
' If the entire column is empty, Excel still returns 1 even though
' there is no data in row 1. Therefore, check to see if the
' cell is empty; if it is, return 0 instead of 1.
'
If iLastRow = 1& Then
If Trim(ws.Cells(iLastRow, iColumn)) = "" Then
iLastRow = 0&
End If
End If
iLastRowFilledInColumn = iLastRow
End Function