|
good8195 发表于 2015-5-13 17:34
還是一樣
老師你直接把這三個excel的vba代碼貼上來好了
我在自己慢慢改,要不然代碼在excel裡面我的都會 ...
- Private Sub Worksheet_Activate()
- Sheet1.[f1] = Sheet1.[f10]
- Sheet1.[g1] = Sheet1.[g10]
- Sheet1.[f2] = Sheet1.[f11]
- Sheet1.[g2] = Sheet1.[g11]
-
- End Sub
- Private Sub 生成Word檔_Click()
- Dim Word對象 As New Word.Application, 當前路徑, 導出檔案名, 導出路徑檔案名, 資料名
- Dim i, j
- Dim Str1, Str2
- 當前路徑 = ThisWorkbook.Path
- 數據表名 = "付款申請表"
- 最後行號 = Sheets(數據表名).Range("B65536").End(xlUp).Row
- 判斷 = 0
- For i = 3 To 最後行號
- 導出檔案名 = "工程付款申請單"
- FileCopy 當前路徑 & "\工程付款申請單(範本).doc", 當前路徑 & "" & 導出檔案名 & "(" & Sheets(數據表名).Range("B" & i) & ").doc"
- 導出路徑檔案名 = 當前路徑 & "" & 導出檔案名 & "(" & Sheets(數據表名).Range("B" & i) & ").doc"
- With Word對象
- .Documents.Open 導出路徑檔案名
- .Visible = False
- '******************為便於理解和移植代碼,這裏就不採用迴圈的方法了!***************************
- '填寫文字資料
- Str1 = "數據1"
- Str2 = Sheets(數據表名).Cells(i, 2)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據2"
- Str2 = Sheets(數據表名).Cells(i, 3)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據3"
- Str2 = Sheets(數據表名).Cells(i, 4)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據4"
- Str2 = Sheets(數據表名).Cells(i, 5)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據4"
- Str2 = Sheets(數據表名).Cells(i, 5)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據5"
- Str2 = Sheets(數據表名).Cells(i, 6)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據6"
- Str2 = Sheets(數據表名).Cells(i, 7)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據4"
- Str2 = Sheets(數據表名).Cells(i, 5)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據7"
- Str2 = Sheets(數據表名).Cells(i, 8)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據8"
- Str2 = Sheets(數據表名).Cells(i, 9)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據9"
- Str2 = Sheets(數據表名).Cells(i, 10)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
- Str1 = "數據10"
- Str2 = Sheets(數據表名).Cells(i, 11)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據4"
- Str2 = Sheets(數據表名).Cells(i, 5)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據11"
- Str2 = Sheets(數據表名).Cells(i, 12)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
-
- Str1 = "數據12"
- Str2 = Sheets(數據表名).Cells(i, 13)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Text = Str2 '替換字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- End If
- '填寫表格資料
- .ActiveDocument.Tables(1).Cell(3, 2).Range = Sheets(數據表名).Cells(i, 14)
- .ActiveDocument.Tables(1).Cell(3, 3).Range = Sheets(數據表名).Cells(i, 15)
- .ActiveDocument.Tables(1).Cell(3, 4).Range = Sheets(數據表名).Cells(i, 16)
- .ActiveDocument.Tables(1).Cell(3, 5).Range = Sheets(數據表名).Cells(i, 17)
-
- .ActiveDocument.Tables(1).Cell(4, 2).Range = Sheets(數據表名).Cells(i, 18)
- .ActiveDocument.Tables(1).Cell(4, 3).Range = Sheets(數據表名).Cells(i, 19)
- .ActiveDocument.Tables(1).Cell(4, 4).Range = Sheets(數據表名).Cells(i, 20)
- .ActiveDocument.Tables(1).Cell(4, 5).Range = Sheets(數據表名).Cells(i, 21)
-
- .ActiveDocument.Tables(1).Cell(5, 2).Range = Sheets(數據表名).Cells(i, 22)
- .ActiveDocument.Tables(1).Cell(5, 3).Range = Sheets(數據表名).Cells(i, 23)
- .ActiveDocument.Tables(1).Cell(5, 4).Range = Sheets(數據表名).Cells(i, 24)
- .ActiveDocument.Tables(1).Cell(5, 5).Range = Sheets(數據表名).Cells(i, 25)
-
- .ActiveDocument.Tables(1).Cell(6, 2).Range = Sheets(數據表名).Cells(i, 26)
- .ActiveDocument.Tables(1).Cell(6, 3).Range = Sheets(數據表名).Cells(i, 27)
- .ActiveDocument.Tables(1).Cell(6, 4).Range = Sheets(數據表名).Cells(i, 28)
- .ActiveDocument.Tables(1).Cell(6, 5).Range = Sheets(數據表名).Cells(i, 29)
-
- .ActiveDocument.Tables(1).Cell(9, 2).Range = Sheets(數據表名).Cells(i, 30)
- .ActiveDocument.Tables(1).Cell(9, 3).Range = Sheets(數據表名).Cells(i, 31)
- .ActiveDocument.Tables(1).Cell(9, 4).Range = Sheets(數據表名).Cells(i, 32)
- .ActiveDocument.Tables(1).Cell(9, 5).Range = Sheets(數據表名).Cells(i, 33)
-
- .ActiveDocument.Tables(1).Cell(10, 2).Range = Sheets(數據表名).Cells(i, 34)
- .ActiveDocument.Tables(1).Cell(10, 3).Range = Sheets(數據表名).Cells(i, 35)
- .ActiveDocument.Tables(1).Cell(10, 4).Range = Sheets(數據表名).Cells(i, 36)
- .ActiveDocument.Tables(1).Cell(10, 5).Range = Sheets(數據表名).Cells(i, 37)
-
- .ActiveDocument.Tables(1).Cell(11, 2).Range = Sheets(數據表名).Cells(i, 38)
- .ActiveDocument.Tables(1).Cell(11, 3).Range = Sheets(數據表名).Cells(i, 39)
- .ActiveDocument.Tables(1).Cell(11, 4).Range = Sheets(數據表名).Cells(i, 40)
- .ActiveDocument.Tables(1).Cell(11, 5).Range = Sheets(數據表名).Cells(i, 41)
-
- .ActiveDocument.Tables(1).Cell(12, 2).Range = Sheets(數據表名).Cells(i, 42)
- .ActiveDocument.Tables(1).Cell(12, 3).Range = Sheets(數據表名).Cells(i, 43)
- .ActiveDocument.Tables(1).Cell(12, 4).Range = Sheets(數據表名).Cells(i, 44)
- .ActiveDocument.Tables(1).Cell(12, 5).Range = Sheets(數據表名).Cells(i, 45)
-
- .ActiveDocument.Tables(1).Cell(13, 2).Range = Sheets(數據表名).Cells(i, 46)
- .ActiveDocument.Tables(1).Cell(13, 3).Range = Sheets(數據表名).Cells(i, 47)
- .ActiveDocument.Tables(1).Cell(13, 4).Range = Sheets(數據表名).Cells(i, 48)
- .ActiveDocument.Tables(1).Cell(13, 5).Range = Sheets(數據表名).Cells(i, 49)
- End With
- Word對象.Documents.Save
- Word對象.Quit
- Set Word對象 = Nothing
- Next i
- If 判斷 = 0 Then
- j = MsgBox("已輸出到 Word 檔!", 0 + 48 + 256 + 0, "提示:")
- End If
- End Sub
复制代码- Function dxje(q)
- ybb = Round(q * 100) '將輸入的數值擴大100倍,進行四捨五入
- y = Int(ybb / 100) '截取出整數部分
- j = Int(ybb / 10) - y * 10 '截取出十分位
- f = ybb - y * 100 - j * 10 '截取出百分位
- zy = Application.WorksheetFunction.Text(y, "[dbnum2]") '將整數部分轉為中文大寫
- zj = Application.WorksheetFunction.Text(j, "[dbnum2]") '將十分位轉為中文大寫
- zf = Application.WorksheetFunction.Text(f, "[dbnum2]") '將百分位轉為中文大寫
- dxje = zy & "元" & "整"
- d1 = zy & "元"
- If f <> 0 And j <> 0 Then
- dxje = d1 & zj & "角" & zf & "分"
- If y = 0 Then
- dxje = zj & "角" & zf & "分"
- End If
- End If
- If f = 0 And j <> 0 Then
- dxje = d1 & zj & "角" & "整"
- If y = 0 Then
- dxje = zj & "角" & "整"
- End If
- End If
- If f <> 0 And j = 0 Then
- dxje = d1 & zj & zf & "分"
- If y = 0 Then
- dxje = zf & "分"
- End If
- End If
- If q = "" Then
- dxje = 0 '如沒有輸入任何數值為0
- End If
- End Function
复制代码- Private Sub CommandButton輸出通知到Word檔_Click()
- Dim Word對象 As New Word.Application, 當前路徑, 導出檔案名, 導出路徑檔案名, i, j
- Dim Str1, Str2
- 當前路徑 = ThisWorkbook.Path
- 最後行號 = Sheets("數據").Range("B65536").End(xlUp).Row
- 判斷 = 0
- For i = 2 To 最後行號
- 導出檔案名 = "授課通知"
- FileCopy 當前路徑 & "\授課通知(範本).doc", 當前路徑 & "" & 導出檔案名 & "(" & Sheets("數據").Range("B" & i) & ").doc"
- 導出路徑檔案名 = 當前路徑 & "" & 導出檔案名 & "(" & Sheets("數據").Range("B" & i) & ").doc"
- With Word對象
- .Documents.Open 導出路徑檔案名
- .Visible = False
- For j = 1 To 5 '填寫文字資料
- Str1 = "數據" & Format(j, "000")
- Str2 = Sheets("數據").Cells(i, j + 1)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- .Selection.Text = Str2 '替換字串
- End If
- Next j
- For j = 1 To 3 '填寫表格資料
- .ActiveDocument.Tables(1).Cell(2, j).Range = Sheets("數據").Cells(i, j + 6)
- .ActiveDocument.Tables(1).Cell(4, j).Range = Sheets("數據").Cells(i, j + 9)
- Next j
- .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader '設置位置在頁眉
- Str1 = "數據006"
- Str2 = Sheets("數據2").Cells(2, 2)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- .Selection.Text = Str2 '替換字串
- End If
- .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter '設置位置在頁腳
- Str1 = "數據007"
- Str2 = Sheets("數據2").Cells(2, 1)
- .Selection.HomeKey Unit:=wdStory '游標置於文件首
- If .Selection.Find.Execute(Str1) Then '查找到指定字串
- .Selection.Font.Color = wdColorAutomatic '字元為自動顏色
- .Selection.Text = Str2 '替換字串
- End If
- End With
- Word對象.Documents.Save
- Word對象.Quit
- Set Word對象 = Nothing
- Next i
- If 判斷 = 0 Then
- i = MsgBox("已輸出到 Word 檔!", 0 + 48 + 256 + 0, "提示:")
- End If
- End Sub
复制代码
|
|