打开多个文件时如何返回我的主文件

时间:2019-06-17 20:43:59

标签: excel vba

我想检查另一个文件中是否存在某些值,以及是否要更改主文件中的重叠单元格(例如为每个X加1),但不关闭刚打开的另一个文件。

Sub ex12()
Dim StdPr As String
Set myrng = Range("B5:P13")

StdPr = Dir("C:\ex12\*.xls")
Do While StdPr <> ""

Workbooks.Open ("C:\ex12\" & StdPr)
  For Each cl In myrng
      If cl.Value = "X" Then
         ' the missing line #
      End If
  Next
     Workbooks(StdPr).Close savechanges:=False
     StdPr = Dir
Loop


 End Sub

1 个答案:

答案 0 :(得分:0)

类似这样的东西:

Sub ex12()

    Dim StdPr As String, wb as workbook
    dim shtMaster as worksheet

    set shtMaster = thisworkbook.sheets("Master")   'for example

    StdPr = Dir("C:\ex12\*.xls")
    Do While StdPr <> ""

        Set wb = Workbooks.Open("C:\ex12\" & StdPr)
        For Each cl In wb.worksheets(1).Range("B5:P13").Cells
              If cl.Value = "X" Then
                  with shtMaster.Range(cl.address())
                      .value = .value +1
                  End with   
              End If
        Next
        wb.Close savechanges:=False
        StdPr = Dir()
    Loop

 End Sub