以前ここで,2列目以降を1列目で固定して回帰する 以下のVBAを書いていただいたのですが,
2列目以降の1行目が同じ数字が続くと実行時エラーが出て作業が止まります.
例えばタブ名を0,0(1),0(2)... のようにするにはどこを書き換えればよいのでしょうか.
Sub RunRegressionByAnalysisToolPack()
On Error GoTo Err_RunRegressionByAnalysisToolPack
Dim addAnalysis As Excel.AddIn
Dim strATPVBA As String
Set addAnalysis = LoadAddIn("分析ツール")
If addAnalysis Is Nothing Then
Exit Sub
End If
Set addAnalysis = Nothing
Set addAnalysis = LoadAddIn("分析ツール - VBA")
If addAnalysis Is Nothing Then
Exit Sub
End If
strATPVBA = addAnalysis.Name
Set addAnalysis = Nothing
Dim wbkSource As Excel.Workbook
Dim wbkDestination As Excel.Workbook
Dim wsSource As Excel.Worksheet
Dim wsDestination As Excel.Worksheet
Dim rngInputRangeX As Excel.Range
Dim rngInputRangeY As Excel.Range
Dim lngFirstRow As Long
Dim lngLastRow As Long
Dim lngColumnX As Long
Dim lngColumnY As Long
Dim lngFirstColumnY As Long
Dim lngLastColumnY As Long
Dim lngSheetCount As Long
Set wbkSource = ActiveWorkbook
Set wsSource = wbkSource.ActiveSheet
With wsSource
lngFirstRow = 1
lngColumnX = 1
lngLastRow = .Cells(.Rows.Count, lngColumnX).End(xlUp).Row
If lngFirstRow > lngLastRow Then
Set wsSource = Nothing
Set wbkSource = Nothing
Exit Sub
End If
lngFirstColumnY = lngColumnX + 1
lngLastColumnY
= .Cells(lngFirstRow, .Columns.Count).End(xlToLeft).Column
If lngFirstColumnY > lngLastColumnY Then
Set wsSource = Nothing
Set wbkSource = Nothing
Exit Sub
End If
Set rngInputRangeX = .Range(.Cells(lngFirstRow, lngColumnX), _
.Cells(lngLastRow, lngColumnX))
Set wbkDestination = Workbooks.Add
lngSheetCount = 0
For lngColumnY = lngFirstColumnY To lngLastColumnY
Set rngInputRangeY = .Range(.Cells(lngFirstRow, lngColumnY),
_
.Cells(lngLastRow, lngColumnY))
With wbkDestination.Worksheets
lngSheetCount = lngSheetCount + 1
If lngSheetCount > .Count Then
Set wsDestination = .Add(After:=.Item(.Count))
Else
Set wsDestination = .Item(lngSheetCount)
End If
wsDestination.Name = "atp_" & rngInputRangeY.Cells(1,
1).Text
End With
Application.Run strATPVBA & "!Regress", _
rngInputRangeY, rngInputRangeX, _
False, True, , _
wsDestination.Cells(1, 1), _
False, False, False, _
False, , False
wsDestination.UsedRange.EntireColumn.AutoFit
Set rngInputRangeY = Nothing
Set wsDestination = Nothing
Next
End With
Application.DisplayAlerts = False
With wbkDestination.Worksheets
Do While .Count > lngSheetCount
.Item(.Count).Delete
Loop
.Item(1).Select
End With
Exit_RunRegressionByAnalysisToolPack:
On Error Resume Next
Application.DisplayAlerts = True
Set addAnalysis = Nothing
Set wsDestination = Nothing
Set wbkDestination = Nothing
Set rngInputRangeY = Nothing
Set rngInputRangeX = Nothing
Set wsSource = Nothing
Set wbkSource = Nothing
Exit Sub
Err_RunRegressionByAnalysisToolPack:
Dim strMsg As String
strMsg = "実行時エラー " & Err.Number & ": " & Err.Description
Debug.Print strMsg
MsgBox strMsg, vbCritical, "実行時エラー
(RunRegressionByAnalysisToolPack)"
Resume Exit_RunRegressionByAnalysisToolPack
End Sub
Function LoadAddIn(AddInName As String)
On Error GoTo Err_LoadAddIn
Dim AddIn As Excel.AddIn
Set AddIn = Application.AddIns(AddInName)
With AddIn
If .Installed = False Then
.Installed = True
End If
If .IsOpen = False Then
Workbooks.Open .FullName
End If
End With
Set LoadAddIn = AddIn
Exit_LoadAddIn:
Exit Function
Err_LoadAddIn:
On Error Resume Next
Dim strMsg As String
strMsg = "実行時エラー " & Err.Number & ": " & Err.Description
Debug.Print strMsg
strMsg = "[" & AddInName & "]を読み込むことが出来ません。" & vbCrLf &
vbCrLf & _
String(30, "-") & vbCrLf & vbCrLf & _
strMsg & vbCrLf & vbCrLf & _
String(30, "-")
MsgBox strMsg, vbCritical, "アドイン読込エラー(LoadAddIn)"
Set AddIn = Nothing
Resume Exit_LoadAddIn
End Function