Beruflich Dokumente
Kultur Dokumente
1.
2. 'Set report sheet name based on form name (duplicate names not allowed
...)
3. wksReport.Name = Mid(Me.Name, 4)
4.
5. End Sub
6.
1.
2. 'Set report sheet name based on form name (duplicate names not allowed
...)
3. wksReport.Name = Mid(Me.Name, 4)
4.
5. 'Copy text fields
6. wksReport.range("E4") = txtPro.Text
7. wksReport.range("l4") = txtLine.Text
8.
9. 'Save workbook as whatever is in "sSaveAsFileName" variable
10. wbkReport.SaveAs FileName:= _
11. sSaveAsFileName, FileFormat:= _
12. xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:
=False _
13. , CreateBackup:=False
14.
15.
1. 'Set report sheet name based on form name (duplicate names not allowed
...)
2. wksReport.Name = Mid(Me.Name, 4)
3.
1. if wksReport.Name <> Mid(Me.Name, 4) then wksReport.Name = Mid(Me.Name, 4)
1. 'Set report sheet name based on form name (duplicate names not allowed
...)
2. wksReport.Name = Mid(Me.Name, 4)
3.
1. Option Explicit
2. Const UseNewSheetPerSave As Boolean = False
3. Private Sub Command1_Click()
4. 'Requires reference: Microsoft Excel (version) Object Library
5. Dim XLapp As New Excel.Application
6. XLapp.Visible = True
7. Dim wbkReport As Excel.Workbook
8. Dim wbkTemplate As Excel.Workbook
9. Dim wksReport As Excel.Worksheet
10. Dim wksTemplate As Excel.Worksheet
11.
12. Dim sOpenFileName As String, sSaveAsFileName As String, iSheet As Inte
ger, irange As Integer, i As Integer
13.
14.
15. 'Set path to template file here
16. 'sOpenFileName = App.Path & "\" & "root\Root.xls"
17. sOpenFileName = "C:\bytesdotcom\vbformToExcel\template.xls"
18.
19. 'Set your path to save file here:
20. 'sSaveAsFileName = App.Path & "\" & "Arhiva - " & Replace(Date, "/", "
-") & ".xls"
21. sSaveAsFileName = "C:\bytesdotcom\vbformToExcel\daily - " & Replace(Da
te, "/", "-") & ".xls"
22.
23. 'Open template
24. Set wbkTemplate = XLapp.Workbooks.Open(sOpenFileName)
25.
26. 'Suppress Excel warnings
27. XLapp.DisplayAlerts = False
28.
29. If Dir(sSaveAsFileName) <> "" Then
30. 'if a report from today exists, use it as the template
31. Set wbkReport = XLapp.Workbooks.Open(sSaveAsFileName)
32. Set wksTemplate = wbkTemplate.Sheets(1)
33.
34. If UseNewSheetPerSave = True Then
35. wksTemplate.Copy after:=wbkReport.Sheets(wbkReport.Sheets.Coun
t)
36. Set wksReport = wbkReport.Sheets(wbkReport.Sheets.Count)
37. Else
38. Set wksReport = wbkReport.Sheets(1)
39. End If
40.
41. wbkTemplate.Close SaveChanges:=False
42. Else
43. 'This is the first report of the day
44. Set wbkReport = wbkTemplate
45. 'Set reference to first sheet in workbook
46. Set wksReport = wbkReport.Sheets(1)
47. 'Delete extra sheets from template
48. If wbkReport.Sheets.Count > 1 Then
49. 'Start deleting extra sheets (only in this 'copy' of the templ
ate)
50. 'if they exist, starting from last sheet
51. While wbkReport.Sheets.Count > 1
52. wbkReport.Sheets(2).Delete
53. Wend
54. End If
55. End If
56.
57. 'Set report sheet name based on form name (duplicate names not allowed
...)
58.
59.
60.
61. ' Copy text fields
62. 'wksReport.Range("A2") = Text1.Text
63. 'wksReport.Range("B2") = Text2.Text
64. Dim FirstCellOfNewRow As Range
65.
66. ' This is the same as going to A65536, pressing CTRL+Up Arrow, then re
leasing CTRL and hitting down arrow to select the first cell of a new row.
67. Set FirstCellOfNewRow = wksReport.Range("A65536").End(xlUp).Offset(1,
0)
68.
69. FirstCellOfNewRow.Value = TextBox1.Text
70. FirstCellOfNewRow.Offset(0, 1).Value = TextBox2.Text
71. FirstCellOfNewRow.Offset(0, 2).Value = "a third value could go here"
72. FirstCellOfNewRow.Offset(0, 3).Value = "a fourth value could go here"
73. FirstCellOfNewRow.Offset(0, 4).Value = "a fifth value could go here"
74.
75. 'Save workbook as whatever is in "sSaveAsFileName" variable
76. wbkReport.SaveAs Filename:= _
77. sSaveAsFileName, FileFormat:= _
78. xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:
=False _
79. , CreateBackup:=False
80.
81. 'Close workbook
82. wbkReport.Close SaveChanges:=False
83.
84. 'Allow Excel warnings
85. XLapp.DisplayAlerts = True
86.
87. 'Remove references to sheets/workbooks before quitting (was hanging a
second on wksreport if Excel was closed already)
88. Set wksReport = Nothing
89. Set wbkReport = Nothing
90. Set wksTemplate = Nothing
91. Set wbkTemplate = Nothing
92.
93. 'Close Excel
94. XLapp.Quit
95.
96. 'Remove final reference
97. Set XLapp = Nothing
98.
99.
100. End Sub
101.
http://p2p.wrox.com/vb-how/30-read-write-excel-file-using-vb6.html