Excel VBA一直在从我的文本框中删除信息,而不是从excel中插入信息

Excel VBA has been removing information from my text box instead of insert information from excel

提问人:Math869 提问时间:11/15/2023 最后编辑:mklement0Math869 更新时间:11/15/2023 访问量:58

问:

当我在下面执行我的宏时,我的宏会清除 PowerPoint 中的信息,而不是我的 PowerPoint 从 Excel 中的特定单元格接收和过去的信息到 PowerPoint。

Sub PasteExcelDataIntoPowerPointTextbox()
    Dim ppApp As Object
    Dim ppSlide As Object
    Dim ppTextBox As Object
    Dim xlApp As Excel.Application
    Dim xlWorkbook As Excel.Workbook
    Dim xlWorksheet As Excel.Worksheet
    Dim excelRange As Excel.Range
    
    ' Initialize PowerPoint and Excel
    Set ppApp = CreateObject("PowerPoint.Application")
    ppApp.Visible = True ' Make PowerPoint visible
       
    ' Open the PowerPoint presentation
    ' Set ppPresentation = ppApp.Presentations.Open("C:\Users\Public\HiringResultsNew.pptx")
    
    ' If both Excel and PPT are in the same folder
    Set ppPresentation = ppApp.Presentations.Open(ThisWorkbook.Path & "\HiringResultsNew.pptx")   
    ' Assuming the Excel file is already open, else you can open it too
    Set xlApp = GetObject(, "Excel.Application")
    Set xlWorkbook = xlApp.ActiveWorkbook
    Set xlWorksheet = xlWorkbook.Worksheets("HiringResults") ' Change to your sheet name
    
    ' Get the range of Excel data you want to copy
    Dim arrCell, arrShp, i As Long
    arrCell = Array("D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", "D16", "D17", "D18", "D19", "D20", "D21")
    arrShp = Array("REFTYPE", "REFBUSINESS", "REFNUMBERFILLS", "REFVARIATION", "REFTTF", "REFCNPS", "REFHMNPS", "REFACTREQ", "REFDIVMALE", "REFDIVFAME", "REFHIREINT", "REFHIREEXT", "REFTSTASO", "REFTSEMRE", "REFTSAGENC", "REFLEVEX", "REFLEVDI", "REFLEVMA", "REFLEVIN", "REFDATAREF", "REFKEYINSIGHTS")

    Set ppSlide = ppPresentation.Slides(1)
    For i = LBound(arrCell) To UBound(arrCell)
        Set excelRange = xlWorksheet.Range(arrCell(i))
        Set ppTextBox = ppSlide.Shapes(arrShp(i)).TextFrame.TextRange
        ppTextBox.Text = excelRange.Text
    Next
       
    ' Clean up
    Set ppApp = Nothing
    Set xlApp = Nothing
    Set xlWorkbook = Nothing
    Set xlWorksheet = Nothing
    Set ppPresentation = Nothing
    
    MsgBox "Report completed. Please edit and save it."
 
End Sub

原文: 将信息从 Excel 转换为 ppt 特定字段

我从没有知识的基本代码开始,但有人好帮我创建了循环,而不是从每个单元格复制和粘贴代码。我执行了它并且它工作正常,但我对我的文件进行了一些更改,包括其他信息(与宏无关),现在当我单击底部执行它时,它不会自动打开,而且我必须单击 F5 开始该过程。

Excel VBA PowerPoint

评论

0赞 Black cat 11/15/2023
尝试将工作簿分配替换为工作簿的名称。 因为它显示在窗口标题中。Set xlWorkbook = xlApp.Workbooks("workbook.name")
0赞 Math869 11/15/2023
我的循环工作正常,但他们没有从我的 excel 文件中收集信息,错误是一样的:它用消息框完成报告,但 powerpoint 文件中的内容为零。我到底在哪里可以更改您提供的信息?我之所以问你,是因为我以前没有使用 VBA 的经验。你能告诉我我必须更改的代码吗?
0赞 Black cat 11/15/2023
之后,以 开头的行GetObjectSet xlWorkbook
0赞 CDP1802 11/15/2023
将行更改为以确认正确的工作表和范围。ppTextBox.Text = excelRange.Address(1,1,xlA1,True)
0赞 Math869 11/15/2023
它应该是这样的:设置 xlWorkbook = xlApp.Workbooks(“workbook.HiringResults“),对吗?对我来说,有一个错误表明下标超出了范围。

答: 暂无答案