Hello Folks,
Been battling copying the results from a specified worksheet to a new workbook.
Now I know the Autofiltering works fine, but as soon as I start adding copying functions, I get a "Subscript out of range" error starting at IngLastrow and for the life of me, I cannot figure out why.
strStart and strEnd will be user textbox values
Code
Option Explicit
Sub CmdReport_Click()
Dim strStart As String, strEnd As String, strPromptMessage As String
strStart = "2005/08/08"
'Validate the input string
If Not IsDate(strStart) Then
strPromptMessage = "Oops! It looks like your entry is not a valid " & _
"date. Please retry with a valid date. (YYYY/MM/DD)"
MsgBox strPromptMessage
Exit Sub
End If
strEnd = "2010/08/08"
If Not IsDate(strStart) Then
strPromptMessage = "Oops! It looks like your entry is not a valid " & _
"date. Please retry with a valid date. (YYYY/MM/DD)"
MsgBox strPromptMessage
Exit Sub
End If
Call CreateSubsetWorkbook(strStart, strEnd)
End Sub
Public Sub CreateSubsetWorkbook(StartDate As String, EndDate As String)
Dim wbkOutput As Workbook
Dim lngLastRow As Long, lngLastCol As Long, lngDateCol As Long
Dim rngFull As Range, rngResult As Range, rngTarget As Range
lngDateCol = 2
Set wbkOutput = Workbooks.Add
lngLastRow = Worksheets("DB").Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
lngLastCol = Worksheets("DB").Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).column
Set rngFull = Worksheets("DB").Range(Worksheets("DB").Cells(1, 1), Worksheets("DB").Cells(lngLastRow, lngLastCol))
With rngFull
.AutoFilter Field:=lngDateCol, _
Criteria1:=">=" & StartDate, _
Criteria2:="<=" & EndDate
Set rngResult = rngFull.SpecialCells(xlCellTypeVisible)
rngResult.Copy Workbooks(wbkOutput.Name).Sheets(1).Range("A1")
End With
Worksheets("DB").AutoFilterMode = False
If Worksheets("DB").FilterMode = True Then
Worksheets("DB").ShowAllData
End If
'MsgBox "Data transferred!"
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Display More
I know it may be cheeky to ask but my next step is to try specify a range to copy and order then like so: B:B,A:A,E:E,D:D,I:I,J:J,AQ:AQ. Doing the selection before copying to the new sheet may complicate things so I was thinking ordering then removing excess columns after being copied?
Any advice, help or suggestions would be greatly appreciated!
Cheers,
Kevin