我想做一个简单的搜索并替换powerpoint。
我正在尝试使用powerpoint中的VBA宏遍历excel电子表格。 电子表格有两列,约100行。我希望宏循环遍历行,直到它到达一个空单元格。 对于每一行,它都会读取第1列和第2列的值,并将它们写入2维数组。
我使用各种.select语句运行它但是我不喜欢它(选择错误?搜索和替换工作了几次,但经常更改电子表格后,宏总是崩溃)。 我试图以更强大的方式使用更好的性能。
Dim excelDataArray(120, 2) As String
Dim slidedeck As Presentation
Set slidedeck = ActivePresentation
Dim singleslide As Slide
Dim excelFile As Excel.Workbook
Set excelFile = Excel.Application.Workbooks.Open(spreadsheetFolder)
Dim excelSheet As Excel.Worksheet
Set excelSheet = excelFile.Worksheets("Sheet1")
'Loop through each row in Column A until empty row
Dim N As Integer
N = excelSheet.Cells(excelSheet.Rows.Count, "A").End(xlUp).Row
For i = 1 To N
excelDataArray(i, 0) = excelSheet.Cells(i, "A").Value
excelDataArray(i, 1) = excelSheet.Cells(i, "B").Value
Next
答案 0 :(得分:0)
您可以直接将其转储到不带循环的变量数组中。
为了完整性,我已经整理了你的变量。
请在此处更改xl
文件的路径,“C:\ temp \ test.xlsx”
Sub likethis()
Dim slidedeck As Presentation
Dim singleslide As Slide
Dim XLS As Excel.Application
Dim excelFile As Excel.Workbook
Dim excelSheet As Excel.Worksheet
Dim lngROw As Long
Dim X
Set slidedeck = ActivePresentation
Set XLS = New Excel.Application
Set excelFile = XLS.Workbooks.Open("C:\temp\test.xlsx")
Set excelSheet = excelFile.Worksheets("Sheet1")
lngROw = excelSheet.Cells(excelSheet.Rows.Count, "A").End(xlUp).Row
ReDim X(1 To lngROw, 1 To 2)
X = excelSheet.Range("A1:B" & lngROw)
End Sub