Consulting

Results 1 to 6 of 6

Thread: Case When error for set values

  1. #1
    VBAX Regular
    Joined
    Oct 2014
    Posts
    24
    Location

    Unhappy Case When error for set values

    HI!

    I'm having a weird problem.. I'm trying to look through a number of different columns for a certain value. If that value is there, then I want to copy over a certain range of cells into a new workbook. Some columns might contain values while other columns don't, and vice versa.

    I've discovered my problem... If the value (example, 32) is NOT in the first column being searched, the rest of my case statements don't work, and I get the error "Run-time error '1004' Application-defined or object-defined error". See a sample of my code below (I have 20 cases total, and several lines of code before/after this; it's just a sample)

      Case 1
      Set NewBook = Workbooks.Add
      Workbooks.Open "M:\John\" & FName
        With Worksheets(WorkSheetPull)
            .AutoFilterMode = False
            .Cells(1).AutoFilter Field:=4, Criteria1:=32
            If .AutoFilter.Range.Rows.Count > 1 Then
                .UsedRange.Columns(1).Offset(1).Resize(, 3).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(1)
                .UsedRange.Columns(4).Offset(1).Resize(, 2).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(4)
            End If
        End With
      
        Case 2
      Workbooks(FName).Activate
        With Worksheets(WorkSheetPull)
            .AutoFilterMode = False
            .Cells(1).AutoFilter Field:=6, Criteria1:=32
            If .AutoFilter.Range.Rows.Count > 1 Then
                .UsedRange.Columns(1).Offset(1).Resize(, 3).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(1).End(xlDown).Offset(1)
                .UsedRange.Columns(6).Offset(1).Resize(, 2).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(4).End(xlDown).Offset(1)
            End If
        End With
        
        Case 3
      Workbooks(FName).Activate
        With Worksheets(WorkSheetPull)
            .AutoFilterMode = False
            .Cells(1).AutoFilter Field:=8, Criteria1:=32
            If .AutoFilter.Range.Rows.Count > 1 Then
                .UsedRange.Columns(1).Offset(1).Resize(, 3).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(1).End(xlDown).Offset(1, 0)
                .UsedRange.Columns(8).Offset(1).Resize(, 2).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(4).End(xlDown).Offset(1, 0)
            End If
        End With
    If I set my criteria to a number that IS in the 1st column (example, 24) then it works!

    I'm realy stuck, any thoughts would be helpful!

    Thanks.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    NewBook is set only in Case 1.
    if it will be used for all cases, move it above Case 1 line.
    otherwise copy and paste that line after relevant case lines.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I think you can move all the below to before the Select Case
    Set NewBook = Workbooks.Add 
        Workbooks.Open "M:\John\" & FName 
        With Worksheets(WorkSheetPull)
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Regular
    Joined
    Oct 2014
    Posts
    24
    Location
    Hi mancubus, SamT,

    Thanks for the help but that still doesn't solve my problem

    Even when I put that line before the case statement, for certain integer values (example, 32) it always gives me that error message on "Case 2" at line " .UsedRange.Columns(1).Offset(1).Resize(, 3).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(1).End(xlDown).Offset(1)"

    The only thing I can think of why is because the values (example, 32) are NOT in the first column, or the first Case statement. Any thoughts would help out a ton.

    Thanks!

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    why not post entire code and workbook here?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I wonder why people ALWAYS leave out the most important part of the code. In this case, the code that sets the Select Case variable variable.

    I see the problem! In every case the criteria is hardcoded to "32," there fore you must insure that in every case the column has "32" in it.

    Case Solved

    I am totally guessing about what code you have and what is raising the error, but...
        Set NewBook = Workbooks.Add
        Workbooks.Open "M:\John\" & FName
        With Worksheets(WorkSheetPull)
            .AutoFilterMode = False
    
     Select Case Johnatha
    Case 1
            If Not .Columns(4).Find(32) Is Nothing Then
            .Cells(1).AutoFilter Field:=4, Criteria1:=32
            If .AutoFilter.Range.Rows.Count > 1 Then
                .UsedRange.Columns(1).Offset(1).Resize(, 3).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(1)
                .UsedRange.Columns(4).Offset(1).Resize(, 2).SpecialCells(12).Copy NewBook.Worksheets("Sheet1").Cells(4)
            End If
        End If
    Case 2
    If Not .Columns(6).Find(32) Is Nothing Then       
    'Etc
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •