Writing a string to a cell in excel

后端 未结 4 1833
花落未央
花落未央 2020-12-15 09:30

I am trying to write a value to the \"A1\" cell, but am getting the following error:

Application-defined or object-defined error \'1004\'

4条回答
  •  眼角桃花
    2020-12-15 10:30

    try this instead

    Set TxtRng = ActiveWorkbook.Sheets("Game").Range("A1")

    ADDITION

    Maybe the file is corrupt - this has happened to me several times before and the only solution is to copy everything out into a new file.

    Please can you try the following:

    • Save a new xlsm file and call it "MyFullyQualified.xlsm"
    • Add a sheet with no protection and call it "mySheet"
    • Add a module to the workbook and add the following procedure

    Does this run?

     Sub varchanger()
    
     With Excel.Application
        .ScreenUpdating = True
        .Calculation = Excel.xlCalculationAutomatic
        .EnableEvents = True
     End With
    
     On Error GoTo Whoa:
    
        Dim myBook As Excel.Workbook
        Dim mySheet As Excel.Worksheet
        Dim Rng  As Excel.Range
    
        Set myBook = Excel.Workbooks("MyFullyQualified.xlsm")
        Set mySheet = myBook.Worksheets("mySheet")
        Set Rng = mySheet.Range("A1")
    
        'ActiveSheet.Unprotect
    
    
        Rng.Value = "SubTotal"
    
        Excel.Workbooks("MyFullyQualified.xlsm").Worksheets("mySheet").Range("A1").Value = "Asdf"
    
    LetsContinue:
            Exit Sub
    Whoa:
            MsgBox Err.Number
            GoTo LetsContinue
    
    End Sub
    

提交回复
热议问题