使用单元格引用和时间戳保存的VBS

时间:2016-05-04 22:16:12

标签: excel excel-vba vbscript vba

我有一个vb脚本,它执行一个excel宏并保存附加名称的文件,其中包含特定的单元格值和时间戳。但它不起作用,我完全迷失了原因。

如果我取出单元格引用和时间戳,它会正确执行并保存。我将非常感谢您可以查看的任何帮助或材料。提前谢谢。

Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample() 

Dim objApp  
Dim objExcel  
Dim objWorkbook
Dim objSheet

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\midi\Desktop\EN\SCORES.xlsm")
Set objSheet = objWorkbook.Worksheets("Cover Tab") 
Set SiteIdentifier = objSheet.Cells("B4").Value  

objExcel.Run "RefreshConns"
WScript.Sleep 30000 

objExcel.ActiveWorkbook.SaveAs "C:\Users\midi\Desktop\EN\SCORES_" & SiteIdentifier & "_"& Format(Now(), "yyyymmdd_hhnnss") & ".xlsm", 52

objExcel.ActiveWorkbook.Close

objExcel.Quit   


End Sub 

1 个答案:

答案 0 :(得分:0)

谢谢大家的建议,帮助我了解我的错误。发布关闭答案。

Option Explicit

ExcelMacroExample

Sub ExcelMacroExample() 

  Dim objApp  
  Dim objExcel  
  Dim objWorkbook
  Dim objSheet
  Dim SiteIdentifier

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\midi\Desktop\EN\SCORES.xlsm")
Set objSheet = objWorkbook.Worksheets("Cover Tab") 

SiteIdentifier = objSheet.Cells(4, 2).Value

 objExcel.Run "RefreshConns"
    WScript.Sleep 60000 

 objExcel.ActiveWorkbook.SaveAs "C:\Users\midi\Desktop\EN\SCORES_" & SiteIdentifier & "_" & Year(Now()) & Month(Now()) & Day(Now()) & "_" & Hour(Now()) & Minute(Now()) & Second(Now())&".xlsm", 52

 objExcel.ActiveWorkbook.Close

 objExcel.Quit   


End Sub