从Excel单元格到Powerpoint表格的宏

时间:2021-01-12 01:45:44

I'm attempting to take values from Excel cells and populate PowerPoint text tables.

我正在尝试从Excel单元格中获取值并填充PowerPoint文本表格。

Below is what I have thus far leveraging a previous macro, but I am not sure that I am on the right track.

以下是我到目前为止利用之前的宏,但我不确定我是否在正确的轨道上。

Private Sub CommandButton1_Click()

    Dim sourceXL As Excel.Application
    Dim sourceBook As Excel.Workbook
    Dim sourceSheet As Excel.Worksheet
    Dim myPres As Presentation
    Dim row_excel As Integer
    Dim construindo As Integer
    Dim mantidos As Integer
    Dim contador As Integer
    Dim numero As Integer
    Dim chave As Integer


    Set sourceXL = Excel.Application
    Set sourceBook = sourceXL.Workbooks.Open(TextBox1.Value)
    Set sourceSheet = sourceBook.Sheets(4)
    Set myPres = ActivePresentation

    row_excel = Int(TextBox4.Value)
    construindo = Int(TextBox3.Value)
    mantidos = Int(TextBox5.Value)

    chave = 0

    If construindo = myPres.Slides.Count Then
        chave = 1
    End If

    While sourceSheet.Cells(row_excel, 1) <> "" 'loop for row in excel source

        myPres.Slides(construindo).Shapes(3).Table.Cell(2, 1).Shape.TextFrame.TextRange.Text = sourceSheet.Cells(row_excel, 2)
        myPres.Slides(construindo).Shapes(3).Table.Cell(4, 1).Shape.TextFrame.TextRange.Text = sourceSheet.Cells(row_excel, 5)
        ActivePresentation.Slides(construindo).Duplicate
        construindo = construindo + 1

        row_excel = row_excel + 1

    Wend

    numero = myPres.Slides.Count

    If numero > construindo Then
        For contador = 1 To (numero - construindo - mantidos + 1)
            myPres.Slides(construindo).Delete
        Next contador
    End If

    If chave = 1 Then
        myPres.Slides(myPres.Slides.Count).Delete
    End If

    sourceBook.Close

End Sub

Private Sub TextBox1_Change()

End Sub

run time error 13 :Type Mismatch

运行时错误13:类型不匹配

从Excel单元格到Powerpoint表格的宏

2 个解决方案

#1


0  

It seems like TextBox3.Value is empty or has text in it... the function Int() throws an error if you pass a string. You can check the value like this:

看起来TextBox3.Value是空的或者里面有文本...如果你传递一个字符串,函数Int()会抛出一个错误。您可以像这样检查值:

if not isnumeric(TextBox3.Value) then
    MsgBox "You have to enter a number!"
else
    construindo = Int(TextBox3.Value)
end if

#2


0  

Int takes a number as an argument and converts by truncating rather than rounding.

Int将数字作为参数并通过截断而不是舍入来转换。

CInt takes a number or a string that "looks like" a number, so might be preferable here.

CInt采用“看起来像”数字的数字或字符串,因此可能更适合。

And if the passed value may contain non-numeric text, this can help

如果传递的值可能包含非数字文本,这可能会有所帮助

Debug.Print CInt(Val("12345xyz"))

#1


0  

It seems like TextBox3.Value is empty or has text in it... the function Int() throws an error if you pass a string. You can check the value like this:

看起来TextBox3.Value是空的或者里面有文本...如果你传递一个字符串,函数Int()会抛出一个错误。您可以像这样检查值:

if not isnumeric(TextBox3.Value) then
    MsgBox "You have to enter a number!"
else
    construindo = Int(TextBox3.Value)
end if

#2


0  

Int takes a number as an argument and converts by truncating rather than rounding.

Int将数字作为参数并通过截断而不是舍入来转换。

CInt takes a number or a string that "looks like" a number, so might be preferable here.

CInt采用“看起来像”数字的数字或字符串,因此可能更适合。

And if the passed value may contain non-numeric text, this can help

如果传递的值可能包含非数字文本,这可能会有所帮助

Debug.Print CInt(Val("12345xyz"))