文件已经存在时如何保存Excel工作簿? [英] How to save an Excel Workbook when the file already exists?

查看:44
本文介绍了文件已经存在时如何保存Excel工作簿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此宏保存我的工作簿:

I use this macro to save my workbook:

sNewFilePath = ActiveWorkbook.Path

ActiveWorkbook.SaveAs Filename:=sNewFilePath & "/" &Range("A1")

但是,如果文件已经存在,并且我取消"或选择否",则会收到VBA错误.我是VBA的新手,所以我不知道在宏中添加哪些功能以及在何处添加功能以使其正常工作...

But if a file already exists, and I "Cancel" or choose "No", I get VBA error. I'm new at VBA, so I cant figure out what and where to add functions to the macro to make it work...

推荐答案

使用函数 Dir(),您可以检查文件是否存在并确定要执行的操作.

Using the function Dir() you can check if a file exist and determine what to do.

示例

if Dir("C:\ScrubRetValFile.txt") = "" Then
    msgbox "File not founded"
else
    msgbox "File founded"
End if

干杯.

这篇关于文件已经存在时如何保存Excel工作簿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆