"Object variable or With block variable not set" but it is, help

LlebKcir

Board Regular
Joined
Oct 8, 2018
Messages
219
Code:
Private Sub NetworkComboBox_Change()

Dim FoundCell As Range
Dim ws As Worksheet
Dim i As Integer
Dim rng As String
' Dim FoundMax As Integer  - Now set as global variable

Set ws = ActiveSheet

GroupComboBox.Clear             ' Clear current Group ComboBox

Set FoundCell = ws.Range("A:A").Find(What:=NetworkComboBox.Value) ' Find the cell for the selected network
rng = "B" & FoundCell.Row       ' Build the string for the range of the max value
FoundMax = Range(rng).Value     ' Assign the maximum group number
For i = 1 To FoundMax
    GroupComboBox.AddItem i
Next i

End Sub

The debugger is yellow on the rng variable. well that is clearly set. This code has been working for months now this morning it is having all kinds of issues. The save as function seems to also be having strange issues. Again that code has been working for months and this morning all freaking out.

any clue? Thank you in advance.
 

Attachments

  • Table.Debug-21-Feb-2020.PNG
    Table.Debug-21-Feb-2020.PNG
    19.9 KB · Views: 26
  • Table.Error-21-Feb-2020.PNG
    Table.Error-21-Feb-2020.PNG
    4.1 KB · Views: 25

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Without having the right data to test it on, best guess would be that the item in the combobox is not found in column A.

Try running it again, then go into debug mode and hover over the FoundCell variable to see if it shows up as 'Nothing'
 
Upvote 0
FoundCell is finding the correct input (user choice) from the combobox provided by the user form.
 
Upvote 0
There is nothing else in that line that would result in runtime error 91.
See if it works if you bypass the string with the method below. If the combobox list shows a single item of 99999 then it means that FoundCell is not finding anything.
Code:
Private Sub NetworkComboBox_Change()

Dim FoundCell As Range
Dim ws As Worksheet
Dim i As Long
' Dim FoundMax As Integer  - Now set as global variable

Set ws = ActiveSheet

GroupComboBox.Clear             ' Clear current Group ComboBox

Set FoundCell = ws.Range("A:A").Find(What:=NetworkComboBox.Value) ' Find the cell for the selected network
If Not FoundCell Is Nothing Then
    FoundMax = FoundCell.Offset(, 1).Value    ' Assign the maximum group number
    For i = 1 To FoundMax
        GroupComboBox.AddItem i
    Next i
Else
    GroupComboBox.AddItem 99999
End If
End Sub
 
Upvote 0
sorry for the long delay, yes the max was labeled as 99999, why now after all these months of working flawlessly? nothing has changed, the columns are still the same on both the list that provides the drop down for the user form and on the workbook that is opened.
 
Upvote 0
Find retains all the settings that were last used, including those done in a manual find, so perhaps one of them had been changed. You should really specify all the settings in a VBA Find operation to be safer.
 
Upvote 0
RoryA, thank you for the reply.

Here is the full code, lets see if that helps you help me.

Code:
Option Explicit

Public FoundMax As Integer ' Group maximum global variable. This is set in the GroupComboBox sub


Private Sub CancelCommandButton_Click()

Unload Me       ' Closed User Form

End Sub

Function DirSelect() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    DirSelect = sItem
    Set fldr = Nothing
End Function

Private Sub StartCommandButton_Click()
' Should Open Table Formulas from test folder
' populate Network field & group number.
' .Value=.Value to clear arrays and keep raw text
' perform Save As .xlsx to local computer

'=====================================
' Set Variables
'=====================================
Dim wb As Workbook      ' Shortcut for workbook
Dim ws As Worksheet     ' Shortcut for worksheet
Dim Network As String   ' Used to name workbook as part of Save As
Dim Group As String     ' Used to name workbook as part of Save As
Dim sFName As String    ' Used to name workbook as part of Save As - Final uniform name
Dim Def As String       ' String used as part of the Save As process
Dim i As Integer        ' Iterator variable
Dim Path As String      ' File save path
Dim screenUpdateState As Variant
Dim statusBarState As Variant
Dim displayPagebreakState As Variant

Path = DirSelect()      ' Call the function for the user to select the save path

    ' ============================================
    ' Get current state of various Excel settings.
    ' ============================================

        screenUpdateState = Application.ScreenUpdating
        statusBarState = Application.DisplayStatusBar
        displayPagebreakState = ActiveSheet.DisplayPageBreaks   ' This is sheet-level only.

    ' ==================================================
    ' Turn off Excel functionality to speed up the code.
    ' ==================================================

        Application.ScreenUpdating = False
        Application.DisplayStatusBar = False
        ActiveSheet.DisplayPageBreaks = False   ' This is sheet-level only.

If NOOptionButton = True Then
    FoundMax = 1
End If
    
For i = 1 To FoundMax
    ' Opens Tabel from ShareNow, currently hardcoded to test folder
    Set wb = Workbooks.Open("https://sharenow....Table-TEST-formulas.xlsm")
    
    'wb.Application.Visible = True           ' Makes the Table spreadsheet visible
    Set ws = wb.Worksheets("Table")     ' Correct worksheet to activate
    'ws.Visible = xlSheetVisible             ' Should keep Table Formula workbook as the visible workbook and worksheet
    ws.Activate                             ' Marks the correct workbook and worksheet to active status
    ws.Range("a1").Select                   ' Select cell A1, safe cell with zero data.
    
    ws.Cells(1, 3).Value = NetworkComboBox.Value       ' Populate cell C1 with Network chosen from dropdown list
    If NOOptionButton = True Then
        ws.Cells(1, 7).Value = GroupComboBox.Value       ' Populate cell G1 with group number
    Else
        ws.Cells(1, 7).Value = i
    End If
    
    Set wb = ActiveWorkbook                 ' Used as shortcut for Workbook
    Set ws = wb.Worksheets("Table")     ' Worksheets shortcut to read data from Table
        ' Read Cells(row, column).Value and store as variable - C1, Network name
    Network = ws.Cells(1, 3).Value
        ' Read Cells(row, column).Value and store as variable - G1, Group #
    Group = ws.Cells(1, 7).Value
        ' Final output will be in format: Table-City_Group_1.xlsx
    Def = "Table-" & Network & "_Group_" & Group
    
    Call CancelCommandButton_Click      ' Close Command Button Window, seems to hang if not closed
    
    '=====================================
    ' Start Copy/Paste Values only section                  Column D -> H, Rows 3 -> 52
    '=====================================
    ws.Range("D3:H52").Value2 = ws.Range("D3:H52").Value2     ' Sets the value from the array output, clearing the array formula
    
    '=====================================
    ' Fill blanks null vaults with FALSE
    ' Leave IS/M (Row 27) blank
    '=====================================
    
    ' Cells(Row, Column) -> Row 3, Column 5 -> E3
    If IsEmpty(Cells(3, 5)) = True Then
        ws.Range("E12:E26").Value = "FALSE"
        ws.Range("E28").Value = "FALSE"
    End If
    
    ' Cells(Row, Column) -> Row 3, Column 6 -> F3
    If IsEmpty(Cells(3, 6)) = True Then
        ws.Range("F12:F26").Value = "FALSE"
        ws.Range("F28").Value = "FALSE"
    End If
    
    ' Cells(Row, Column) -> Row 3, Column 7 -> G3
    If IsEmpty(Cells(3, 7)) = True Then
        ws.Range("G12:G26").Value = "FALSE"
        ws.Range("G28").Value = "FALSE"
    End If
    
    ' Cells(Row, Column) -> Row 3, Column 8 -> H3
    If IsEmpty(Cells(3, 8)) = True Then
        ws.Range("H12:H26").Value = "FALSE"
        ws.Range("H28").Value = "FALSE"
    End If
    
    '=====================================
    'Start Save As process
    '=====================================
'    sFName = Application.GetSaveAsFilename(InitialFileName:=Def, FileFilter:="Excel Files (*.xlsx), *.xlsx, Macro Enabled Workbook" & "(*.xlsm), *xlsm")
    
'        If sFName <> "False" Then                   ' Checks that sFName has not been cancled
'          If Right(sFName, 4) = "xlsx" Then         ' If saved as *.xlsx this section provides correct file type 51
'             Application.DisplayAlerts = False      ' *.xlsx is no macros enabled
'             ws.SaveAs sFName, 51
'             Application.DisplayAlerts = True       ' Displays all errors from SaveAs
'          ElseIf Right(sFName, 4) = "xlsm" Then     ' If saved as *.xlsm this section provides correct file type 52
'             ws.SaveAs sFName, 52                   ' *.xlsm is macro enabled
'          End If
'        End If
    wb.SaveAs Filename:=Path & "\" & Def, CreateBackup:=False
Next i

          ' ===============================
          ' Return Excel to original state.
          ' ===============================

          Application.ScreenUpdating = screenUpdateState
          Application.DisplayStatusBar = statusBarState
          ActiveSheet.DisplayPageBreaks = displayPagebreakState         ' This is sheet-level only.
Unload Me


End Sub

Private Sub UserForm_Click()
On Error Resume Next
TableForm.Show

End Sub

Private Sub UserForm_Initialize()

' Create dropdown list for Network
' The use of the Me. provides auto-populate to gain access for the
' data contained in tNetworkName table with
' column header Underground Network Name
' This one line replaces the need to manually populate the list below with
' .AddItem "Network".  Old code.
' <xxx 9/17/2019> Code updated to simplify future data
Me.NetworkComboBox.List = Range("tNetworkName[Network Name]").Value

' Empty group ComboBox box
GroupComboBox.Value = ""

' Set All Groups radial button default NO
NOOptionButton.Value = True

End Sub

Private Sub NetworkComboBox_Change()

Dim FoundCell As Range
Dim ws As Worksheet
Dim i As Integer
Dim rng As String
' Dim FoundMax As Integer  - Now set as global variable

Set ws = ActiveSheet

GroupComboBox.Clear             ' Clear current Group ComboBox

Set FoundCell = ws.Range("A:A").Find(What:=NetworkComboBox.Value) ' Find the cell for the selected network
rng = "B" & FoundCell.Row       ' Build the string for the range of the max value
FoundMax = Range(rng).Value     ' Assign the maximum group number
For i = 1 To FoundMax
    GroupComboBox.AddItem i
Next i

End Sub
well cleaned up a bit to hide some name schemes, but the code is still there.
 
Upvote 0
Thanks for that info, Rory. This is something that I wasn't aware of.

I knew that ctrl f / ctrl h retained the last used settings, but didn't realise a vba range.find would use those same, changed settings.
 
Upvote 0
What Rory means is that you should define the optional parameters of Find in case one of them has been changed without you realising.

VBA Code:
Set FoundCell = ws.Range("A:A").Find(What:=NetworkComboBox.Value, After:=ws.Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlByRows, MatchCase:=False, MatchByte:=False, SearchFormat:=False) ' Find the cell for the selected network
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top