如何自动化vb脚本

如何自动化vb脚本

我有这个代码:

Sub NewYearData()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim r1 As Range, r2 As Range
    Dim addy As String, sh As Worksheet
    Dim shName As String
    
    Set wb1 = Workbooks("PreviousYear.xlsm")
    Set wb2 = Workbooks("CurrentYear2.xlsx")
    addy = "C16:AH18"
    
    For Each sh In wb1.Sheets
        shName = sh.Name
        Set r1 = sh.Range(addy)
        Set r2 = wb2.Sheets(shName).Range(addy)
        r1.Copy r2
    Next sh
    
End Sub

这是我必须复制的单元格的固定位置。在某些工作表中,位置不同,因此我必须找到所有引用外部文件和工作表的单元格,例如: 'D:\Data\[Data_1990-2019.xlsx]Set01'!D105

问题:如何找到所有包含引用的单元格,并将这些单元格复制到新文件?

答案1

以下代码是从文章中复制的
如何在 Excel 中查找并列出所有链接(外部引用)?

For Each xSheet In Worksheets
    Set xRg = xSheet.UsedRange.SpecialCells(xlCellTypeFormulas)
    If xRg Is Nothing Then GoTo LblNext
    For Each xCell In xRg
        If InStr(1, xCell.Formula, "[") > 0 Then
            ...
       End If
    Next
LblNext:

您可以调整上述InStr调用以满足您的需要。

相关内容