I have two PivotTables on a sheet that I would like to have refreshed automatically whenever the sheet is open. I've put that macro this macro within that sheet. However, I am getting an error on this line "oPivot.RefreshTable"
Private Sub Worksheet_Activate()
Dim oSheet As Worksheet
Dim oPivot As PivotTable
Set oSheet = ActiveSheet
For Each oPivot In oSheet.PivotTables
oPivot.RefreshTable
Next oPivot
End Sub
This method works:
Private Sub Worksheet_Activate()
Dim oPivot As PivotTable
Dim xlsheet As Worksheet
Set xlsheet = ActiveSheet
For Each oPivot In xlsheet.PivotTables
oPivot.PivotCache.Refresh
Next oPivot
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've got a script that deletes rows 1-4 on on every worksheet, but would like it to skip a worksheet if its name is "Sheet1"
Sub RowDelete()
Dim xWs As Worksheet
Set xWs = ActiveSheet
ThisWorkbook.Worksheets.Select
Rows("1:4").Select
Selection.Delete
xWs.Select
End Sub
Run it through a FOR EACH Loop:
Sub RowDelete()
Dim xWs As Worksheet
For Each xWs In Worksheets
If xWs.Name <> "Sheet1" Then
xWs.Rows("1:4").Delete
End If
Next xWs
End Sub
Btw, try to learn coding without using .Select.
Here is my code. I'm new to VBA so, I am unsure how to iterate through multiple pages.
Here's my code:
Dim ws As Worksheet
Sub spellCheck()
For Each ws In ActiveWorkbook.Worksheets
Cells.CheckSpelling
Next
End Sub
Try this (this will simply activate each sheet):
Sub spellCheck()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
' Do stuff...
Next
End Sub