Code Page 2Code Page 1I am trying to open the sheet with listbox selection. The code is as under. But it is showing an error. I am new to vba. Plz help
Public Sub AddData_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim Sht As String
Sht.Text = ListBox1.SelectedItem.Tostring()
Worksheets(CStr(Sht)).Activate
Try this
Private Sub AddData_Click()
Dim i As Integer
Dim sht As String
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
sht = ListBox1.List(i)
End If
Next i
If sht = "" Then
MsgBox "You didn't select an item in the listbox.", vbExclamation
Exit Sub
Else
Worksheets(sht).Activate
End If
End Sub
you could try this:
Private Sub AddData_Click()
With Me.ListBox1
If .ListIndex = -1 Then
MsgBox "No item selected!"
Else
Worksheets(.Value).Activate
End If
End With
End Sub
Related
I keep getting an error:
Run-time error '91; Object variable or with block variable not set.
My script runs fine and does what it needs to do but I can't figure out how to get rid of this error.
Thank you for the help.
Public Sub CommandButton1_Click()
Dim rng As Range
Set rng = Range("F24:I24")
rng.Select
If TextBox1.Text = "" Then
MsgBox ("Must insert Temperature you dingus!")
Else
rng = TextBox1.Text
Call GetCabinet1
End If
Unload Me
End Sub
Public Sub UserForm_Initialize()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheets("Executive Summary")
wb.Activate
ws.Select
UserForm1.Show
Unload Me
End Sub
remove all those Unload.Me from both your subs and place it in the sub calling that userform
place a Me.Hide by the end of CommandButton1_Click(), instead
finally remove UserForm1.Show from UserForm_Initialize since it'd make it repeat twice
so your "Main" sub would look like:
Sub main()
Dim UF As UserForm1
Set UF = New UserForm1
UF.Show
Unload UF ' unload the userform from here
End Sub
and your userform1 code like:
Private Sub CommandButton1_Click()
Dim rng As Range
Set rng = Range("F24:I24")
rng.Select
If TextBox1.Text = "" Then
MsgBox ("Must insert Temperature you dingus!")
Else
rng = TextBox1.Text
Call GetCabinet1
End If
Me.Hide
End Sub
Public Sub UserForm_Initialize()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheets("Executive Summary")
wb.Activate
ws.Select
End Sub
Simply replacing the unload me with me.hide fixed my problem... Thank you to all of those who gave their input....
in WB1, I use the code above to display the opened workbook names in the Lisbox1 and their respective worksheets in listbox2 using a User form. But I would like to use Listview1 and Listview2 instead because I would like for every workbook and Worksheet name to show beside each one of them a checkbox, What changes should I do so it works in Listview1 and Listview2.
Option Explicit
Private Sub UserForm_Initialize()
Dim wb As Workbook
Me.Caption = "Workbooks and Sheets Detail"
For Each wb In Application.Workbooks
ListBox1.AddItem wb.Name
Next wb
End Sub
Private Sub ListBox1_Click()
Dim sWorkbookname As String
sWorkbookname = ListBox1.List(ListBox1.ListIndex)
ListWbWorksheets sWorkbookname
End Sub
Private Sub ListWbWorksheets(ByVal psWorkbookName As String)
Dim targetWb As Excel.Workbook
Dim n As Long
Set targetWb = Application.Workbooks(psWorkbookName)
ListBox2.Clear
For n = 1 To targetWb.Sheets.Count
ListBox2.AddItem targetWb.Sheets(n).Name
Next n
Set targetWb = Nothing
End Sub
It'll take some trial and error to learn how to use a Listview. This should give you a good start.
Private Sub ListView1_Click()
Dim ws As Worksheet
Dim item As ComctlLib.ListItem
ListView2.ListItems.Clear
For Each ws In Workbooks(ListView1.SelectedItem.Text).Worksheets
Set item = ListView2.ListItems.Add(Text:=ws.Name)
Next
End Sub
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim item As ComctlLib.ListItem
With ListView1
.View = lvwReport
.MultiSelect = False
.ColumnHeaders.Add Text:="Workbooks"
.ColumnHeaders.Add Text:="Paths"
End With
With ListView2
.View = lvwReport
.MultiSelect = False
.ColumnHeaders.Add Text:="Worksheets"
End With
For Each wb In Workbooks
Set item = ListView1.ListItems.Add(Text:=wb.Name)
item.SubItems(1) = wb.Path
Next
End Sub
In a User form I would like to display two listboxs . In the listbox1 I would display the Opened Workbooks with the Exception of the Personal Workbook; and whenever I click one of the Workbooks in the Listbox1 I would like to display the Worksheets available in that workbook in the Listbox2.
Doing some research I found the following code which represent how far I have been able to go about this:
Private Sub UserForm_Initialize()
UserForm1.Caption = "Workbooks and Sheets"
Dim wb As Workbook
Dim n As Long
For Each wb In Application.Workbooks
ListBox1.AddItem wb.Name
Next wb
For n = 1 To ActiveWorkbook.Sheets.Count
ListBox2.AddItem ActiveWorkbook.Sheets(n).Name
Next n
End Sub
Also I found this Post in Here but I should be doing something wrong because when I run the code both list box appear without any content whatsoever. Do you know How could I get this code to Work?
Thank you
You should create a subroutine that will refresh both list. In this way, you can keep the open Workbook list up to date.
Private Sub ListBox1_Click()
RefreshListBoxes
End Sub
Private Sub UserForm_Initialize()
RefreshListBoxes
End Sub
Sub RefreshListBoxes()
Dim wb As Workbook, ws As Worksheet
ListBox2.Clear
If ListBox1.ListIndex > -1 Then
On Error Resume Next
Set wb = Workbooks(ListBox1.Value)
On Error GoTo 0
If wb Is Nothing Then
MsgBox "Workbook not found: " & ListBox1.Value, vbCritical, "Try Again"
Else
For Each ws In wb.Worksheets
ListBox2.AddItem ws.Name
Next
End If
End If
ListBox1.Clear
For Each wb In Workbooks
If Not wb.FullName Like "*Excel\XLSTART\PERSONAL.XL*" Then ListBox1.AddItem wb.Name
Next
End Sub
Here's some code to get you started:
Option Explicit
Private Sub UserForm_Initialize()
Dim wb As Workbook
Me.Caption = "Workbooks and Sheets"
For Each wb In Application.Workbooks
ListBox1.AddItem wb.Name
Next wb
'Selecting item 0 (zero), i.e. the list's top element, will trigger its Click event,
'which in turn will call ListWbWorksheets, populating ListBox2.
ListBox1.Selected(0) = True
End Sub
Private Sub ListBox1_Click()
Dim sWorkbookName As String
sWorkbookName = ListBox1.List(ListBox1.ListIndex)
ListWbWorksheets sWorkbookName
End Sub
Private Sub ListWbWorksheets(ByVal psWorkbookName As String)
Dim targetWb As Excel.Workbook
Dim n As Long
Set targetWb = Application.Workbooks(psWorkbookName)
ListBox2.Clear
For n = 1 To targetWb.Sheets.Count
ListBox2.AddItem targetWb.Sheets(n).Name
Next n
Set targetWb = Nothing
End Sub
Note that the code above doesn't check for any errors; #Thomas Inzina's answer at least checks whether a workbook's name leads to an actual workbook.
I'm working with the following code:
Option Explicit
Sub checkboxfilter()
Dim cb As CheckBox
Dim oWS As Worksheet
Dim oWB As Workbook
Dim oPvt As PivotTable
Dim oPvtField As PivotField
Dim oPvtFilter As PivotFilter
Set cb = oWS("Control").Controls("YTD Filter")
If cb.Value = True Then
For Each oWS In ThisWorkbook.Worksheets
For Each oPvt In oWS
With oPvtField
.CurrentPage.Name = "Yes"
End With
Next oPvt
Next oWS
End If
End Sub
the goal is to toggle each pivot table in the workbook by a yer-to-date filter via checkbox. The code hits a snag under set cb= as an object variable or with not set. What am I missing here to get this control working? I'm also avoiding the use of a slicer.
Thanks.
That kind of control has it's own event and you should use it. Therefore:
go to sheet where you have your checkbox
set Design mode on developer tab on
double click on you check box to ...
...see something like Private Sub CheckBox1_Click()
inside that sub call your subroutine:
Private Sub CheckBox1_Click()
call checkboxfilter
End Sub
I was able to revise based on adjusting the type of format the set cb = as a .Checkboxes and ensuring at each pivot fielt was accurately called as #KazimierzJawor pointed out. Additionally with this type the value needed to be a 0 or 1 rather than True or False. Corrected and final code below.
Private Sub checkboxfilter()
Dim cb As CheckBox
Dim oWS As Worksheet
Dim oWB As Workbook
Dim oPvt As PivotTable
Dim oPvtField As PivotField
Dim oPvtFilter As PivotFilter
Set cb = Sheets("Control").CheckBoxes("YTD Filter")
If cb.Value = 1 Then
For Each oWS In ThisWorkbook.Worksheets
For Each oPvt In oWS.PivotTables
With oPvt.PivotFields("YTD?")
.CurrentPage = "Yes"
End With
Next oPvt
Next oWS
Else
For Each oWS In ThisWorkbook.Worksheets
For Each oPvt In oWS.PivotTables
With oPvt.PivotFields("YTD?")
.CurrentPage = "(All)"
End With
Next oPvt
Next oWS
End If
End Sub
Sub PivotFilter()
Dim pvtF As PivotField
Dim pvtI As PivotItem
Dim StartDate As Date
Dim EndDate As Date
Dim pvtIVal As String
StartDate = DateValue("Jan 1, 2018")
EndDate = Application.WorksheetFunction.EoMonth(Date, 0)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Create Month").ClearAllFilters
Set pvtF = ActiveSheet.PivotTables("PivotTable1").PivotFields("Create Month")
For Each pvtI In pvtF.PivotItems
If (pvtI <> "(blank)") Then
If DateValue(pvtI) >= StartDate And DateValue(pvtI) <= EndDate Then
pvtI.Visible = True
Else
pvtI.Visible = False
End If
Else
pvtI.Visible = False
End If
Next pvtI
End Sub
Here's a Sub at the Workbook level:
Public Sub BuildList(targetSheet As Worksheet, ByRef targetListBox As ListBox, lastRow As Integer)
For r = 1 To lastRow
If Trim(targetSheet.Range("A" & r).Value) <> "" Then
With targetListBox
.AddItem Trim(targetSheet.Range("A" & r).Value)
.List(.ListCount - 1, 1) = Trim(targetSheet.Range("B" & r))
End With
End If
Next r
End Sub
Here's some code in a Userform that calls it:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim bottomRow As Integer
Set ws = Worksheets("Our Status Code")
bottomRow = ws.Range("A65536").End(xlUp).Row
ThisWorkbook.BuildList ws, StatusCodesListbox, bottomRow
End Sub
When it hits the line that calls BuildList it throws an error 13 and I can't see why.
You need
ByRef targetListBox As MSForms.ListBox
There are two types of listbox for Excel.