【VBA】シート内容を外部テキスト保存

Private Sub Workbook_BeforeClose(Cancel As Boolean)

'セルクリア後は保存しないで閉じる

Dim cell_count As String

Sheets("特定履歴").Select

cell_count = WorksheetFunction.CountA(ActiveSheet.Range("A:A"))

If cell_count > 0 Then

    Call Sheets("特定履歴").rireki_export

Else

   Exit Sub

End If

End Sub



Private Sub CommandButton1_Click()

Call rireki_export

End Sub


Private Sub CommandButton2_Click()

Call cell_clear

End Sub


Sub cell_clear()

'セルクリア前にバックアップ
Call rireki_export


'全セルクリア
'処理コードをここに追加

'次月のファイル名を作成して保存
Dim save_folderpass As String
Dim PC_name As String
Dim now_year As String
Dim now_month As String

next_month = Month(Now) + 1

If next_month > 12 Then

    
    now_year = Format(Now, "yyyy") + 1
    next_month = "01"
    

Else

    now_year = Format(Now, "yyyy")
    next_month = Format(next_month, "00")

End If



'作業環境に合わせて変更
save_folderpass = "C:\Users\ya570\Desktop\"
PC_name = "①検品側"


Application.DisplayAlerts = False

Sheets("特定履歴").Copy

ActiveWorkbook.SaveAs Filename:=save_folderpass & now_year & next_month & "_" & PC_name & ".txt", _
                      FileFormat:=xlText, Local:=True
                      
                      

                      
  ActiveWorkbook.Close
  
  Application.DisplayAlerts = True



End Sub

Sub rireki_export()

Dim save_folderpass As String
Dim PC_name As String
Dim now_year As String
Dim now_month As String


now_year = Format(Now, "yyyy")
now_month = Format(Now, "mm")

'作業環境に合わせて変更
save_folderpass = "C:\Users\ya570\Desktop\"
PC_name = "①検品側"


Application.DisplayAlerts = False

Sheets("特定履歴").Copy

ActiveWorkbook.SaveAs Filename:=save_folderpass & now_year & now_month & "_" & PC_name & ".txt", _
                      FileFormat:=xlText, Local:=True
                      
                      

                      
  ActiveWorkbook.Close
  
  Application.DisplayAlerts = True


End Sub

この記事が気に入ったらサポートをしてみませんか?