Platform : MS Excel 2007(MS Visual Basic 6.0)
I have a few excel file with different textboxes in them.
All the textboxes were name. E.g TxTbox_AAAA, TxtBox_BBBB
Most of them have similar number of Textboxes with same name.
I also required to update the content inside the textboxes. But like i said... some excel file doesn't contain the textboxes.
E.g aaa.xls and bbb.xls have TexTbox_AAAA, TextBox_BBBB
and ccc.xls only have TexTbox_AAAA
my script is something like this
xlApp.ActiveSheet.TextBoxes("TextBox_AAAA").Text = TxtAAAA
xlApp.ActiveSheet.TextBoxes("TextBox_BBBB").Text = TxtBBBB
but if i run, it will encounter run-time error '1004':
Unable to get the TextBoxes property of the Worksheet class
which i suspect it is due to the excel doesn't have this textbox named "TextBox_BBBB"
so how do i put a check before xlApp.ActiveSheet.TextBoxes("TextBox_BBBB").Text = TxtBBBB
to check if this worksheet/activesheet doesn't contain TextBoxes("TextBox_BBBB"), it will not execute this step?
Since a TextBox is a Shape, here is one way to tell if a specific TextBox is on the activesheet:
Public Function IsItThere(sIn As String) As Boolean
IsItThere = False
If ActiveSheet.Shapes.Count = 0 Then Exit Function
For Each s In ActiveSheet.Shapes
If s.Name = sIn Then
IsItThere = True
Exit Function
End If
Next s
End Function
Not sure but try this:
Dim ws as Worksheet
Dim shp as Shape
Set ws = Activesheet '~~> change to suit
For Each shp in ws.Shapes
If shp.Name = "Textbox_Name" Then
'~~> your code here
End If
Next
Hope this helps
Thanks L42 and Gary's Student
I got it.. i should put
For Each s In xlApp.ActiveSheet.Shapes
instead of
For Each s In ActiveSheet.Shapes
only
Thanks for the help!!! =)
Related
I have a DialogSheet (not a userform, this is just the way the sheet was set up nearly 20 years ago) that I have added a combobox to. I am trying to save the value of that combobox to a cell on my sheet but it I get the following error:
Run-time error '438' : Object doesn't support this property or method.
Here is the sub that causes the error:
Sub ModelNameSelection_Change()
Dim ModelName As String
ModelName = DialogSheets("setup").ModelNameSelection.Value
Worksheets("sheet1").Unprotect
Worksheets("sheet1").Cells(1, 10).Value = ModelName
Worksheets("sheet1").Protect
End Sub
The dialog sheet the combobox is on is called "setup" and the combobox has the name ModelNameSelection.
Is there a specific way to refer to the value in the drop down box?
Replace your line:
ModelName = DialogSheets("setup").ModelNameSelection.Value
with:
ModelName = DialogSheets("setup").DropDowns("ModelNameSelection").Value
EDIT: Oops I didn't see #Tim Williams comment which has the same answer
From this post - this should get you started:
Dim sheet As Object ' As DialogSheet?
Set sheet = DialogSheets("setup")
Dim shp As Excel.Shape
For Each shp In sheet.Shapes
If shp.Type = msoFormControl Then
If shp.FormControlType = XlFormControl.xlDropDown Then
'todo
End If
End If
Next
In other words, you access controls through the sheet's Shapes collection. Since you know the exact name of the control, you can retrieve the shape by name instead of iterating them all:
Set shp = sheet.Shapes("ModelNameSelection")
Debug.Assert shp.FormControlType = XlFormControl.xlDropDown
And from there use its ControlFormat property to get the control's Value:
ModelName = shp.ControlFormat.Value
I'm building a chart automation script in powerpoint and i have any issue when calling upon "ActivateChartDataWindow".
I would use "Activate" instead of "ActivateChartDataWindow", but "Activate" loads the full Excel program and makes the whole routine run slow and ulgy.
The problem I have is that "ActivateChartDataWindow" does work to populate the charts, but when I manually go to edit the data - right click, edit data - to access the excel application, it does not seem to want to load!
It has been driving my crazy for the last 5 hours and would appreciate any ideas on how to over come this.
OLE.dlll are working correctly and the code I am using is given below.
Code below:
There are 5 slides with one chart on each page and the code below is what i am using as a point of concept
I have a felling i am using "ActivateChartDataWindow" wrong, but there is not much on the web to know what i am doing wrong! Arrrhhhh!
For i = 1 To 5
Set instance = Nothing
Set instance = ActivePresentation.Slides(i).Shapes(1).Chart.ChartData
With instance
.ActivateChartDataWindow
instance.Workbook.Sheets(1).Range("A1:H26").Value = 27
instance.Workbook.Close
End With
Next i
End Sub
As always recommended, you don't need to Activate an object to modify it. If you're trying to handle a Workbook embedded in a slide, you can do it this way
' This function will get you a Workbook object embedded in a Slide (late binding)
Function getEmbeddedWorkbook(sld As Slide) As Object
Dim shp As Shape
On Error Resume Next
For Each shp In sld.Shapes
If shp.Type = 3 Then ' embedded chart workbook created in PP
Set getEmbeddedWorkbook = shp.Chart.ChartData.Workbook
Exit Function
End If
If shp.Type = 7 Then ' embedded workbook pasted from excel
Set getEmbeddedWorkbook = shp.OLEFormat.Object
Exit Function
End If
Next
End Function
' For Testing, I have 6 slides, Some have a workbook pasted from Excel
' OLE, shape type = 7, others have a chart created in PP (type = 3)
Sub Test()
Dim wb As Object, i As Long
For i = 6 To 6 'ActivePresentation.Slides.Count
Set wb = getEmbeddedWorkbook(ActivePresentation.Slides(i))
If Not wb Is Nothing Then
wb.Sheets(1).Range("A1:D5").Value = i * i
End If
Next
End Sub
I have this Problem for deleting a shape in Excel with vba. Excel gives all the time different Name to These shapes, thatswhy i get error on this line:Here Activesheet is my open sheet and straight connectgor 1 is the Name of the shape, it is set as "Straight Connector 1" but when i reuse the macro it can not be found.
ActiveSheet.Shapes.Range(Array("Straight Connector 1")).Select
Selection.Delete
It can not find the element if i use the macro again. What should i do for this ?
use the activtsheet.shapes collection
Sub connector_delete()
Dim s As Shape
For Each s In ActiveSheet.Shapes
If s.AutoShapeType = msoShapeMixed And s.Name Like "*Connector*" Then
s.Delete
End If
Next s
End Sub
I have created different shapes in excel and have assigned a macro to it which functions as activating another sheet. I want to put all these under one macro and then assign it to different shapes with different linking property. But this code doesn't work because obviously I am doing something stupid. Can someone please help?
Dim shp As ShapeRange, ws As Sheets, i As Integer
Set ws = ActiveWorkbook.Sheets(Array("Introduction", "S1 Fuel Consumption", "S1 Fugitive", "S2 Electricity Consumption"))
Set shp = ws(2).Shapes.Range(Array("Chevron1", "Chevron2"))
Select Case shp(i)
Case shp(1)
ws(1).Activate
Case shp(2)
ws(3).Activate
End Select
End Sub
There is a much easier way to do "buttons" in VBA (I assume this is what your trying to achieve)
First off, in a module, create the "Open Worksheet" code:
Sub Open_Sheet2
Sheets("Sheet2").visible = True
Sheets("Sheet2").Activate
End Sub
Then right click your shape, choose Assign Macro and assign Open_Sheet2 to that shape. Now when it is clicked, it will open Sheet2
I asked a similar question Here and now I need to do the same thing again but this time using VBA in Excel 2010.
Essentially I have numerous text boxes with generic names (i.e. textbox1,textbox2 etc). How can I programically construct the object reference so that I can create a loop?
EDIT:
It is a regular textbox on a worksheet. When I start a sub for this worksheet I can reference the textboxes with the following line:
TextBox1.LinkedCell = "B2"
This is what your after:
Dim oleObj As OLEObject
'Dim oleTxtBox As TextBox
For Each oleObj In Sheet1.OLEObjects
If oleObj.OLEType = xlOLEControl Then
If Mid(oleObj.progID, 1, 14) = "Forms.TextBox." Then
Set oleTxtBox = oleObj.Object
oleTxtBox.PasswordChar = "*"
End If
End If
Next
Just using PasswordChar as an example field from the TextBox object, but it wouldn't actually left me Dim as a TextBox