Writing values to Textboxes on Chart Sheets - vba

Looking for some guidance. I have created a statistics chart but also it lists the Top 5 WIP for certain categories.
the trouble I'm having is writing to text boxes on a chart. I have managed to do it with standard text boxes found on the insert tab but not with the ActiveX boxes which is what I need due to the need of scroll bars
Here is the function I have written to write strings to all text boxes.
Function Top5HeldCell(aSheet)
Dim row As Integer
Dim commentBox As TextBox
row = 12
For i = 4 To GetNumberOfEntries(aSheet, "A")
If Sheets(aSheet).Range("S" & i).Value <> "" Then
If Sheets(aSheet).Range("T" & i).Value = "" Then
rmNumber = Sheets(aSheet).Range("A" & i).Value
partNumber = Sheets(aSheet).Range("B" & i).Value
serialNumber = Sheets(aSheet).Range("C" & i).Value
ncNumber = Sheets(aSheet).Range("D" & i).Value
currentWIP = Sheets(aSheet).Range("W" & i).Value
Comments = Sheets(aSheet).Range("I" & i).Value
If row <= 16 Then
ActiveChart.Shapes.Range(Array("rmNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = rmNumber
ActiveChart.Shapes.Range(Array("partNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = partNumber
ActiveChart.Shapes.Range(Array("serialNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = serialNumber
ActiveChart.Shapes.Range(Array("ncNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = ncNumber
ActiveChart.Shapes.Range(Array("WIPTime" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = currentWIP
' This works using standard textboxes
'ActiveChart.Shapes.Range(Array("comment" & row)).Select
'Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = Comments
' here I'm trying to use ActiveX boxes
Charts("Statistics Chart").OLEObjects("comment" & row).Object.Text = ""
row = row + 1
End If
End If
End If
Next i
End Function
I seem to be getting the error 'Unable to get OLEObjects property of the Chart Class.
From using my nogging I expect this is something to do with the Charts not supporting this feature. Is there a work around that is known or am I really stuck???
Cheers

Related

VBA to generate a Single PDF with multiple pages instead of separate PDF files for each rows in excel

I have the following Code that generates separate PDF Files for each rows of my excel sheet.
My goal is to generate a Single PDF file having a number of pages instead of seperate PDF Files.
Can any kind soul help me with this?
Option Explicit
Sub Create_PDF_Files()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim dsh As Worksheet
Dim tsh As Worksheet
Dim setting_Sh As Worksheet
Set dsh = ThisWorkbook.Sheets("Data")
Set tsh = ThisWorkbook.Sheets("Letter Template")
Set setting_Sh = ThisWorkbook.Sheets("Settings")
Application.DisplayStatusBar = True
Application.StatusBar = ""
Dim i As Integer
Dim File_Name As String
For i = 2 To dsh.Range("A" & Application.Rows.Count).End(xlUp).Row
Application.StatusBar = i - 1 & "/" & dsh.Range("A" & Application.Rows.Count).End(xlUp).Row - 1
tsh.Range("C2").Value = dsh.Range("A" & i).Value
tsh.Range("C3").Value = dsh.Range("D" & i).Value
tsh.Range("C5").Value = dsh.Range("E" & i).Value
tsh.Range("C6").Value = dsh.Range("F" & i).Value
tsh.Range("C7").Value = dsh.Range("G" & i).Value
tsh.Range("C8").Value = dsh.Range("H" & i).Value
tsh.Range("C9").Value = dsh.Range("I" & i).Value
tsh.Range("C10").Value = dsh.Range("J" & i).Value
tsh.Range("D12").Value = dsh.Range("K" & i).Value
File_Name = dsh.Range("B" & i).Value & "_" & dsh.Range("C" & i).Value & "_" & dsh.Range("K" & i).Value & ".pdf"
tsh.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Sheets("Settings").Range("F4").Value & "\" & File_Name
Next i
Application.StatusBar = ""
MsgBox "PDF files generated successfully."
End Sub
The line starting tsh.ExportAsFixedFormat... is inside the For... next loop. This is why it is generating a new PDF for each file.
There are three approaches you could try:
Move that line below the next statement, and adjust your code so that your accumulating variable is fed into an overall variable, which you then export.
Run it like you are now, but add a step at the end to combine all the generated files into one PDF.
Find a different export function that generates a PDF page rather than file, and can aggregate at the end.

VBA does not catch SAP list's row

I am working on SAP Gui Scripting with VBA. I have 200line of information to enter into SAP from excel. However, SAP list hits 23lines max, which you have to than scroll down to enter more lines into the list. Here is my code
i = 0
With session
.findById("wnd[0]").maximize
.findById("wnd[0]/tbar[0]/okcd").Text = "MIGO"
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_HEADER:SAPLMIGO:0101/subSUB_HEADER:SAPLMIGO:0100/tabsTS_GOHEAD/tabpOK_GOHEAD_GENERAL/ssubSUB_TS_GOHEAD_GENERAL:SAPLMIGO:0112/txtGOHEAD-BKTXT").Text = "PKG QTY REF 015835"
While Cells(7 + i, 1).Value <> ""
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-MAKTX[1," & i & "]").Text = Cells(7 + i, 2)
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/txtGOITEM-ERFMG[4," & i & "]").Text = Cells(7 + i, 4)
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-ERFME[5," & i & "]").Text = "PC"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-LGOBE[6," & i & "]").Text = "BORD"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-NAME1[12," & i & "]").Text = "2S98"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMLGOBE[27," & i & "]").Text = "DMDV"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMBAR[32," & i & "]").Text = "CATNEW"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMBAR[32," & i & "]").SetFocus
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMBAR[32," & i & "]").caretPosition = 6
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM").verticalScrollbar.Position = i
.findById("wnd[0]").sendVKey 0
i = i + 1
Wend
End With
I tried using
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM").verticalScrollbar.Position = i
to scroll down the list but apparently VBA still stops at line 23. Is there another for all my 200lines to be added inside the list?
Here is a picture of how it looks like when my code ran, There are lines below 23 but vba still stops and doesnt continue. Any help?
Hi Have you tried manually entering data above 23 lines? Are these lines are disabled after 23rd row?
Below should do the job. I usually scroll and set my last row on top. BTW Which t-code are you using?
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM").verticalScrollbar.Position = i

Userform listbox populate with range

Is there any way I can populate list box or other feature in user form by range of cells?
I would like to put each of my selected columns into 1 list box like in:
.
For example A2:U100 without creating new list box for every column?
Right now I do it like:
ListBox1.List = Application.Worksheets("Můj_Ranking").Range("B2:B" & lastRw).Value
ListBox2.List = Application.Worksheets("Můj_Ranking").Range("C2:C" & lastRw).Value
ListBox3.List = Application.Worksheets("Můj_Ranking").Range("D2:D" & lastRw).Value
ListBox4.List = Application.Worksheets("Můj_Ranking").Range("E2:E" & lastRw).Value
ListBox5.List = Application.Worksheets("Můj_Ranking").Range("F2:F" & lastRw).Value
ListBox6.List = Application.Worksheets("Můj_Ranking").Range("G2:G" & lastRw).Value
ListBox7.List = Application.Worksheets("Můj_Ranking").Range("H2:H" & lastRw).Value
ListBox8.List = Application.Worksheets("Můj_Ranking").Range("I2:I" & lastRw).Value
ListBox9.List = Application.Worksheets("Můj_Ranking").Range("J2:J" & lastRw).Value
ListBox10.List = Application.Worksheets("Můj_Ranking").Range("K2:K" & lastRw).Value
ListBox11.List = Application.Worksheets("Můj_Ranking").Range("L2:L" & lastRw).Value
So you indeed want a ListBox with multiple columns, something like this should help :
With ListBox1
.ColumnCount = 11
.ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50"
.ColumnHeads = False
.RowSource = "=Můj_Ranking!B2:L" & LastRw
.MultiSelect = fmMultiSelectMulti
End With
Or how to loop through controls :
For i = 1 To 11
With Application.Worksheets("Můj_Ranking")
Controls("ListBox" & i).List = .Range(ColLet(i) & "2:" & ColLet(i) & lastRw).Value
End With
Next i
And you also have .RowSource property for most of the controls! ;)
And the function to get the letters for columns :
Public Function ColLet(x As Integer) As String
With ActiveSheet.Columns(x)
ColLet = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function
haven't tested for Listbox but here is how I fill a Combobox with the result of a recordset
Function Fill_Combobox(ByRef cbo As ComboBox, ByVal rs As ADODB.Recordset, ByVal colWidth As String)
Dim aryColumnWidth() As String
Dim i As Integer
aryColumnWidth = Split(colWidth, ";")
cbo.Clear
cbo.ColumnCount = UBound(aryColumnWidth) + 1
cbo.ColumnHeads = False
cbo.ColumnWidths = colWidth
Do Until rs.EOF
With cbo
.AddItem
For i = 0 To UBound(aryColumnWidth)
.List(.ListCount - 1, i) = rs.Fields(i)
Next
End With
rs.MoveNext
Loop
End Function
it should work similar for a listbox. Call the AddItem Method to add an new entry to the listbox and then fill it by accessing the List element
Assuming I'm reading your question right, this should add a single line with the number of columns you have in a single ListBox.
for i = 2 to lstRw
With ListBox1
.AddItem Application.Worksheets("Můj_Ranking").Range("B" & i).value
.List(.ListCount - 1 ,1) = Application.Worksheets("Můj_Ranking").Range("C" & i).Value
.List(.ListCount - 1 ,2) = Application.Worksheets("Můj_Ranking").Range("D" & i).Value
'And so on for each column
.List(.ListCount - 1 ,10) = Application.Worksheets("Můj_Ranking").Range("L" & i).Value
End With
next i
Remember to edit the column count properties of the ListBox control on your userform of it won't work :)

Looping through cells, building a range in VBA

I am looking to loop through cells and build a range for a graph. My main issue is that I cannot figure out how to incorporate the 'i' into the range. Example:
Dim name As String
Dim newChart as Chart
Dim i as Integer
Set newChart = Charts.add
For i = 1 To 20
accName = Range("C" & i).Value 'I understand why this works.
With newChart
.ChartType = xlColumnClustered
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = accName
.SeriesCollection(1).Values = wb.Worksheets("Summary-Account").Range("E&i:G&i, I&i:K&i, M&i:O&i, Q&i:S&i") 'How can I get this to work?
.SeriesCollection(1).XValues = wb.Worksheets("Summary-Account").Range("E3:G3, I3:K3, M3:O3, Q3:S3")
End With
i = i + 1
Loop
You don't increment manually i.
The function for loop already does it for you.
So remove that i = i + 1.
Now, to make your Range work, you would need its "" to be something like this : Range("E" & i & ":G" & i). I don't quite understand what you are trying to achieve with trying to take data from 6 different columns.. See range for further information.
You probably need to have:
.SeriesCollection(1).Values = wb.Worksheets("Summary-Account").Range("E" & i & ":G" & i & ", I" & i & ":K" & i & ", M" & i & ":O" & i & ", Q" & i & ":S" & i)
Not the clearest line ever, but should give you the pattern to go by. There are other problems like needing a "Next i" instead of a "Loop" and no i=i+1, but on my computer this gave me a graph with multiple series on it.
Try
.SeriesCollection(1).Values = Range("E1").Resize(20,1).Value
this will copy all the values into an array for use in series collection
use Next i instead of Loop; and get rid of that i = i+1.
Also, use "E" & i & ":G" & i instead of "E&i:G&i"
One step further, in vba
Dim i as double
For i = 1 to 100 Step 10 '<= Step 10 means when updating i, i = i + 10, and default value would be 1
Loop '<=breaks here, compile error is expected.
this is not even valid in syntax, the keyword Loop is associated with another keyword Do, and For is associated with Next

Put variable in UserForm name

I have following code in a subfunction:
If Auswertung.Controls("T" & Te & "F" & Fr & "Option1").Value = False Then
falseSelection = falseSelection + 1
end If
If Auswertung2.Controls("T" & Te & "F" & Fr & "Option1").Value = False Then
falseSelection = falseSelection + 1
end If
There are 15 UserForms, so I need to use a variable for the UserForm name.
"Auswertung" & X & .Controls("T" & Te & "F" & Fr & "Option1").Value
(This is wrong, but I do not know which capabilites VBA has)
Is there a way to accomplish this?
Access VBA
This is how you loop through all the forms of a project. In your case you can modify it to be stored in a variable and then run a check on .Name to see if it matches the form you need
Dim f
For Each f In CurrentProject.AllForms
Debug.Print f.Name
Next
OR you can simply refer to a form by its name in the following way
Forms.Item("Auswertung" & X).Controls
Excel VBA
Dim f As Object
For Each f In VBA.UserForms
Debug.Print f.Name
Next