提问人:Math869 提问时间:11/15/2023 最后编辑:mklement0Math869 更新时间:11/15/2023 访问量:58
Excel VBA一直在从我的文本框中删除信息,而不是从excel中插入信息
Excel VBA has been removing information from my text box instead of insert information from excel
问:
当我在下面执行我的宏时,我的宏会清除 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
我从没有知识的基本代码开始,但有人好帮我创建了循环,而不是从每个单元格复制和粘贴代码。我执行了它并且它工作正常,但我对我的文件进行了一些更改,包括其他信息(与宏无关),现在当我单击底部执行它时,它不会自动打开,而且我必须单击 F5 开始该过程。
答: 暂无答案
评论
Set xlWorkbook = xlApp.Workbooks("workbook.name")
GetObject
Set xlWorkbook
ppTextBox.Text = excelRange.Address(1,1,xlA1,True)