This is my first question, so sorry for terminology.
I'm beginner in VBA, so I'm stopped in few questions.
I'm working in Powerpoint. I have combobox and I want to add items from table (it could be as table, or Excel spreadSheet) which is on previous slide.
I found one example for Excel (I don't know if will work in PPT):
Sub Loadbox()
row_review = 1
Dim TheSheet As ?????
Set TheSheet = ?????
Do
DoEvents
row_review = row_review + 1
item_in_review = TheSheet.Range("A" & row_review)
If Len(item_inreview) > 0 Then ComboBox1.AddItem (item_in_review)
Loop Until item_in_review = ""
End Sub
But I couldn't understand how to define table from witch I get data for item.
Maybe there is better way how to do it?
To read the contents of the first cells of a row in a PowerPoint table this is the starting point.
Dim tbl As Table
Dim i As Long
Set tbl = ActivePresentation.Slides(1).Shapes(2).Table
For i = 1 To tbl.Rows.Count
Debug.Print tbl.Cell(i, 1).Shape.TextFrame2.TextRange.Text
Next
Related
I would like to create a macro which will change the "Vertical Alignment" to "Middle" of the selected rows/cells in a PowerPoint table. Can anyone pls help me with this.
Below example snapshot attached.
Below is the code. My code is perfectly working with the shape but could't work for the tables. pls assist.
ActiveWindow.Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
To approach a table you can not use the Shape Object, but need to use Tables.
You can also format only Cell by Cell so you need to run a loop through all Rows and Columns
-----edited-----
To use the selected cells you have to iterate through all cells and see if they are selected
Sub SelectedCells()
Dim oTbl As table
Dim iIdx As Integer
Dim iIdy As Integer
Set oTbl = ActiveWindow.Selection.ShapeRange(1).table
For iIdx = 1 To oTbl.Rows.Count
For iIdy = 1 To oTbl.Columns.Count
If oTbl.Cell(iIdx, iIdy).Selected Then
With oTbl.Cell(iIdx, iIdy).Shape
.TextFrame.VerticalAnchor = msoAnchorTop
End With
End If
Next
Next
End Sub
I've been asked to create a MS access database that catalogs all the data that is stored in MS Word Files. The Word files have tables that contain the data.
For example, "Customer Name:" | Customer data | "Date" | Date data
I have used MS Access to loop and open each file using objects Word.Application and Word.Document .
Then I use a 2nd loop on i using this command to get the value in the cell: worddoc.Tables(tableindex).Range.Cells(i).Range.Value
HOWEVER if the cell contains a dropdown box, I do not get the value in the dropdown box; I get a square character.
1) Is there a way to determine what type of data is in the cell? Most of the time it would be Text, Textbox, or Dropdown box.
2) When it is a Dropdown box, how do I get the data from it?
I hope I provided enough information. please ask if you need more info.
Maybe something like this (for example - assuming you have "content controls" and not some other type of control...)
Sub Tester()
Dim t As Table, r, c, rw, cel As Cell, cc
Set t = ActiveDocument.Tables(1)
For r = 1 To t.Rows.Count
Set rw = t.Rows(r)
For c = 1 To rw.Cells.Count
Set cel = rw.Cells(c)
Debug.Print r, c, GetContent(cel)
Next c
Next r
End Sub
Function GetContent(c As Cell)
Dim cc, con, sep
Set cc = c.Range.ContentControls
If cc.Count = 0 Then
'stripping off "end of cell" marker...
GetContent = Right(c.Range.Text, Len(c.Range.Text) - 2)
Else
For Each con In cc
GetContent = GetContent & sep & con.Range.Text
sep = " "
Next con
End If
End Function
The little square box you are getting is the end of cell marker. In a table cell, it functions kind of like a last paragraph. So ... you have to get rid of it before you do anything else.
The following is some example test code, just looking at one cell in a table, but use it as a guide to fit into your process of looping thru the various cells. My other caveat is I took your reference to a "Textbook" literally in this example. If what you meant was a text box content control, then you can ignore that portion of this example.
Dim rng As Range, cc As ContentControl, shp As Shape
Set rng = ActiveDocument.Tables(1).rows(1).Cells(1).Range
rng.MoveEnd wdCharacter, -1
If rng.ContentControls.Count > 0 Then
Set cc = rng.ContentControls(1)
Debug.Print cc.Range.Text
ElseIf rng.ShapeRange.Count > 0 Then
If rng.ShapeRange(1).Type = msoTextBox Then
Set shp = rng.ShapeRange(1)
If shp.TextFrame.HasText Then
Debug.Print shp.TextFrame.TextRange.Text
End If
End If
Else
If Not rng.Text = vbNullString Then
Debug.Print rng.Text
End If
End If
I want to create a macro that will generate N number of template sheets based on the value of a cel. For example, User inputs 4 into this particular cell and it subsequently generates 4 new sheets in the workbook of this template.
I've searched all through Stack overflow for a question that matches mine but none do. The closest I found was this and although the inital headline question asks generally the same question, when going into detail the user who asked this changes their question to"insert number of cells based on a cell value". Still I used this as a starting point.
Sub CreateSheets()
Dim facilitiesNum As Integer
facilitiesNum = Range("B2").Value
sheetsNeeded = facilitiesNum
With ThisWorkbook.Sheets
For i = sheetsNeeded To Master.Range("B2").Value2
.Item("TemplateSheet").Copy After:=.Item(.Count)
.Item(.Count).Name = sheetsNeeded
Next
End With
End Sub
I am new to VBA so I could be very off syntax-wise but in pseudocode my goal is
numberOfTemplates = cell value
numSheetsNeeded = numberOfTemplates
For i = numSheetsNeeded To NumOfTemp:
create sheets using numSheetsNeeded as reference for how many need to be
generated
How do I go about doing this?
If you just want to add new sheets this should be enough
Sub CreateSheets()
Dim facilitiesNum As Long
facilitiesNum = Range("B2").Value
With ThisWorkbook.Sheets
For i = 1 To facilitiesNum
.Item("TemplateSheet").Copy After:=.Item(.Count)
.Item(.Count).Name = i
Next i
End With
end sub
I am using VBA to help manage a set of data. I will have Monthly Data for 50 months and I wish to categorize it into different sheets based on the FIRST word within a cell. Here is what I done so far;
I created a workbook with 2 sheets,
Sheet1(Employee Inventory)
Sheet2(PB)
and my code is written and saved in this Workbook.
Sub myCode()
Dim OldString As String
Dim NewString As String
Set i = Sheets("Employee Inventory")
Set PB = Sheets("PB")
Dim counterPB
counterPB = 2
Dim d
Dim j
d = 1
j = 2
Do Until IsEmpty(i.Range("D" & j))
OldString = i.Range("D" & j)
NewString = Left(OldString, 2)
If NewString = "PB" Then
i.Rows(j).EntireRow.Copy
PB.Range("A" & counterPB).Select
PB.Paste
counterPB = counterPB + 1
End If
j = j + 1
Loop
End Sub
Apologies for the code as it looks weird. This code looks at Sheet1 and scans column "D" and looks for the first word starting with "PB". Once it does find it, it will copy and paste the whole row into another sheet called Sheet2(PB).
When I am in Microsoft Visual Basic window AND I have the Excel Spreadsheet with Sheet1(Employee Inventory) tab opened and when I click Run Sub I get the following error: Run-time error '1004': Application-defined or object-defined error. When I click on "PB" tab, nothing is being copy and pasted in there.
HOWEVER, when I click on the PB tab and then I click Run Sub, the codes executes and any rows containing the first word "PB" will be copied and pasted in the "PB" tab.
My question is, why does it only work when I have the Sheet2 opened and not when I have Sheet1 Opened?
when use range.select its parent worksheet must be selected, so we can use PB.Activate or not use .select at all.
Try to replace this:
i.Rows(j).EntireRow.Copy
PB.Range("A" & counterPB).Select
PB.Paste
with this line:
i.Rows(j).Copy PB.Rows(counterPB)
Why don't you just select the second sheet at the beginning of the code?
Try the following
ActiveWorkbook.Sheets("Sheet2").Activate
If it really works when this sheet is selected, then it should work with this.
Thanks for the response!
however the code which you sent started from the beginning of the workbook. but it should not be.
For example, a workbook contains sheets (only user knows the sheet name, so we can't give the sheet name on the code) as following
Summary/Version/AA/BB/CC/DD/Final
If beginsheet=AA then the code should start collecting data from AA and if endsheet = DD then the code should stop collecting data at Final. Only till DD is valid.
So how can we achieve this.
You can do a FOR - EACH: For each theSheets in ActiveWorkbook.Sheets
For each theSheets in ActiveWorkbook.Sheets
Sheets(theSheets.Name).Select
msgbox(theSheets.Name)
next
I put the MSGBOX just to show the name of the sheet. Also the Select is not necessary I put it there just to show how it changes from sheet to sheet.
If you don't want to search all sheets (each sheet) but a specified number of sheets, you can assign the sheets names to a collection and the iterate that collection; in that case you have to know the names of the sheets.
sorry I did not get your first question...
Well, I don't know how practical this solution will be for you purpose but you could do the following:
When you open the workbook create CheckBoxes with the names of the Sheets:
Private Sub Workbook_Open()
Dim row As Integer
row = 14
For Each mysheets In ActiveWorkbook.Sheets
ActiveSheet.CheckBoxes.Add(20, row, 50, 20).Select
With Selection
.Caption = mysheets.Name
.Value = xlOff
.LinkedCell = "C" & ToRow
.Display3DShading = False
End With
row = row + 50
Next
End Sub
Have the users select the Sheets that apply.
Fill a collection with the "Text" of the selected CheckBoxes (which will be the names of the sheets)
Iterate through the sheets in the collection
Dim allSelectedSheets As New Collection
Public Sub FindSelectedCkBox()
For Each ckbox In ActiveSheet.CheckBoxes
If ckbox.Value > 0 Then
allSelectedSheets.Add ckbox.Text
End If
Next
iterateThroughSheets
End Sub
Sub iterateThroughSheets()
For Each theSheets In allSelectedSheets
ActiveWorkbook.Sheets(theSheets).Select
Next
End Sub
Make sure to remove the CheckBoxes when opening the Book so you don't end up with duplicates.