运行时错误1004使用For循环中的Range - VBA Excel Mac

时间:2013-03-18 09:04:58

标签: macos excel excel-vba vba

某些背景:   - 我正在建造一张大桌子(1000x1000)   - 我想用值1填充一些案例   - 我将每个案例的行和列放在一个单独的表中

这是我用来填表的代码。正如您所看到的,我在For循环中使用Range。它之前使用较小的数据集。导致麻烦的是这一行:     表格(“Feuil1.csv”)。单元格(rng.Value,rng.Offset(,1).Value)=“1”

Sub FillEdges()
Dim rng As Range
For Each rng In Sheets("Feuil2").Range("A1:A53498")
    Sheets("Feuil1.csv").Cells(rng.Value, rng.Offset(, 1).Value) = "1"    
Next rng
End Sub

做一些研究我明白我可能偶尔会保存,关闭并重新打开工作簿。我试过了,但似乎VBA没有考虑“保存,关闭和重新打开”代码。我试图通过这个帮助页面找出一些东西:

http://support.microsoft.com/kb/210684/en-us

这导致我使用此代码的错误91。错误发生在第三行。

    Sub FillEdges()
        Dim rng As Range, mybook As Workbook

mybook.SaveAs "Macintosh HD:Users:Nicolas:test.xslm", FileFormat:=53
    For Each rng In Sheets("Feuil2").Range("A1:A53498")
    If rng Mod 2500 = 0 Then
                mybook.Close SaveChanges:=True
                Set mybook = Nothing
                Set mybook = Application.Workbooks.Open("Macintosh HD:Users:Nicolas:test.xslm")
            End If
        Sheets("Feuil1.csv").Cells(rng.Value, rng.Offset(, 1).Value) = "1"

    Next rng
    End Sub

你知道怎么解决这个错误91吗?

所以我不确定这将完全修复错误1004,但我认为这是正确的道路。或者也许你会建议其他的东西......?

非常感谢你的时间。 祝你有美好的一天。 尼古拉斯。

1 个答案:

答案 0 :(得分:0)

这就是我最终的表现。在编码之前了解有关VBA的更多信息是关键!特别是关于打开,保存和关闭文件。

请注意,最后我几乎没有触及上面引用的微软网站的例子。

Sub remplir()

Application.ScreenUpdating = False

Dim rng As Range
Dim worksheet1 As Worksheet

Dim iTemp As Integer
Dim oBook As Workbook
Dim iCounter As Long


Set worksheet1 = ActiveWorkbook.ActiveSheet
iCounter = 1

    ' Open the new full of zeros workbook:

    Application.Workbooks.Open "Macintosh HD:Users:Nicolas:vba:test.xlsm"
    Set oBook = ActiveWorkbook



    'iTemp = Application.SheetsInNewWorkbook
    'Application.SheetsInNewWorkbook = 1
    'Set oBook = Application.Workbooks.Add
    'Application.SheetsInNewWorkbook = iTemp

    ' Add a defined name to the workbook
    ' that RefersTo a range:
    oBook.Names.Add Name:="tempRange", _
        RefersTo:="=Sheet1!$A$1"

    ' Save the workbook: (attention extension fausse, mais ça n'empeche rien !)
    oBook.SaveAs "Macintosh HD:Users:Nicolas:vba:test.xslm", FileFormat:=53

    ' Work the sheet in a loop. Eventually,
    ' you get error 1004: Copy Method of
    ' Worksheet class failed.
  For Each rng In worksheet1.Range("A1:A5038")

  If rng.Value > 0 And rng.Row <> 1 Then
  If rng.Value <> rng.Offset(-1, 0).Value Then
  iCounter = iCounter + 1
  End If
  End If

    Sheets("Feuil1").Cells(iCounter, rng.Offset(0, 1).Value + 1) = "1"
    Sheets("Feuil1").Cells(iCounter, 1) = rng.Value
        'Uncomment this code for the workaround:
        'Save, close, and reopen after every 100 iterations:
        If rng Mod 1000 = 0 Then
            oBook.Close SaveChanges:=True
            Set oBook = Nothing
            '(attention extension fausse, mais ça n'empeche rien !)
            Set oBook = Application.Workbooks.Open("Macintosh HD:Users:Nicolas:vba:test.xslm")
        End If

Next rng