Is it possible to control charts in powerpoint using combo box? - excel

The below image is an example of what i am trying to do. My chart shows the capacity of Resources over month. I choose the month for which i want to view data from the dropdown list. While this is in excel, im trying to do the same in Powerpoint using the charts and AxtiveX controls. Can anyone please guide me on this?
Chart and combobox example

Using a standard Microsoft Forms 2.0 ComboBox control (I do not use ActiveX controls), this is pretty much what you're looking for.
Insert the ComboBox control on the slide where the chart exists. This assumes that the chart data exists in the default ListBox item on the ChartData.Workbook.Worksheets(1) worksheet (i.e., this is what happens when you insert a chart directly in PPT) if you're copying a chart from Excel, this may need revision, but the general idea is the same:
When the user selects the combobox, (ComboBox1_GotFocus queries the chart's underlying data to populate the list. If your data is structured differently, this will need to be modified.
User can make a selection in the ComboBox.
After making the selection, the ComboBox1_Change event will identify the range of data which contains the selected series, and hides the other series, so that only the selected series is visible
Here's my default chart & data which I can view by right-click/Edit Data:
Displaying the slideshow, entering the ComboBox will display the list of series names:
Then, change the selection, and see only the selected series:
Option Explicit
'This code belongs in a SLIDE module in PowerPoint
Private Sub ComboBox1_Change()
'This procedure hides/unhides chart series, based on combobox value
Dim rng As Object 'Excel.Range object
Dim c As Long
With Me.Shapes("Content Placeholder 5").Chart.ChartData '## MODIFY YOUR SHAPE NAME
.Activate
.Workbook.Parent.WindowState = -4140
For c = 2 To .Workbook.Worksheets(1).ListObjects(1).HeaderRowRange.Columns.Count
Set rng = .Workbook.Worksheets(1).ListObjects(1).HeaderRowRange.Cells(c)
rng.EntireColumn.Hidden = (rng.Value <> ComboBox1.Value)
Next
.Workbook.Close
End With
End Sub
Private Sub ComboBox1_GotFocus()
'This procedure sets the list items in the combobox whenever it gets focus
Dim lst As Variant
Dim xlApp As Object
With Me.Shapes("Content Placeholder 5").Chart.ChartData '## MODIFY YOUR SHAPE NAME
.Activate
.Workbook.Parent.WindowState = -4140
Set xlApp = .Workbook.Parent
.Workbook.Worksheets(1).Columns("B:D").Hidden = False
lst = xlApp.Transpose(xlApp.Transpose(.Workbook.Worksheets(1).Range("B1:D1").Value))
.Workbook.Close
End With
ComboBox1.List = lst
End Sub

Related

Can a Textbox have data validation from a table?

I'm pretty strong on Excel but new to VBA.
I've built a set of tables and user forms to maintain records of calls, contacts, etc.
This is my question:
Can I use a textbox on a user form for input and editing a table where that specific textbox has data validation from another table in the workbook?
That is, can the data validation list for that textbox be a table field that can add names, etc. to the data validation?
For an example, on the main contact table I have a user form that has a textbox for the person taking the call.
They should be able to pull their name from a list.
I know I could use a combo box but I don't see how to make the combo box data validation dynamic.
Can I use a field in a different table that workbook to be a dynamic range?
Hope that makes sense...
Thanks in advance for any guidance where to look.
Haven't been able to tie a specific table field into the validation thus making it "dynamic".
Set the rowsource for the Listbox on Form initialization.
Private Sub UserForm_Initialize()
Dim lr As Long
ListBox1.Clear
ListBox1.ColumnHeads = True
With Sheets("Sheet1")
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
If lr > 1 Then
ListBox1.RowSource = .Range("A2").Resize(lr-1).Address
End if
End With
End Sub
or alternatively from a table
Private Sub UserForm_Initialize()
ListBox1.Clear
ListBox1.ColumnHeads = False
With Sheets("Sheet1")
ListBox1.List = .Range("Table1[[#All],[MYCOLUMN]]").Value2
End With
End Sub
Got it...
Works well with a Combobox
Used "intersect" to use only the data in that column...
Dim myTable As ListObject
Dim myArray As Variant
Set myTable = wksISSInfo.ListObjects("tblISSInfo")

Excel VBA: Displaying table data using drop down list

I am a beginner in VBA. I have a worksheet in Excel where tables for each individual month is created to calculate the total amount of manhours for each month. The tables are located in different columns in the worksheet. The data in this table is populated using manually inputted data in respective sheets created for each month.
Currently, this is being done manually and the list is getting very long. I would like to improve on this by creating a dropdown list filter and retrieving the respective data from each month and year and displaying it in the form of a calendar instead. Can anyone guide me on how this can be done?
This is an example of the worksheet I want to compile:
I want it to look something like this where the data for the data will change according to the data I retrieve from the respective months. The months and years will be in the form of drop down list.
The workbook will be updated on a daily basis. The names of the worksheets involved will be named in a format like this: Jul'19, Aug'19, Sept'20, etc. There will be more worksheets created over time.
Please, create a form and place the next code in its module:
Option Explicit
Private Sub UserForm_Initialize()
Dim sh As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
For Each sh In wb.Sheets
Me.cbMonth.AddItem sh.Name
Next sh
End Sub
Private Sub cbMonth_Change()
Dim wb As Workbook, sh As Worksheet, Tbl As ListObject
Set wb = ActiveWorkbook
Set sh = wb.Sheets(Me.cbMonth.Value)
Set Tbl = sh.ListObjects(1)
arr = Tbl.Range.Value
With Me.ListBox1
.ColumnCount = UBound(arr, 2)
.ColumnWidths = "40;25;22;23;22;22;22;48;40"
.list = arr
End With
End Sub
So, you must place a combo box named cbMonth and a list box named 'ListBox1`.
On the form Initialize event the combo box is populated with the sheets name.
On the combo box Change event the 'ListBox1` is populated with the first table range of the selected sheet name. So, in each sheet should exist only one table, or, if more tables, the necessary one must be the first.
You will also be able to input data in the list box, using its DblClick event. The user will be asked about the day where the data to be inputted and then, the sheet can be updated, too...

Trouble assigning a dynamic range to a ListBox control

This should be straightforward but can't get it to work. I have a button on a sheet called Master that should display a userform (named AddApplicant) when clicked. The userform contains a ListBox (named lstCourses) in which I want to display some values from a worksheet called Course Details. The Course Details worksheet is programmatically generated, but this should have no bearing, as I've set the button that launches the user form to only be enabled when this sheet is present.
When I try to click the button that displays the userform I get an application defined or object defined error.
The problem seems to be with the code that loads the range into the ListBox, as the form displays when I comment these lines of code out.
Private Sub UserForm_Initialize()
Dim cell As Range
Dim cellList As Range
Set cellList = Worksheets("Course Details").Range(Range("A2"), Range("A2").End(xlDown))
For Each cell In cellList
lstCourses.AddItem cell.Value
Next cell
End Sub
'Here is the code assigned to the button that launches the form
AddApplicant.Show
Going to clean up a couple things for you on here:
Dim cell As Range, cellList As Range
With Worksheets("Course Details") 'added this as you have range references without sheets
Set cellList = .Range(.Range("A2"), .Range("A2").End(xlDown))
For Each cell In cellList
lstCourses.AddItem cell.Value
Next cell
End With
As this is run from initialize, you would have already loaded the sheet, so no need to .show again.
There should have been a separate button that uses a macro reading like:
sub uf_loader()
AddApplicant.Show
end sub

How to access textBox placed on sheet in VBA module?

I have placed a TextBox control on a sheet in Excel, in a VBA module I want to acces the TextBox and populate with content.
How do I reference the TextBox control?
I've named the TextBox 'tbSQL', in the module I can see Application and can refernece the sheet from the module, the sheet is called 'Database Info.'
I want to write data into the TextBox from the VBA module, but so far I haven't been able to reference it.
I've tried:
Public Const DATABASE_INFO As String = "Database Info."
Dim objDBsheet As Worksheet, objSQL As Range
Dim tbSQL As TextBox
Set objDBsheet = Application.Sheets(DATABASE_INFO)
Set tbSQL = objDBsheet.Shapes("tbSQL")
But this is as far as I get it errors on the Set tbSQL line. The reported error is "Type Mismatch"
I know the control is a TextBox, it was created from the Controlbox toolbar. When looking in the range bar in Excel it displays:
"=EMBED("Forms.TextBox.1","")"
In the Properties box for the TextBox control I have set the (Name) property to tbSQL, however it remains unchanged in the fx text box. It does show as tbSQL in the range box.
Most likely its a bug in Excel. It has nothing to do with Excel-2003.
If you use a Worksheet type variable then Excel fails to discover the control on that sheet in VBA. So if you declare your sheet holding variable as Object / Variant , the code will work fine.
Other alternative is to directly use the Worksheet's CodeName, so if you set the Worksheet's name as wksDBSheet in the VBA IDE's property grid and use that in your code, it will discover the TextBox
Sub test()
Dim objDBsheet As Object 'As Worksheet // Making the 0bjDBSheet type as Object or Variant
'// Allows the discovery of the TextBox on the sheet.
'// Most Likely its a bug.
Dim objSQL As Range
Dim tbSQL As MSForms.TextBox
Set objDBsheet = Application.Worksheets("Database Info.")
Set tbSQL = objDBsheet.tbSQL
tbSQL.Text = "Bug"
'/ Other Alternative is to directly use the CddeName of the sheet.
Set tbSQL = wksDBsheet.tbSQL
tbSQL.Text = "Code Name used"
End Sub
Text Box as shape, or Label as Form Control, or ActiveX control:
Sub f()
Dim tb As Shape, lblControl As Object, lblActiveX As Object
Set tb = Sheet1.Shapes("TextBox 1")
Set lblControl = Sheet1.Shapes("Label 2").OLEFormat.Object
Set lblActiveX = Sheet1.Shapes("Label1").OLEFormat.Object
lblControl.Text = "Form Control"
lblActiveX.Object.Caption = "ActiveX Control"
tb.TextFrame.Characters.Text = "Text Box"
End Sub
You need to use:
Sheets("Sheetname").Shapes("tbSQL").TextFrame.Characters.Text = "Anything you want"
In the end it looks like it is a bug in Excel 2003, I was able to reference the control by using Sheet1.tbSQL instead of going through Application.Sheets.
If you put an ActiveX texbox on a worksheet named "MyTextBox" then you can set it's text in vba like this:
ActiveSheet.MyTextBox.Text = "hi"

Import excel chart with spin button to powerpoint

I have a chart in excel with a spin button that changes a value to change the chart when pressed. I can import them into powerpoint by using insert -> object but this doesn't make the spin button usable.
I'm trying to get it so when showing the presentation I can click the spin button and the chart will correspondingly change, just like it does in excel. Is this possible?
THe spin button is connected to a macro and that's what's causing the chart to change in Excel.
You can copy the code from Excel and place it in a standard module in the Powerpoint file.
You will likely have to make some tweaks -- more or less, depending on how well the original macro was written (e.g., if it is full of ActiveSheet.This and ActiveChart.That then it's going to need a bit more tweaking than if it uses a Chart or ChartObject variable to represent the chart.
Here is an example of interacting with a powerpoint chart:
Sub TEST()
Dim sld As slide
Dim cht As Chart
Dim srs As Series
Set sld = ActivePresentation.Slides(1) 'Modify to be the correct slide #'
'You can hardcode the shape by index if you know it, otherwise'
' this method will apply to the FIRST chart object found on the slide'
For i = 1 To sld.Shapes.count
If sld.Shapes(i).Type = msoChart Then
Set cht = sld.Shapes(i).Chart
Exit For
End If
Next
'use a variable to interact with the Series:'
Set srs = cht.SeriesCollection(1) '<Modify as needed.'
'specify a particular formula for the series:'
'your macro likely changes the chart's source data/formula information'
' so something like this:'
srs.Formula = "=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$5,Sheet1!$B$2:$B$5,1)"
'Or you can display the series formula:'
MsgBox srs.Formula
'Or you can toggle the series axis group:'
With srs
If Not .AxisGroup = xlSecondary Then
.AxisGroup = xlSecondary
Else
.AxisGroup = xlPrimary
End If
'etc.'
'Basically anything you can do to an Excel chart in VBA, you can do in PPT.'
End With
End Sub
When you copy/paste anything from Excel to PowerPoint, you get an OLE object. What appears in PowerPoint is a Windows metafile picture of whatever you copied from Excel. You'd have to either activate the Excel content (doubleclick it within PPT's normal view or give it the appropriate Action Setting to have it activate during a slideshow). Then the spinner should also work within the instance of Excel that launches.
Or you could create another spinner in PPT and have it run code to modify the chart (using David's example code as a basis).

Resources