Excel编译错误:无法分配给只读属性

时间:2021-02-27 18:52:56

I'm getting an error in Excel regarding assignment to a read-only property with the following code:

我在Excel中收到有关使用以下代码分配给只读属性的错误:

1    Sub GetSheets()
2    Path = "C:WHERE MY DOCUMENTS ARE KEPT"
3    Filename = Dir(Path & "*.CSV")
4      Do While Filename <> ""
5      Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
6         For Each Sheet In ActiveWorkbook.Sheets
7         Sheet.Copy After:=ThisWorkbook.Sheets(1)
8      Next Sheet
9         Workbooks(Filename).Close
10         Filename = Dir()
11      Loop
12    End Sub

1 个解决方案

#1


3  

I'm guessing you have the posted code in the ThisWorkbook module ?

我猜你在ThisWorkbook模块中有发布的代码?

ThisWorkbook represents the workbook itself, which has a built-in (read-only) Path property.

ThisWorkbook表示工作簿本身,它具有内置(只读)Path属性。

Rename Path to (e.g.) myPath and you should be OK.

将路径重命名为(例如)myPath,您应该没问题。

Sub GetSheets()
    'best to use a Constant here...
    Const THE_PATH As String = "C:\WHERE\MY DOCUMENTS\ARE KEPT\"

    Dim Filename as String, wb As Workbook, Sheet As Worksheet

    Filename = Dir(THE_PATH  & "*.CSV")
    Do While Filename <> ""
         Set wb = Workbooks.Open(Filename:=THE_PATH & Filename, ReadOnly:=True)
         For Each Sheet In wb.Sheets
             Sheet.Copy After:=ThisWorkbook.Sheets(1)
         Next Sheet
         wb.Close
         Filename = Dir()
      Loop
End Sub

#1


3  

I'm guessing you have the posted code in the ThisWorkbook module ?

我猜你在ThisWorkbook模块中有发布的代码?

ThisWorkbook represents the workbook itself, which has a built-in (read-only) Path property.

ThisWorkbook表示工作簿本身,它具有内置(只读)Path属性。

Rename Path to (e.g.) myPath and you should be OK.

将路径重命名为(例如)myPath,您应该没问题。

Sub GetSheets()
    'best to use a Constant here...
    Const THE_PATH As String = "C:\WHERE\MY DOCUMENTS\ARE KEPT\"

    Dim Filename as String, wb As Workbook, Sheet As Worksheet

    Filename = Dir(THE_PATH  & "*.CSV")
    Do While Filename <> ""
         Set wb = Workbooks.Open(Filename:=THE_PATH & Filename, ReadOnly:=True)
         For Each Sheet In wb.Sheets
             Sheet.Copy After:=ThisWorkbook.Sheets(1)
         Next Sheet
         wb.Close
         Filename = Dir()
      Loop
End Sub