Private Sub CommandButton1_Click()
Dim i As Integer
Dim tblCurrent As Range
Dim lRow, lasrRow As Long
Dim new_Date As Date
Dim old_Date As Date
Dim FoundCell As Range
Dim wb As Workbook
Set wb = ActiveWorkbook
Sheet1.Activate
'Show InputBox and Collect Data in a String Variable
new_Date = InputBox("Please date with this format [dd-mm-yyyy] to start with:", "Collect user Input")
'End Macro if Cancel Button is Clicked or no Text is Entered
If IsEmpty(new_Date) = True Then Exit Sub
Range("A6").Select
With ActiveWorkbook.Connections("test").OLEDBConnection
.CommandText = "usp_GetNewData '" & new_Date & "'"
ActiveWorkbook.Connections("test".Refresh
End With
With Sheet1
Set FoundCell = .Cells.Find("Name").Offset(1, 0)
'Define the first row of the data
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row - FoundCell.Row + 1
Debug.Print TypeName(lRow), lRow
'Set up the dynamic Table of Data
Set tblCurrent = FoundCell.Resize(lRow, 7)
'Adding 5 more rows as data starting at row 6
tblCurrent.Copy
'old_Date = Range("D" & lRow + 5).Value
End With
Sheets("Sheet2").Activate
With Sheet2
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Range("A1").Offset(lastRow, 0).Select
ActiveSheet.Paste
End With
End Sub