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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

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