Solved: An error with setting a wb. Why?
MD, et al,
The short answer to my question is that I forgot to 'set' my wb and ws assignments. New code replaces what I first posted.
Here's where my original query began:
The next project that I have is to write code to move selected data from my Grade & "_INFO.xls" file to another named Grade & "_Diagnostic Assessment.xls". Here is what I have so far. I copied the wb1/wb2 (here, called wbInfo, wbDiag) from the code MD gave me for the last project. As the comment says, I'm getting an error that I don't follow. The files are open on my desktop and yet the code, here, is not recognizing the first one.
My goal at the moment is to have autofilter find me only those lines in INFO that have a particular Standard code associated with them. Once I have that I'll add in using "activecell" to identify which row of those filtered I want to use, and then use offset to get the various pieces of data to move to the Diagnostic Assessment worksheet/workbook.
[vba]Option Explicit
Sub Move_Data()
Dim wbInfo As Workbook, wsInfo As Worksheet, FilenameInfo As String, LastInfoRow As Long
Dim wbDiag As Workbook, wsDiag As Worksheet, FilenameDiag As String, LastDiagRow As Long
Dim Grade As String, std As String, LastInfoCol As Long, LastDiagCol As Long
Dim rngInfo As Range, rngDiag As Range, InfoCol As Long, DiagCol As Long
Dim wb1 As Workbook
' InfoFile = OpenFile("Info")
' DiagFile = OpenFile("Diag")
Grade = "G4"
Set wbInfo = Workbooks(Grade & "_INFO.xls") ' <<< Err 91:Object Var or With Block Var not set ??
Set wbInfo = Workbooks("G4_INFO.xls")
' Set wsInfo = wbInfo.Worksheet("INFO")
With wsInfo
Grade = Left(.Cells(2, 2).Value, 2)
LastInfoRow = .UsedRange.Rows
LastInfoCol = .UsedRange.Columns
InfoCol = .Range("F1").Column
Set rngInfo = .Range(Cells(2, 1)).Resize(LastInfoRow, LastInfoCol) 'Column T
std = .Range("F2").Text
End With
wbDiag = Workbooks(Grade & " - Diagnostic Assessment.xls")
wsDiag = wbDiag.Worksheets("DA NL " & Grade)
With wsDiag
LastDiagRow = .UsedRange.Rows
LastDiagCol = .UsedRange.Columns
DiagCol = .Range("T1").Column
Set rngDiag = .Range(Cells(2, 1)).Resize(LastDiagRow, LastDiagCol) 'Column F
' std = ActiveCell.Text
With rngInfo
.AutoFilter
.AutoFilter Field:=InfoCol, Criteria1:=std
If Not rngInfo Is Nothing Then
.SpecialCells (xlCellTypeVisible)
If Not rngInfo Is Nothing Then MsgBox "Nothing Selected. Try next Std.", vbOKOnly, "Filter INFO File"
End If
End With
End Sub
[/vba]
Advice and counsel is cheerfully sought. And, don't worry, I'll carve code out and place it in Subs and Functions as may be appropriate. Right now, a single file is adequate for testing.
Thanks,
New query: How to get Autofilter to filter?
The autofilter is working, sort of. I expected it to display the list of items which match Criteria1. Instead, it's collasping them and showing one line; if I click the drop box on the search column, click my criteria value, and click ok, AutoFilter will finally show it to me.
Is my expectation wrong or am I failing to do something in my code?
Here's how things look at the moment:
[VBA]Option Explicit
Sub Move_Data()
Dim wbInfo As Workbook, wsInfo As Worksheet, FilenameInfo As String, LastInfoRow As Long
Dim wbDiag As Workbook, wsDiag As Worksheet, FilenameDiag As String, LastDiagRow As Long
Dim Grade As String, std As String, LastInfoCol As Long, LastDiagCol As Long
Dim rngInfo As Range, rngDiag As Range, InfoCol As Long, DiagCol As Long
Dim wb1 As Workbook
' InfoFile = OpenFile("Info")
' DiagFile = OpenFile("Diag")
Grade = "G4"
Set wbInfo = Workbooks(Grade & "_INFO.xls")
Set wsInfo = wbInfo.Sheets("INFO")
With wsInfo
LastInfoRow = .UsedRange.Rows.Count
LastInfoCol = .UsedRange.Columns.Count
InfoCol = .Range("T1").Column
Grade = Left(.Range("B2").Text, 2)
Set rngInfo = .Range("A2").Resize(LastInfoRow, LastInfoCol) 'Column T
End With
Set wbDiag = Workbooks(Grade & " Diagnostic Assessment (2).xls")
Set wsDiag = wbDiag.Worksheets("DA - NL " & Grade)
With wsDiag
LastDiagRow = .UsedRange.Rows.Count
LastDiagCol = .UsedRange.Columns.Count
DiagCol = .Range("F1").Column
Set rngDiag = .Range("A3").Resize(LastDiagRow, LastDiagCol) 'Column F
' wsDiag has a list of unique Standards numbers; wsInfo has 1 or more instances of each Std code
' this next line gets a Std code for autofiltering the INFO list to show only lines w/ std codes
std = .Range("F3").Text ' std = SC.4.E.5.1 & by visual inspec the are 7 items
End With
' std = ActiveCell.Text
' what puzzles me is that the following code autofilters my Std Code but shows it collapsed as
' one row, to see all selected, I have to click the dropdown arrow and select my Std Code and
' click okay. In Bob's (XLD's) example all of the selection appears (or so it seems). Am I not
' doing something necessary?
With rngInfo
.AutoFilter
.AutoFilter Field:=InfoCol, Criteria1:=std, VisibleDropDown:=True
On Error Resume Next
If Not rngInfo Is Nothing Then
.SpecialCells (xlCellTypeVisible)
If rngInfo Is Nothing Then MsgBox "Nothing Selected. Try next Std.", vbOKOnly, "Filter INFO File"
End If
On Error GoTo 0
End With
End Sub[/VBA]
Thanks,