Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 08-10-2020, 07:21 PM
Peterson Peterson is offline Speed up macro that calls Excel-based function Windows 10 Speed up macro that calls Excel-based function Office 2019
Competent Performer
Speed up macro that calls Excel-based function
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default Speed up macro that calls Excel-based function

I've cobbled together a macro that loops through all of the comments in a file and modifies each with text strings from an Excel file, which the macro obtains via a function I found on the Web.

The macro is running very slowly, presumably because the function is opening/closing Excel each time it obtains another string?? But when I mess with the function, I either get errors, or I end up with the Excel file being locked for editing after running the macro.


Can you offer some guidance on the best way to approach this?





Code:
Sub Comments_AddResponsesToAll_6() ' 08/10/2020
' This macro loops through all comments in a file and adds text,
' sourced from an Excel file, to the beginning of the comment. 

    Dim rngNewComment As Range
    Dim rngComment As Range
    
    Dim lngCommentNum As Long
    Dim lngExcelID_RowNum As Long
      
    ' Initially set the Excel ID row number; these numbers do NOT correlate
    ' with the comment numbers in the Word files:
    lngExcelID_RowNum = 2
      
    For lngCommentNum = 1 To (ActiveDocument.Comments.Count)
                     
        Set rngComment = ActiveDocument.Comments(lngCommentNum).Range
        
        rngComment.Collapse Direction:=wdCollapseStart
        
        rngComment.Text = Read_Excel_Cell(lngExcelID_RowNum) & vbCr
                
        'Increment the Excel row number:
        lngExcelID_RowNum = lngExcelID_RowNum + 1
    Next
  
End Sub

Function Read_Excel_Cell(cellRin As Long) As String
' https://stackoverflow.com/questions/47486565/insert-text-from-an-excel-file-into-a-word-document-word-vba

    Dim objExcel As Excel.Application
    Dim myWB As Excel.Workbook

    Set objExcel = New Excel.Application
    Set myWB = objExcel.Workbooks.Open("C:\Temp\TestFile.xlsx")
    ' Below, the first number here -- ".Cells(cellRin, 15)" -- is the row number, the second is the column number:
    Read_Excel_Cell = myWB.Sheets(1).Cells(cellRin, 15)
    myWB.Close
    Set myWB = Nothing
    Set objExcel = Nothing

End Function
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Concurrent Calls in Excel sbcccc Excel 1 08-15-2018 12:24 AM
Is there a way to search all workbooks for speciic function calls? Jennifer Murphy Excel Programming 4 07-01-2018 03:55 AM
Rest api calls in excel Santhosh_84 Excel Programming 0 06-24-2016 10:31 AM
Speed up macro that calls Excel-based function Need to improve speed of Excel macro that searches for whole word matches Marrick13 Excel Programming 20 02-19-2016 09:54 AM
how to speed up macro AC PORTA VIA Excel 3 10-30-2015 08:58 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:45 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft