我已经打开了一个工作簿,但我正在运行另一个工作簿中的宏。我想使用它的名称激活第一个工作簿。
代码:
FileName = input_path_1 & input_file_1
Workbooks(FileName.xls).Activate
当我尝试这样做时,它会给我“下标超出范围”错误。我该如何解决?
答案 0 :(得分:6)
检查变量Filename
是否包含正确的文件名。 (例如 Sample.xls )
同时检查input_path_1
和input_file_1
是否具有正确的值
如果他们有,它应该是这样的:
Workbooks(Filename).Activate
现在,如果您需要附加扩展名称(例如Filename
值只是 Sample ):
Workbooks(Filename & ".xls").Activate
参数应始终采用字符串形式,并且应该是完整的文件名(带扩展名)。虽然也接受数字(索引),但您无法确定哪个索引引用了哪个工作簿。更好的是,将其分配给变量。
Dim otherWB As Workbook
Set otherWB = Workbooks(Filename)
'Set otherWB = Workbooks(Filename & ".xls") '~~> for second scenario above
编辑1:从评论中,如果Filename
包含完整路径,则可能会有效。
Dim Filename1 As String
Filename1 = Split(Filename, "\")(UBound(Split(Filename, "\")))
Workbooks(Filename1).Activate
答案 1 :(得分:1)
只能通过以下方法访问特定工作簿窗口
Vba
#include <cppconn/prepared_statement.h> // preparedStatement
sql::PreparedStatement* pstatement;
try{
for(const auto& bar : m_bars) {
std::string sql = "INSERT INTO "
+ m_table_name
+ " VALUES (' "
+ trade_platform::datetime::toString(datetime) + "', '"
+ bar.first + "', "
+ "'IB', "
+ std::to_string(bar.second.getOpen()) + ", "
+ std::to_string(bar.second.getHigh()) + ", "
+ std::to_string(bar.second.getLow()) + ", "
+ std::to_string(bar.second.getClose()) + ", "
+ std::to_string(bar.second.getVolume()) + ");";
pstatement = m_conn->prepareStatement(sql);
// prepare our statement and execute query
pstatement->executeUpdate();
}
}catch(const std::exception& e){
std::cerr << "flushToDB problem: " << e.what() << "\n";
}catch(...){
std::cerr << "unspecified flushToDB problem\n";
}
// free
delete pstatement;
C#
Dim filename as string
set filename = Path.GetFileName(fullFilename)
set Workbook.Windows(filename).WindowState = Excel.XlWindowState.xlMinimized
set Workbook.Windows(filename).WindowState = Excel.XlWindowState.xlNormal
' You can also use Worksheet.Activate() here if you want
答案 2 :(得分:0)
aback
abaft
abeam
abed
abandonedly
abashedly
abhorrently
将无法工作
为打开的文件设置一个全局 wb 变量并使用它 例如。
Set OutsideWb = Workbooks("path + Filename.xlsm")