excel VBA 打开一个工作簿,然后赋值,再打开另一个,这个里面有公式等于第一个工作簿的内容
我希望能后台打开,不知道语句错在哪里,然后保存关闭时应该怎么写,我只要关闭1,2工作簿Subtry()DimmypathAsString,svalue'定义变量mypat...
我希望能后台打开,不知道语句错在哪里,然后保存关闭时应该怎么写,我只要关闭1,2工作簿
Sub try()
Dim mypath As String, svalue '定义变量
mypath = "C:\Users\Administrator\Desktop\1.xls"
svalue = "C:\Users\Administrator\Desktop\2.xlsm"
Workbooks.Open Filename:=mypath '这句是打开文件
'ActiveWorkbook.Visible = False '这句是隐藏文件
ActiveWorkbook.Sheets(1).Range("a1").Value = "hello"
Workbooks.Open Filename:=svalue
Application.DisplayAlerts = False
ActiveWorkbook.Close True
ActiveWorkbook.Close True
End Sub 展开
Sub try()
Dim mypath As String, svalue '定义变量
mypath = "C:\Users\Administrator\Desktop\1.xls"
svalue = "C:\Users\Administrator\Desktop\2.xlsm"
Workbooks.Open Filename:=mypath '这句是打开文件
'ActiveWorkbook.Visible = False '这句是隐藏文件
ActiveWorkbook.Sheets(1).Range("a1").Value = "hello"
Workbooks.Open Filename:=svalue
Application.DisplayAlerts = False
ActiveWorkbook.Close True
ActiveWorkbook.Close True
End Sub 展开
推荐于2016-11-04 · 知道合伙人软件行家
关注
展开全部
改成这样:
Sub try()
Application.DisplayAlerts = False
Dim mypath As String, svalue '定义变量
mypath = "C:\Users\Administrator\Desktop\1.xls"
svalue = "C:\Users\Administrator\Desktop\2.xlsm"
dim wkbk as object,wksht as object
set wkbk=getobject(mypath) '这句是打开文件
set wksht=wkbk.Sheets(1)
with wksht
.Range("a1").Value = "hello"
end with
wkbk.close true
dim wkbk2 as object
set wkbk2=getobject(svalue)
wkbk2.Close True
End Sub
追问
这样出现了一个问题,我手动打开1和2,结果我什么都看不到了,这是怎么回事?我还想请教一个问题,怎么实现把这样做好的工作表,不是里面有公式的,变成数字或者文本格式的,还有不知道你有没有用过GOOFFICE,如果我用这个刷新1.xls,能不在后台打开2,刷新2中的数据嘛
追答
如果要显示1和2,代码改成
Sub try()
Application.DisplayAlerts = False
Dim mypath As String, svalue '定义变量
mypath = "C:\Users\Administrator\Desktop\1.xls"
svalue = "C:\Users\Administrator\Desktop\2.xlsm"
dim wkbk as object,wksht as object
set wkbk=getobject(mypath) '这句是打开文件
wkbk.visible =true
set wksht=wkbk.Sheets(1)
with wksht
.Range("a1").Value = "hello"
end with
wkbk.close true
dim wkbk2 as object
set wkbk2=getobject(svalue)
wkbk2.visible =true
wkbk2.Close True
End Sub
我没有用过GOOFFICE,但是估计GOOFFICE无法运行这样的宏。
展开全部
Sub try()
Dim mypath As String, File1 as string,file2 as string,wb as workbook
application.ScreenUpdating=false
Application.DisplayAlerts = False
mypath = "C:\Users\Administrator\Desktop\"
file1="1.xls":file2="2.xlsm"
set wb=workbooks.open(mypath & file1,,false) '可写方式打开
with wb
windows(.name).visible=false
.worksheets(1).[a1]="hello"
windows(.name).visible=true
.close true '保存改变并关闭
end with
set wb2=workbooks.open(mypath & file2,,false)
wb2.close true
application.ScreenUpdating=true
Application.DisplayAlerts =true
End Sub
Dim mypath As String, File1 as string,file2 as string,wb as workbook
application.ScreenUpdating=false
Application.DisplayAlerts = False
mypath = "C:\Users\Administrator\Desktop\"
file1="1.xls":file2="2.xlsm"
set wb=workbooks.open(mypath & file1,,false) '可写方式打开
with wb
windows(.name).visible=false
.worksheets(1).[a1]="hello"
windows(.name).visible=true
.close true '保存改变并关闭
end with
set wb2=workbooks.open(mypath & file2,,false)
wb2.close true
application.ScreenUpdating=true
Application.DisplayAlerts =true
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |