从Excel单元格到Powerpoint表格的宏

时间:2013-11-19 00:16:39

标签: excel vba powerpoint-vba

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

以下是我迄今为止利用之前的宏,但我不确定自己是否走在正确的轨道上。

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

运行时错误13:输入不匹配

enter image description here

2 个答案:

答案 0 :(得分:0)

看起来TextBox3.Value为空或者其中包含文本...如果传递字符串,函数Int()会抛出错误。您可以像这样检查值:

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

答案 1 :(得分:0)

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

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

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

Debug.Print CInt(Val(“12345xyz”))