I am trying to write a macro to copy data from a set of cells and paste it in a new worksheet.
I want to stop the selection where the last cell in column A that equals "HOURS TOTAL", which will be dynamic based on the data between A9 and the last row/cell where "HOURS TOTAL" is. I've tried four different methods and none of them produce the correct results.
Sub Copy_Data()
'
'Copy_Data Macro
'
'
Dim lastCell As String
Sheets("OPSEQB").Select
Range("A9", Range("P9").End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveSheet.Paste
End Sub
Sub Copy_Data2()
Dim copyRange As String
Startrow = A9
LastRow = 11
Let copyRange = "A" & Startrow & ":" & "D" & LastRow
Range(copyRange).Select
End Sub
Sub Copy_Data3()
'
'Copy_Data Macro
'
'
Dim LastRow As String
LastRow = Range.SpecialCells(xlCellTypeLastCell, "HOURS TOTAL").Row
Sheets("OPSEQB").Select
Range("A9", Range("P9").End(xlToRight)).Select
Range(Selection, Selection.End(LastRow)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveSheet.Paste
End Sub
Sub Copy_Data4()
'Best used when you want to include all data stored on the spreadsheet
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set sht = Worksheets("OPSEQB")
Set StartCell = Range("A9")
'Refresh UsedRange
Worksheets("OPSEQB").UsedRange
'Find Last Row and Column
LastRow = StartCell.SpecialCells(xlCellTypeLastCell, "HOURS TOTAL").Row
LastColumn = StartCell.SpecialCells(xlCellTypeLastCell).Column
'Select and copy Range
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
Selection.Copy
'Add a sheet and paste the range
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveSheet.Paste
End Sub