|
3つのファイルがありA.xlsを起動し、A.xls内のマクロの記述を元に、B.xlsとC.xlsを立ち上げています
データのインポートをC.xlsのシート1と2に行なっております
B. xlsにデータがあれば下記コードで問題無ですが、
データがない場合にC.xlsとB.xlsを閉じれません
データがあっても、なくてもC.xlsとB.xlsを閉じ、
データがありませんと表示させたく思います。
VBAで行なうにはどのようにすれば良いでしょうか?
Sub ボタン1_Click()
Application.ScreenUpdating = False
Workbooks.Open ("C:\東京\B.xls")
Workbooks.Open ("C:\東京\C.xls")
'sheet1
Dim ws1 As Worksheet, ws2 As Worksheet
Dim r As Range
Dim n As Long
Dim i As Long, j As Long, k As Long
Set ws1 = Workbooks("C.xls").Worksheets("Sheet1")
Set ws2 = Workbooks("B.xls").Worksheets("Sheet1")
Set r = ws2.Range("A:I").Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
If r Is Nothing Then Exit Sub
n = Int((r.Row - 2) / 20)
If n < 0 Then Exit Sub
Application.ScreenUpdating = False
For i = 0 To n
If i > 0 Then ws1.Range("A1:J33").Copy ws1.Cells(33 * i + 1, 1)
For j = 1 To 9
If j = 1 Then k = j Else k = j + 1
ws1.Cells(33 * i + 12, k).Resize(20).Value = _
ws2.Cells(20 * i + 2, j).Resize(20).Value
Next
Next
Dim x As Long
For x = 1 To Cells(Rows.Count, 10).End(xlUp).Row
If Range("G" & x).Value = "数" Then
Range("J" & x).Value = "送"
End If
Next
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="\\サーバ名\フォルダ名1\共有フォルダ名2\C"
Application.DisplayAlerts = True
'sheet2
Dim ws3 As Worksheet, ws4 As Worksheet
Dim r1 As Range
Dim n1 As Long
Dim i1 As Long, j1 As Long, k1 As Long
Set ws3 = Workbooks("C.xls").Worksheets("Sheet2")
Set ws4 = Workbooks("B.xls").Worksheets("Sheet2")
Set r1 = ws4.Range("A:I").Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
If r1 Is Nothing Then Exit Sub
n1 = Int((r1.Row - 2) / 20)
If n1 < 0 Then Exit Sub
Application.ScreenUpdating = False
For i1 = 0 To n1
If i1 > 0 Then ws3.Range("A1:J33").Copy ws3.Cells(33 * i1 + 1, 1)
For j1 = 1 To 9
If j1 = 1 Then k1 = j1 Else k1 = j1 + 1
ws3.Cells(33 * i1 + 12, k1).Resize(20).Value = _
ws4.Cells(20 * i1 + 2, j1).Resize(20).Value
Next
Next
Dim x1 As Long
For x1 = 1 To Cells(Rows.Count, 10).End(xlUp).Row
If Range("G" & x1).Value = "数" Then
Range("J" & x1).Value = "送"
End If
Next
Workbooks("C.xls").Close SaveChanges:=True
Workbooks("B.xls").Close SaveChanges:=False
Application.ScreenUpdating = True
MsgBox "終わりました"
End Sub
|
|