尝试使用Workbooks.Open(filename,ReadOnly:=False)方法在写入模式下使用VB脚本打开Excel文件时,收到错误消息,指出该文件是以只读模式打开的。收到错误消息-- * 运行时错误“1004”:无法保存“testqry1.xlsm”,因为该文件是只读的。若要保留更改,您需要使用新名称保存工作簿或将其保存在其他位置。* PFB我使用的vb脚本...
Sub read_Test1()
Dim val
Dim lrow
ReDim cell_val(1)
Dim fp, fn, newFileName
fn = ThisWorkbook.FullName
fp = ThisWorkbook.Path
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
Set objWrk = objExcel.Workbooks.Open(fn, ReadOnly:=False)
Set ws = objWrk.Sheets("Sheet2")
RowCount = ws.UsedRange.Rows.Count
' Redefine array size dynamically based on number of Rows
ReDim cell_val(RowCount)
ReDim newCell_val(RowCount)
For j = 1 To RowCount
cell_val(j - 1) = ws.Cells(j, 1)
Next
For k = 1 To 3
Dim str_tmp
If (InStr(cell_val(k - 1), "<DIV>")) Then
str_tmp = Replace(cell_val(k - 1), "<DIV>", "", 1, -1, vbTextCompare)
End If
newCell_val(k - 1) = str_tmp
Next
For m = 1 To 3
ws.Cells(m, 2).value = newCell_val(m - 1)
objExcel.ActiveWorkbook.Save
Next
End Sub
我也试过Workbook.ChangeFileAccess方法,但仍然是徒劳的...我是vba的新手,请告诉我我错过了什么。
1条答案
按热度按时间vvppvyoh1#
您打开的是正在工作的同一个工作簿,我的设备不是打开工作簿并使用保存来保存,而不是objWrk.Save。