I am trying to create a clickable toggle button that hides a year's worth of data if it is currently unhidden and unhides it if it is hidden. I know this syntax is incorrect but I am not sure how to make it work. Any help would be appreciated.
Sub Hide_2012()
Dim Yr2012 As Range
Set Yr2012 = ThisWorkbook.Worksheets("Open Jobs Calculations").Range("AI:AT")
If Yr2012.Visible = False Then
Yr2012.Visible = True
Else If Yr2012.Visible = True Then
Yr2012.Visible = False
End If
This works for me. Apparently hidden only works on entire columns.
Dim Yr2012 As Range
Set Yr2012 = ThisWorkbook.WorkSheets("Open Jobs Calculations").Range("A1:B10") 'change range
If Yr2012.EntireColumn.Hidden = False Then
Yr2012.EntireColumn.Hidden = True
ElseIf Yr2012.EntireColumn.Hidden = True Then
Yr2012.EntireColumn.Hidden = False
End If
Edit per Scott's comment:
Yr2012.EntireColumn.Hidden = Not Yr2012.EntireColumn.Hidden
much more elegant.
This modification of #findwindow's answer worked for me:
Set Yr2012 = ThisWorkbook.Worksheets("Open Jobs Calculations").Columns("AI:AT") ' <- (ie, set range to the desired columns)
then you can still reference the "Yr2012" range, without needing ".EntireColumn":
If Yr2012.Hidden = False Then ...
Just another take on the same idea, to reduce typing :)
Related
I am trying to set the data-label properties of a data point using this code snippet, but for some reason the properties are not being set as intended. [ Please see the pic for better clarity ].
A workaround could be using the Reset label Text [ See Pic - 2 ] thus forcing the data-label to update the set properties. But I couldn't find the vba equivalent of the same. Help.
'This is happening inside a loop
Dim thisbarpoint As Point
Set thisbarpoint = thischart.FullSeriesCollection(ibar).Points(jbar)
thisbarpoint.DataLabel.ShowCategoryName = True
thisbarpoint.DataLabel.ShowValue = False
thisbarpoint.DataLabel.ShowSeriesName = False
Debug.Print thisbarpoint.DataLabel.caption
Dim DataLabelCaption As String
DataLabelCaption = thisbarpoint.DataLabel.caption
Debug.Print DataLabelCaption 'This gives the value as 26.7%
PIC-1:
PIC-2:
This worked fine for me:
Dim cht As Chart, s As Series
Set cht = ActivePresentation.Slides(1).Shapes(1).Chart
Set s = cht.SeriesCollection(1)
Debug.Print s.HasDataLabels 'False
s.HasDataLabels = True
With s.DataLabels
.ShowCategoryName = True
.ShowValue = False
.ShowSeriesName = False
End With
We can't know if your file is corrupted, or if the problem is elsewhere in code you didn't include.
I need help with some rather easy macro, but I can't do that. So all I want is to switch between filters - I have two possibilities, by example two colours - black and white, and I only want to have one of them in the moment (I want to add button, so it will work as switch). Here is the situation
I've tried did it with conditional, of course it doesn't work, but hope it will helps you to understand my idea. If one filter is enable, the other one is disabled (so the objects connected with that obviously too).
Sub Makro5()
ActiveSheet.PivotTables("PivotTable3").PivotFields("colour"). _
CurrentPage = "(All)"
If ActiveSheet.PivotTables("PivotTable3").PivotFields("colour")
.PivotItems("black").Visible = False
.PivotItems("white").Visible = True
Then ActiveSheet.PivotTables("PivotTable3").PivotFields ("colour")
.PivotItems("black").Visible = True
.PivotItems("white").Visible = False
Else: ActiveSheet.PivotTables("PivotTable3").PivotFields ("colour")
.PivotItems("black").Visible = False
.PivotItems("white").Visible = True
End Sub
Something is wrong with If, because it's on red and I get "Complie error: Syntax error" info
This is what slicers are built for.
Put your cursor inside the the pivottable data area. Then goto insert > slicer > and choose colour. You will then have a slicer which will allow you to select either or. I don't know if you can disable multiselect in current excel version so user could press ctrl and select both. But this seems a simple way.
Or you could run code such as the following and then user just types in Black or White in to field and filter is applied:
Sub Test()
Dim wb As Workbook
Dim ws As Worksheet
Dim pvt As PivotTable
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet3")
Set pvt = ws.PivotTables("PivotTable3")
Dim pvtField As PivotField
Dim item As Long
Dim item2 As Long
Set pvtField = pvt.PivotFields("Colour")
pvtField.EnableItemSelection = False
End Sub
You need to have the Then on the same line as the If. (You can use _ to tell VBA that "code continues on next line")
You also don't have your And set up correctly, and you are making both "Black" and "White" visible before you check if they are visible?
Try this:
Sub ToggleColour()
With ActiveSheet.PivotTables("PivotTable3").PivotFields("colour")
If .PivotItems("white").Visible=True Then
.CurrentPage = "black"
Else
.CurrentPage = "white"
End If
End With
End Sub
I've got a workbook with 5 sheets: Sheet1, Sheet2, Sheet3, Sheet4, Sheet5. The first sheet: "Sheet" has a cell: "C3" with a drop down menu with 4 different options: Opt1, Opt2, Opt3, Opt4.
Depending on what is selected in this drop down menu, I'd like different columns to be hidden in the various sheets - in real time. And if nothing is entered, I'd like no columns hidden.
I've entered the below code which partly works but I think there's an issue because I've selected overlapping columns to hide - not completely sure.
Additionally, I'd like to work in hiding particular rows as well as the column below, depending on what different option you select in the drop down menu.
Also, I'll replicate hiding the same columns across all Sheets1-5.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("C3").Value = "Opt1" Then
Sheets("Sheet1").Columns("G:L").EntireColumn.Hidden = True
Sheets("Sheet1").Columns("N:T").EntireColumn.Hidden = True
Else
Sheets("Sheet1").Columns("G:L").EntireColumn.Hidden = False
Sheets("Sheet1").Columns("N:T").EntireColumn.Hidden = False
End If
If Range("C3").Value = "Opt2" Then
Sheets("Sheet1").Columns("B:F").EntireColumn.Hidden = True
Sheets("Sheet1").Columns("N:T").EntireColumn.Hidden = True
Else
Sheets("Sheet1").Columns("B:F").EntireColumn.Hidden = False
Sheets("Sheet1").Columns("N:T").EntireColumn.Hidden = False
End If
If Range("C3").Value = "Opt3" Then
Sheets("Sheet1").Columns("B:M").EntireColumn.Hidden = True
Else
Sheets("Sheet1").Columns("B:M").EntireColumn.Hidden = False
End If
If Range("C3").Value = "Opt4" Then
Sheets("Sheet1").Columns("B:AB").EntireColumn.Hidden = True
Else
Sheets("Sheet1").Columns("B:AB").EntireColumn.Hidden = False
End If
End Sub
Following on from my comment. You can define the entire range of columns as a variable e.g. entireRange and set this to unhidden at the start of each worksheet change.
Add fully qualified references to the ranges i.e. ThisWorkbook.Sheets("Sheet1") or ws (as a variable shown below).
As everything is unhidden at start Else is not needed for each If statement. This is probably where confusion arises.
Change to a Select case statement as you are testing the value of a single cell against different expected values.
Combine your separate line column ranges that you are hiding into one line statements e.g.
Sheets("Sheet1").Columns("G:L").EntireColumn.Hidden = True
Sheets("Sheet1").Columns("N:T").EntireColumn.Hidden = True
Becomes:
ws.Range("G:L,N:T").EntireColumn.Hidden = True
You code would then look like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook
Dim ws As Worksheet
Dim entireRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
Set entireRange = ws.Columns("B:AB")
entireRange.EntireColumn.Hidden = False
Select Case ws.Range("C3") 'Test the value of C3
Case "Opt1"
ws.Range("G:L,N:T").EntireColumn.Hidden = True
Case "Opt2"
ws.Range("B:F,N:T").EntireColumn.Hidden = True
Case "Opt3"
ws.Range("B:M").EntireColumn.Hidden = True
Case "Opt4"
entireRange.Hidden = True
End Select
End Sub
This will be easier to debug in terms of where things are being hidden or unhidden.
I have rewritten your code to make it easy to read and edit. My goal was to make the code as short as possible. I changed your if statement into a Select Case. It's more elegant and I think it's faster, but don't take my word for it.
The problem that had to be tackled was that if I select Opt1 and then select Opt2, the columns from Opt1 would still be hidden. So before the code hides anything, it automatically unhides all the columns in the range B:AB.
I added the Case Else to unhide all columns if you entered anything but Opt1, Opt2, Opt3 or Opt4. This could easily be changed into a MsgBox that warns the user that the entered value is incorrect. You can remove this line if you have limited the choice for the users already.
The Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Unhide Columns
Worksheets("Sheet1").Range("B:AB").EntireColumn.Hidden = False
Select Case Worksheets("Sheet1").Range("C3").Value
Case "Opt1"
Worksheets("Sheet1").Range("G:L,N:T").EntireColumn.Hidden = True
Case "Opt2"
Worksheets("Sheet1").Range("B:F,N:T").EntireColumn.Hidden = True
Case "Opt3"
Worksheets("Sheet1").Range("B:M").EntireColumn.Hidden = True
Case "Opt4"
Worksheets("Sheet1").Range("B:AB").EntireColumn.Hidden = True
' If anything else is entered, the columns will be unhidden.
Case Else
Worksheets("Sheet1").Range("B:AB").EntireColumn.Hidden = False
End Select
End Sub
I am creating a quiz. It works well for the part I have but is a bit lengthy. The RadioButtons I am using are set at the start through the Properties window. I have a "Reset" CommandButton that I am using to clear the quiz.
' Question Block 1
' THIS method does work
ActiveSheet.OptionButton1.Enabled = True
ActiveSheet.OptionButton2.Enabled = True
ActiveSheet.OptionButton3.Enabled = True
ActiveSheet.OptionButton4.Enabled = True
ActiveSheet.OptionButton1.Value = False
ActiveSheet.OptionButton2.Value = False
ActiveSheet.OptionButton3.Value = False
ActiveSheet.OptionButton4.Value = False
' Question Block 1 (Alternate Method)
' THIS method does NOT work
ActiveSheet.OptionButton1.GroupName = "Q1"
ActiveSheet.OptionButton2.GroupName = "Q1"
ActiveSheet.OptionButton3.GroupName = "Q1"
ActiveSheet.OptionButton4.GroupName = "Q1"
ActiveSheet.OptionButton.GroupName("Q1").Enabled = True
ActiveSheet.OptionButton.GroupName("Q1").Value = False
The first block of code works fine. The second one does not. What am I doing wrong with trying to assign the RadioButtons to a group in this manner? I've tried numerous approaches and most end with error messages saying object does not support this property or method, method or member not found, object required and on and on. Scratching my head. --Edited I am NOT using this in a UserForm. The RadioButtons are just placed on the Excel Sheet.
Found an answer that seems to work really well for my purposes. I was looking to do this by GroupNames property but as long as it works there are a number of ways to solve any problem. Hope someone else can benefit from this code.
' Defines OptionButtons as Objects
Dim OptBtn As OLEObject
' Re-Enables RadioButtons
For Each OptBtn In Sheet1.OLEObjects
If TypeName(OptBtn.Object) = "OptionButton" Then
OptBtn.Object.Enabled = True
End If
Next
' Resets RadioButtons to False (removes the dot)
For Each OptBtn In Sheet1.OLEObjects
If TypeName(OptBtn.Object) = "OptionButton" Then
OptBtn.Object.Value = False
End If
Next
Can some quicly explain the way to deselect all items in a newly created pivot table so that I can go back and select only one or two items? I tried the following:
.PivotItems("(Select All)").Visible = False
Thanks.
This is probably the closest you can get to what you want:
Dim i As Long
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next
This will make the very first option the only selected option (assuming this is within a with that points to the pivotfield). If you know what you want before hand... modify accordingly.
I've found that looping through each data item takes a lot of time, what you can do if you want to filter on a single item in a pivot without looping through all items is use the following code:
ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").ClearAllFilters
ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").PivotFilters.Add _
Type:=xlCaptionEquals, Value1:="Your string here"
this is basically a label filter but it worked for me.
Check the following out. Select data for specific field name. Please do note that you have to at least select one item by default. And also do not forget that if you want to hide items, Only contiguous items in a PivotTable Field can be hidden. Perhaps at page load, or worksheet open or any of your other sub trigger, you could select a particular items to be selected based on a specific field. Then allow your code to proceed with anything else.
Sub specificItemsField()
Dim pf As PivotField
Dim pi As PivotItem
Dim strPVField As String
strPVField = "Field Name"
Set pt = ActiveSheet.PivotTables(1)
Set pf = pt.PivotFields(strPVField)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
pf.AutoSort xlManual, pf.SourceName
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
pf.AutoSort xlAscending, pf.SourceName
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
This is how I do for custom filter selection. May be slower due to double looping.
Dim toSelect(1 To 3) As String
toSelect(1) = "item1"
toSelect(2) = "item2"
toSelect(3) = "item3"
For Each pvItem In objField.PivotItems
For Each st In toSelect
If pvItem.Value = st Then
pvItem.Visible = True
Exit For
Else
pvItem.Visible = False
End If
Next
Next
Well.
Because you have not how to hide all, because, always you need to have 1 item visible
I do this:
I start hiding the first field, and before go to the next, i show all fields i need visible, then, i go to the secont item, and hide, and again show all items i want, and so on. Then, always will be visible any field, and wont have error.
After the loop, i again try to show all fields i want.
With ActiveSheet.PivotTables("TablaD2").PivotFields("Entity")
Dim i As Long
For i = 1 To .PivotItems.Count
.PivotItems(i).Visible = False
.PivotItems("ARG").Visible = True
.PivotItems("BRL").Visible = True
.PivotItems("GCB").Visible = True
.PivotItems("MEX").Visible = True
Next
.PivotItems("ARG").Visible = True
.PivotItems("BRL").Visible = True
.PivotItems("GCB").Visible = True
.PivotItems("MEX").Visible = True
End With