+ Reply to Thread
Results 1 to 9 of 9

[UserForm's Listbox]Why is sheetname empty when using it in another procedure?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Problem: using listbox.value (for sheet name selection) in another procedure results in nothing?

    thwuserform.png

    Trying to use shtName selection in the list box to use it in another procedure but this is not returning anything after checking debugger. Full code at the end shows that I declared shtName as public and in declarations section (whole code behind userform).

    Copy to clipboard
    Private Sub wsList_Click()
    
    Dim nSheet As String
    
    nSheet = wsList.Value
    
    Set shtName = Sheets(nSheet)
    
    shtName.Activate
         
    End Sub
    After drilling down, I find shtName variable to be empty.

    Copy to clipboard
    Private Sub import()
    
    Set mainWb = ActiveWorkbook
    
    MsgBox shtName.Name
    
    copyData 'this procedure is working correctly as I am using activesheet as reference from workbooks(2).
    
    Set GetRng = Application.InputBox("Select the first header WITHIN data area!", _
    "Row Collector", Type:=8)
    
    strRow = GetRng.Row 'Getting row
    
    Debug.Print strRow
    Debug.Print mainWb.Name
    Debug.Print shthame.Name ' I put a breakpoint here to see if this was the problem. MainWb was captured correct but variable ShtName was not used in this procedure.
    
    'copying the relevant range
    
    With mainWb.shtName
            i = .Cells(strRow + 1, 1).End(xlDown).Row 'this is row number
            Debug.Print "The value of i is " & i
            .Cells(i, 1).Paste
    End With
    
    
    End Sub
    Things that I have tried:

    - Putting shtName var in declarations section: Public shtName as worksheet
    - Setting shtName to worksheet object using Set keyword.


    Is there any other listbox method I should be aware of?

    One other question if you could answer:

    • How to avoid standard message that states to clear clipboard (at the end of copyData procedure)? Programmatically.

    For reference, here is the full code:

    Public shtName As Worksheet 'tried to change this to Public
    Private mainWb As Workbook
    Private iWb As Workbook
    __________________________________________________________
    
    Private Sub AppendBtn_Click()
    
    Set mainWb = Workbooks(1)
    
    'if cleardata.value is true then start cleaning + pasting
    If ClearData.Value = True Then
    cleanAndimport
    Else
    import
    End If
    
    
    
    End Sub
    __________________________________________________________
    
    Private Sub wsList_Click()
    
    Dim nSheet As String
    
    nSheet = wsList.Value
    
    Set shtName = Sheets(nSheet)
    
    shtName.Activate
    
    End Sub
    Private Sub UserForm_Initialize()
    
    Dim n As Long
    
    For n = 1 To ActiveWorkbook.Sheets.Count
    wsList.AddItem ActiveWorkbook.Sheets(n).Name
    Next n
    
    End Sub
    __________________________________________________________
    
    Private Sub ClearData_Click() 'This is a simple checkbox
    End Sub
    __________________________________________________________
    Private Sub CloseBtn_Click()
    Unload Me
    End Sub
    __________________________________________________________
    Private Sub copyData()
    
    Dim strRow As Integer
    Dim OneRowBelow As Long
    Dim GetRng As Range
    
    'Get file path for setting workbook object
    f = Application.GetOpenFilename(filefilter:="Excel Files,*.xl*;*.xm*")
    
    Set iWb = Workbooks.Open(f)
    
    Set GetRng = Application.InputBox("Select the first header WITHIN data area!", _
    "Row Collector", Type:=8)
    
    strRow = GetRng.Row 'Getting row
    
    Debug.Print strRow
    
    'copying the relevant range
    
    With iWb.ActiveSheet
    i = .Cells(strRow + 1, 1).End(xlDown).Row 'this is row number
    Debug.Print i
    j = .Cells(strRow + 1, Columns.Count).End(xlToLeft).Column
    Debug.Print j
    .Range(Cells(strRow + 1, 1), Cells(i, j)).Copy
    End With
    
    iWb.Close
    
    End Sub
    __________________________________________________________
    
    Private Sub import()
    
    Set mainWb = ActiveWorkbook
    
    MsgBox shtName.Name
    
    copyData
    
    Set GetRng = Application.InputBox("Select the first header WITHIN data area!", _
    "Row Collector", Type:=8)
    
    strRow = GetRng.Row 'Getting row
    
    Debug.Print strRow
    Debug.Print mainWb.Name
    Debug.Print shthame.Name
    
    'copying the relevant range
    
    With mainWb.shtName
    i = .Cells(strRow + 1, 1).End(xlDown).Row 'this is row number
    Debug.Print "The value of i is " & i
    .Cells(i, 1).Paste
    End With
    
    
    End Sub
    __________________________________________________________
    
    Private Sub cleanAndimport()
    
    Dim strRow As Integer
    Dim OneRowBelow As Long
    Dim GetRng As Range
    
    copyData
    
    Set GetRng = Application.InputBox("Select the first header WITHIN data area!", _
    "Row Collector", Type:=8)
    
    With mainWb.shtName
    'clearing sheet
    i = .Cells(strRow + 1, 1).End(xlDown).Row 'this is row number
    Debug.Print i
    j = .Cells(strRow + 1, Columns.Count).End(xlToLeft).Column
    Debug.Print j
    .Range(Cells(strRow + 1, 1), Cells(i, j)).Clear
    i = .Cells(strRow + 1, 1).End(xlDown).Row 'this is row number
    .Range(Cells(strRow + 1, 1), Cells(i, 1)).Paste
    End With
    
    End Sub
    Last edited by immigrated4urjob; 01-18-2022 at 03:59 AM.

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    There are parts of your code where you refer to:
    Debug.Print shthame.Name
    The above will not give a result as shthame has not been defined, could this typo be your issue?

    To clear the clipboard use the below after the paste line:
    Application.CutCopyMode = False
    The other option for a variable could be 'Global' have you tried to declare as 'Global'?

    Hope this helps
    Last edited by CheeseSandwich; 01-18-2022 at 04:06 AM.

  3. #3
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Quote Originally Posted by CheeseSandwich View Post
    There are parts of your code where you refer to:
    Debug.Print shthame.Name
    The above will not give a result as shthame has not been defined, could this typo be your issue?

    To clear the clipboard use the below after the paste line:
    Application.CutCopyMode = False
    The other option for a variable could be 'Global' have you tried to declare as 'Global'?

    Hope this helps
    Hi, using option explicit definitely helped and now the debugger problem narrows down to the line in red.
    Option Explicit
    
    Private mainWb As Workbook, iWb As Workbook
    Private shtName As Worksheet
    Private Rng1 As Range, Rng2 As Range
    Private i As Long, j As Long, strRow As Long
    Private f As String
    'Public - all modules
    'Private - all procedures
    'Dim - procedure
    
    __________________________________________________________________
    
    Private Sub ClearData_Click()
    End Sub
    __________________________________________________________________
    Private Sub CloseBtn_Click()
    Unload Me
    End Sub
    __________________________________________________________________
    Private Sub UserForm_Initialize()
    
    Set mainWb = ActiveWorkbook
    
    Dim n As Long
    
    For n = 1 To ActiveWorkbook.Sheets.Count
         wsList.AddItem ActiveWorkbook.Sheets(n).Name
    Next n
    
    End Sub
    Private Sub AppendBtn_Click()
    
    Set mainWb = Workbooks(1)
    
    'if cleardata.value is true then start cleaning + pasting
    If ClearData.Value Then
         cleanAndimport
    Else
         import
    End If
    
    
    End Sub
    __________________________________________________________________
    
    Private Sub wsList_Click()
    
    Set shtName = Sheets(Me.wsList.Value) 'will this actually work?
    
    MsgBox shtName.Name
    
    shtName.Activate
    
    End Sub
    __________________________________________________________________
    Private Sub copyData()
    
    Dim strRow As Long
    
    'Get file path for setting workbook object
    f = Application.GetOpenFilename(filefilter:="Excel Files,*.xl*;*.xm*")
    
    Set iWb = Workbooks.Open(f)
    
    Set Rng1 = Application.InputBox("Select the first header WITHIN data area!", _
    "Export", Type:=8)
    
    strRow = Rng1.Row 'Getting row
    
    Debug.Print strRow
    
    'copying the relevant range
    
    With iWb.ActiveSheet
            i = .Cells(strRow + 1, 1).End(xlDown).Row 'this is row number
            Debug.Print i
            j = .Cells(strRow + 1, Columns.Count).End(xlToLeft).Column
            Debug.Print j
            .Range(Cells(strRow + 1, 1), Cells(i, j)).Copy
    End With
    
    iWb.Close
    
    End Sub
    __________________________________________________________________
    
    Private Sub import()
    
    Dim strRow As Long
    
    'Set mainWb = ActiveWorkbook
    
    copyData
    
    Set Rng2 = Application.InputBox("Select the first header WITHIN data area!", _
    "Import", Type:=8)
    
    strRow = Rng2.Row 'Getting row
    
    Debug.Print strRow
    Debug.Print shtName.Name
    Debug.Print mainWb.Name
    
    'copying the relevant range
    
    With mainWb.shtName
            i = .Cells((strRow + 1), 1).End(xlDown).Row 'this is row number
            Debug.Print "The value of i is " & i
    End With
    
    mainWb.shtName.Cells((i + 1), 1).Paste
    
    
    End Sub
    
    End Sub

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Two words: Option Explicit.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    With mainWb.shtName
    should be:

    With shtName

  6. #6
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126
    Quote Originally Posted by rorya View Post
    With mainWb.shtName
    should be:

    With shtName
    I guess the question is that mainWb is implied and I don't need to reference it?

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Quote Originally Posted by immigrated4urjob View Post
    I guess the question is that mainWb is implied and I don't need to reference it?
    When you assign a sheet to the variable, it is a specific sheet which can only be in one workbook, so there is no need to qualify it further. If you use MainWb.shtname you should get a 438 error since you are effectively trying to access a shtname property of a workbook, and that doesn't exist.

  8. #8
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    This one:
    i = .Cells((strRow + 1), 1).End(xlDown).Row 'this is row number
    Should be:
    i = .Cells(strRow + 1, 1).End(xlDown).Row 'this is row number
    Although I doubt this would have caused an error.

  9. #9
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Usually you would define the workbook first and then the sheet from that workbook, after that you only need to reference it as the worksheet. See below:

    Sub test()
        Dim wb As Workbook
        Dim ws As Worksheet
        
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Sheet1")
        
        ws.Range("A1").Value = "Hello World"
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Userform Can't Open When ListBox is Empty
    By Matt W in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 09-14-2021, 12:39 PM
  2. userform listbox add empty cell to my list
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2020, 06:12 PM
  3. [SOLVED] Selecting SheetName from Listbox
    By n4rs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-15-2019, 11:26 PM
  4. Unable to get userform listbox items into next empty cell
    By neevee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2018, 02:18 PM
  5. [SOLVED] if anything in listbox select first item, if listbox empty do nothing (listbox in userform
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2015, 12:49 PM
  6. Listbox to Listbox, no duplicates & submitting same UserForm data for each Listbox entry.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 12:18 PM
  7. Use sheetname to open a Userform
    By aph in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-02-2007, 03:19 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1