2016-09-06 20 views
1

別のユーザーが自分のアドインを実行しても同じコードを自分で実行しても問題がない場合、下付き文字が範囲外のエラーになります。これは、ブックの値を設定するときに発生します。ファイル名は現在の日付を取得して生成され、gendateとして保存されます。これから、ファイル名が作成され、ユーザーが作成したファイルパスに基づいて保存されます。この例では、gv.Range( "b2")。textの値はC:¥Users¥username¥Desktop¥ReportGenerationです。したがって、fpはC:\ Users \ dmulhausen \ Desktop \ ReportGeneration \ TSReports9_6_201615h5m32s.xlsx です。これは私にとってエラーは発生しませんが、スクリプトの別のユーザーにエラーが発生しています。下付き文字が範囲外のエラー9 vba

Dim ai As Workbook 'add in data ---Initialized in Report Setup 
Dim dwb As Workbook 'destination workbook ---Initialized in Report Setup 
Dim ss As Worksheet 'source sheet 
Dim ds As Worksheet 'destination sheet or writing sheet 
Dim rv As Worksheet 'reporting variables sheet ---Initialized in Report Setup 
Dim pv As Worksheet 'ts variables sheet ---Initialized in Report Setup 
Dim gv As Worksheet 'global ai variables ---Initialized in Report Setup 
Dim tempstr As String 
Dim fp As String 'file path ---Initialized in Report Setup 
Dim gendate As Date 
Dim reportscreated As Integer 
Dim initialized As Boolean 
Dim sheetnames(1 To 12) As String 

Sub reportsetup() 
    Set ai = Workbooks("TSReports add in.xlam") 
    Set rv = ai.Worksheets("ReportVars") 
    Set pv = ai.Worksheets("TS1_2Vars") 
    Set gv = ai.Worksheets("globalVars") 
    If (IsEmpty(gv.Range("b2").Value)) Then 
     MsgBox ("Please select a designated folder for reports") 
     With Application.FileDialog(msoFileDialogFolderPicker) 
      .AllowMultiSelect = False 
      .Show 
      If .SelectedItems.Count > 0 Then 
       gv.Range("b2").Value = .SelectedItems(1) 
      End If 
      ai.Save 
     End With 

    End If 
    initialized = True 
    gendate = Now() 
    tempstr = "TSReports" & Month(gendate) & "_" & Day(gendate) & "_" & Year(gendate) & Hour(gendate) & "h" & Minute(gendate) & "m" & Second(gendate) & "s" 
    fp = gv.Range("b2").Text & "\" & tempstr & ".xlsx" 
    Workbooks.Add 
    Application.DisplayAlerts = False 
    Application.AlertBeforeOverwriting = False 
    Application.DisplayAlerts = False 
    ActiveWorkbook.SaveAs Filename:=fp 
    Set dwb = Workbooks(tempstr) '*******Error occurs here******* 

答えて

0

参照してください:Windows().Activate works on every computer except one

これは、問題を修正する必要があります。

tempstr = "TSReports" & Month(gendate) & "_" & Day(gendate) & "_" & _ 
      Year(gendate) & Hour(gendate) & "h" & Minute(gendate) & "m" & _ 
      Second(gendate) & "s" & ".xlsx" 

fp = gv.Range("b2").Text & "\" & tempstr 

Workbooks.Add 
Application.DisplayAlerts = False 
Application.AlertBeforeOverwriting = False 
Application.DisplayAlerts = False 
ActiveWorkbook.SaveAs Filename:=fp 
Set dwb = Workbooks(tempstr) 

しかし、これはより堅牢になります:

Set dwb = Workbooks.Add 
Application.DisplayAlerts = False 
Application.AlertBeforeOverwriting = False 
Application.DisplayAlerts = False 
dwb.SaveAs Filename:=fp 
関連する問題