需依據單一模板,批量產生數十、上百個內容遞增的 CSV 檔?結合 Excel 模板公式、VBA 使用者表單(UserForm)輸入參數、巨集迴圈存檔,即可實現全自動化批量產出。
假設 A2=檔名稱首字+編號,B2=起始數值,其他欄透過公式引用 A2、B2 計算。修改 A2、B2 即更新整張表。





Private Sub CommandButton1_Click() '確定鈕
Dim n As Long
If TextBox1 <> "" And TextBox2 <> "" And TextBox3 <> "" And TextBox2 < TextBox3 Then
On Error Resume Next
MkDir CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & TextBox1
For n = 1 To (TextBox3 - TextBox2 + 1) / 10
Sheets(1).Cells(2, 1).Value = TextBox1 & "-" & n
Sheets(1).Cells(2, 2).Value = TextBox2 + 10 * (n - 1)
ActiveWorkbook.SaveAs Filename:=CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & TextBox1 & "\" & TextBox1 & "-" & n, _
FileFormat:=xlCSV, CreateBackup:=False
Next n
Unload Me
MsgBox "資料處理成功!", vbOKOnly + vbInformation, "提示"
Else
MsgBox "請核對資料資訊!", vbOKOnly + vbExclamation, "提示"
TextBox1.SetFocus
End If
End Sub
Private Sub CommandButton2_Click() '清除鈕
TextBox1 = "": TextBox2 = "": TextBox3 = "": TextBox1.SetFocus
End Sub
Private Sub CommandButton3_Click() '取消鈕
Unload Me
End Sub
' 輸入限制:TextBox1 僅允許英文字母
Private Sub TextBox1_Change()
Dim i%, Str$
With TextBox1
For i = 1 To Len(.Text)
Str = Mid(.Text, i, 1)
Select Case Str
Case "a" To "z", "A" To "Z"
Case Else: Beep: .Text = Replace(.Text, Str, "")
End Select
Next
End With
End Sub
' TextBox2/3 僅允許數字 0-9,程式碼類似


Sub 開啟視窗()
UserForm1.Show
End Sub




