VBA Powerpoint - Modify category names - vba

I'm trying to modify category names of an existing chart in VBA powerpoint with the below code, but instead the category names are removed from the chart.
Dim mCatArray() As String
lngSeries = 0
Dim testvar As Variant
'Get the existing category names in an Array
'oSh = Shape element
For Each testvar In oSh.Chart.Axes(xlCategory).CategoryNames
strIn = ProperCaps(testvar & "", objRegex)
ReDim Preserve mCatArray(lngSeries)
'Do my changes and insert the category name in the Array
mCatArray(lngSeries) = strIn
lngSeries = lngSeries + 1
Next
'Assign the Changed Category array back - METHOD 1
For lngSeries = 1 To .Chart.SeriesCollection.Count
oSh.Chart.SeriesCollection(lngSeries).XValues = mCatArray
Next
'Assign the Changed Category array back - METHOD 2
oSh.Chart.Axes(xlCategory).CategoryNames = mCatArray
Both the method's don't seem to help. Any help is appreciated.

Related

How to reference similar named textboxes within a loop, with the loop iteration [duplicate]

So I have a table that has a list of totals im trying to display on a form, I have 10 totals I need to get from the totals table and display in 10 textboxes on the form.
The 10 textboxes are "A1, A2, A3..." and its using DLookup to find the ID field number.
It seems like its a syntax issue with Me.TEXTX & X1.Value though I'm not sure how else I can type it.
Hope this makes sense. Thanks!
Private Sub UPDATETOTALS()
Dim FORMX As String
FORMX = "GRID"
Dim TEXTX As String
TEXTX = "A"
Dim TABLENAMEx As String, FINDFIELDx As String, GETFIELDx As String
TABLENAMEx = "GRID_TOTALS"
FINDFIELDx = "[ID]="
GETFIELDx = "TODAY"
Dim X1 As Integer
For X1 = 1 To 10
Me.TEXTX & X1.Value = DLookup(GETFIELDx, TABLENAMEx, FINDFIELDx & X1)
Next X1
End Sub
You cannot access an object reference directly using a concatenated string, as such reference is not of string data type.
Instead, you will need to access the object from the relevant collection (in this case, the Controls collection), by supplying the name of the object (as a string) to the Item method of that collection.
Since the Item method is the default method for a collection, the item name can immediately follow the collection as an argument.
For example:
For X1 = 1 To 10
Me.Controls(TEXTX & X1).Value = DLookup(GETFIELDx, TABLENAMEx, FINDFIELDx & X1)
Next X1

Excel VBA Dictionary Storing and Retrieving

How do you go about creating excel VBA dictionaries?
Say I have the following values:
How can I set column A as the key, column B as the value?
Do I loop through every value to store?
How do I go about using the dictionary afterward to get the value of 5 for instance ("Key A")
In Excel:
=VLOOKUP("D", A:B, 2,FALSE)
returns 20.
In VBA:
MsgBox WorksheetFunction.VLookup("D", Sheet1.Range("A:B"), 2, False)
pops 20.
putting answer here for documentation purposes, from reddit user MRMCMLXXXV
source https://www.reddit.com/r/excel/comments/6u4swi/how_do_you_create_a_dictionary_in_excel_vba_and/
Public Sub DictionaryExamples()
Dim exampleValues As Variant
Dim i As Long
Dim aKey As String
Dim aValue As Integer
Dim exampleDict As Object
'Load values into a variant array
exampleValues = Range("A1:B10").Value
'Instantiate a dictionary
Set exampleDict = CreateObject("scripting.dictionary")
'Read all keys and values, and add them to the dictionary
For i = 1 To UBound(exampleValues)
aKey = CStr(exampleValues(i, 1))
aValue = CInt(exampleValues(i, 2))
exampleDict.Add aKey, aValue
Next i
'Output the value associated with key A
MsgBox exampleDict.Item("A")
End Sub
The result looks like this in excel

VBA Array Variant - SubScript out of Range

This is my third day programming in VBA for the first time. I have been taught C programming and Java programming in the past for reference. Making a custom Excel Macro from scratch. Struggling with this error. Have spent hours on it...
Purpose of application is to take data, and move it around between worksheets. This is only part of the code.
Error occurs in the IF-ELSE. Occurs in the else first, so the program has never tried to run the if portion yet.
Note that array1 is declared globally. It wont even let me set the first element to 5 for example. But if I try to change the value in the TempArray120 (which already has data stored) it works fine.
^thinking this is a declaration/instantiation issue
array1(i, 1) = ((TempArray120(i, 1) + TempArray277(i, 1)) / 2) 'getting the avg
^this is the line I am having trouble with
array1(1, 1) = 5
^this line also does not work
Dim array1() As Variant 'declare a array. The lower array determined by current
Dim array2() As Variant 'delcare a array. The upper array determined by current
Sub main()
Call DataFetch("Test", False)
Call DataFetch("Test1", True)
End Sub
Sub DataFetch(sheet As String, LowOrUpper As Boolean)
'Instance Variable Declaration
Dim TempArray120() As Variant 'create and array that will hold 10 values and the current for the 120volts
Dim TempArray277() As Variant 'create and array that will hold 10 values and the current for the 277 volts
TempArray120 = Worksheets(sheet).Range("F12:F2").Value 'read in the InPower from Dim lvl of 0Volts to 10volts #120volts
TempArray120(11, 1) = Worksheets(sheet).Range("K2").Value 'read in the OutCurrent at the 10Volt Dim lvl #120volts
TempArray277 = Worksheets(sheet).Range("F23:F13").Value 'read in the InPower from Dim lvl of 0Volts to 10volts #277volts
TempArray277(11, 1) = Worksheets(sheet).Range("K13").Value 'read in the OutCurrent at the 10Volt Dim lvl #277volts
'i belive the .value is correct for array use
'-------------------------------------------------------------------------------------------------------
'need to average this data and return to a global array. Needs to be the right array. Will check for that.
'LowOrUpper is flase for lower current and true for higher current
If LowOrUpper Then '-if the higher current data
For i = 1 To 11 Step 1
Set array2(i, 1) = ((TempArray120(i, 1).Value + TempArray277(i, 1).Value) / 2) 'set avg value to the global array. Note that this is for the lower array
Next 'end of for loop
Else '-was false and must be the lower current data
For i = 1 To 11 Step 1
array1(i, 1) = ((TempArray120(i, 1) + TempArray277(i, 1)) / 2) 'set avg value to the global array. Note that this is for the lower array
'array1(i, 1) = TempArray120(i, 1)
'this does not work. same error
'array1(1, 1) = 5
'this does not work. same error
'TempArray120(1,1)=5
'^this
Next 'end of for loop
End If
'-------------------------------------------------------------------------------------------------------
Call DataHandler
End Sub
'**********************************
Sub DataHandler()
'Instance Variable Declaration
'-------------------------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------
'paste data into lower and upper curve. The data will the be generated. This is the First generation
Worksheets("Step 1 - Coarse Curve").Range("C7:C18").Value = array1 'setting the data values for Lower Curve.Data is in Array1. This should work 5/18/2017 spent a lot of time on this line
Worksheets("Step 1 - Coarse Curve").Range("K7:K17").Value = array2 'setting the data values for Upper Curve.Data is in Array2
Worksheets("Step 1 - Coarse Curve").Range("B5").Value = array1(11, 1).Value 'setting the current cell for lower
Worksheets("Step 1 - Coarse Curve").Range("J5").Value = array2(11, 1).Value 'setting the current cell for upper
Worksheets("Step 1 - Coarse Curve").Range("F5").Value = Worksheets("Main").Range("B5") 'sets the generated data current to user spec
'-------------------------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------
'handle the data that was just generated => Transfer to the Fine curve
Worksheets("Step 2 - Fine Curve").Range("E3:E13").Value = Worksheets("Step 1 - Coarse Curve").Range("H7:H17").Value 'this is correct
Worksheets("Step 2 - Fine Curve").Range("A102").Value = ID 'insert the ID at the end of data!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!MUST EDIT
Dim fineData As Range 'this will be sent to the CSV file
Set fineData = Worksheets("Step 2 - Fine Curve").Range("B2:B102").Value 'do not believe this needs a .value??????
'-------------------------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------
'Open new file. Make it visiable.
Dim myFile As String 'will hold path name
myFile = Application.DefaultFilePath & "\" & ID & ".csv" 'determine the pathname for new CSV file
Open myFile For Output As #1 'allows the file to be written to. Can now be refered to as #1 as well
'^if the file already exist it will be deleted and a new file will be created with same name
'now write in the array data
Write #1, fineData.Value
Close #1 'gotta close the file
'note the csv file is saved to the root directory of project
'-------------------------------------------------------------------------------------------------------
End Sub
You're off-by-one.
Implicitly sized arrays are 0-based*, unless specified otherwise by Option Base 1.
Given your example saying:
array1(1, 1) = 5 'doesn't work either
The only explanation is that you're assuming your arrays are 1-based, when they're 0-based.
*An array obtained from a Range will be 1-based.
Since you have a mix of 0-based implicitly-sized arrays and 1-based Range arrays in the same module, consider specifying Option Base 1 to unify the array bounds and work with 1-based arrays everywhere in the module, or you'll need to offset (-1/+1) array to cell coordinates.

VB.NET - How do I use a variable in another variables name?

How would I do something similar to this:
Dim amnt As Integer
For x As Integer = 1 To 6
If amnt[x] = 0 Then
btn[x].Enabled = False
End If
Next x
What I mean is, can I reference a variable by using another variable in the name. For example, if "x" were to be 4, then I want to change the button "btn4" to .Enabled = False, but I also want to be able to change the button that I'm changing properties of. So like
If (variablesName & x) = 0 Then
If x is 4 then it should look like this
If variablesName4 = 0 Then
If you mean evaluating a variable by supplying it's name as a string - then this cannot be done in VBA.
For example:
Dim testVar As Integer
Dim v As String
v = "Var"
testVar = 5
Debug.Print test & v '// error
Debug.Print "test" & v '// prints "testVar" as a string
Debug.Print "testVar" '// prints "testVar" as a string
Debug.Print testVar '// prints "5" <~~ only way it can be done
However, for some controls, and some other items - you can access the parent collection and supply a string value to get the correct index. So something like:
For i = 1 To 10
'// Will set checkboxes 1 to 10 to be checked
myForm.Controls("Checkbox" & i).Checked = True
Next
As it stands - it's too broad a question to give a definitive answer, but depending on the kind of objects you are working with you may be able to access a parent collection in this way.

Using selected values in an Excel list box to formulate the legend

I am attempting to pass the selected values from a list box in Excel to legend in a chart. Specifically, I have data of certain companies in the following format
And I also have a list box, globalList, which contains the names of companies that can be selected. Selected companies' data will then be passed onto a chart using VBA.
However, the problems I encounter are in the following sections:
Initialising a variable to hold values selected in the globalList
listMax = globalList.ListCount - 1
`this creates the upper bound for the list box
For i = 0 To (globalList.ListCount - 1)
If globalList.Selected(i) = True Then
companiesSelected = companiesSelected + 1
End If
If i = listMax Then
Exit For
End If
Next i
`the above is used to retrieve the number of companies that have been selected by a user - whether =0 or > 0
Dim myLegend() As String
ReDim myLegend(0 To (globalList.ListCount - 1))
For i = 0 To (globalList.ListCount - 1)
If globalList.Selected(i) = True Then
myLegend(i) = globalList.List(i)
End If
If i = listMax Then
Exit For
End If
Next i
`this is the array object in which I intend to store company names selected in the list box.
The problem is that even though the above creates the myLegend string array, it also contains empty array items for the companies that may not have been selected by the user in the list box.
And even if I am able to remove these empty items from the array, the following problem occurs
Passing the held values from my variable to my chart
For i = 1 To companiesSelected
myChart.SeriesCollection(i).Name = myLegend(i)
Next i
Problem here is that myLegend array starts from 0, while SeriesCollection seems to start from 1. So I am unable to pass the string values for selected items to the legend of my chart's.
Could somebody please point out how to circumvent these problems?
Many thanks in advance!
Here is a code to extract the selected items into an one-based array of String (without empty items):
Dim i As Integer
Dim iCount As Integer
Dim myLegend() As String
iCount = 0
With globalList
ReDim myLegend(1 To .ListCount)
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
iCount = iCount + 1
myLegend(iCount) = .List(i)
End If
Next i
End With
If iCount > 0 Then
ReDim Preserve myLegend(1 To iCount)
Else
ReDim myLegend(1 To 1)
myLegend(1) = "Nothing here!"
End If