Listbox rowsorce with Named Variable's

Bernieg

Board Regular
Joined
Jan 1, 2009
Messages
146
Office Version
  1. 365
Platform
  1. Windows
Hi Guys

I use the code below to refresh listboxs in forms.
The code works well when the the sheets are named, in this case "Change_Controll" .

What i would like to do is use a named variable called "Controll_sht"
Can't get it to work though.

VBA Code:
.RowSource = Controll_sht!(a2:L" & last_Row)

Thanking you in advance

Bernie


VBA Code:
Sub Refresh_Data()
Dim sh As Worksheet

Set sh = ThisWorkbook.Sheets("Change_Controll")

Dim last_Row As Long
last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
With Me.ListBox1
.ColumnHeads = True
.ColumnCount = 12
.ColumnWidths = "25,50,70,70,70,25,70,25,30,70,70,70"
If last_Row = 1 Then

.RowSource = "Change_Controll!A2:L2" ' Sheet called Change_Controll
Else
.RowSource = "Change_Controll!a2:L" & last_Row
End With
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi,
see if this update to your code does what you want

VBA Code:
Sub Refresh_Data()
    Dim sh              As Worksheet
    Dim last_Row        As Long
    Dim Controll_sht    As String
    
    Set sh = ThisWorkbook.Sheets("Change_Controll")
    
    Controll_sht = sh.Name
    
    last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
    If last_Row = 1 Then last_Row = 2
    
    With Me.ListBox1
        .ColumnHeads = True
        .ColumnCount = 12
        .ColumnWidths = "25,50,70,70,70,25,70,25,30,70,70,70"
        .RowSource = "'" & Controll_sht & "'!a2:L" & last_Row
    End With
    
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,823
Members
449,470
Latest member
Subhash Chand

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