Вы находитесь на странице: 1из 6

Dim fso As New FileSystemObject Dim stream As TextStream Dim iLoop As Integer iLoop = 1 While Sheets("Sheet1").

Range("A" & iLoop) <> "" Set stream = fso.CreateTextFile("C:\Row" & iLoop & ".txt", True) stream.WriteLine "Col1 " & Sheets("Sheet1").Range("A" & iLoop) stream.WriteLine "Col2 " & Sheets("Sheet1").Range("B" & iLoop) stream.Close iLoop = iLoop + 1 Wend

A base idea for VBScript (stand-alone .vbs file)...


'connect to Excel Set XLApp = CreateObject("Excel.Application") XLApp.Visible = True 'open the file XLApp.DisplayAlerts = False Set XLBook = XLApp.WorkBooks.Open("c:\path_to\my.xls") XLApp.DisplayAlerts = True 'if you know the Worksheet name: sSheetName = "MySheetName" Set XLSheet = XLBook.Sheets.Item(sSheetName) 'else use index: Set XLSheet = XLBook.Worksheets(1) 'or if you want to process all sheets: For Iter = 1 To XLBook.Worksheets.Count Set XLSheet = XLBook.Worksheets(Iter) ... Next 'you can use XLSheet.Columns.Count to iterate 'or if the columns names are known: Set Col1 = XLSheet.Columns("Col1") Set Col2 = XLSheet.Columns("Col2") 'or get Range: Set Range = XLSheet.Columns("Col1:Col2") 'same as above: Set Range = XLSheet.Range("Col1:Col2") 'vbs FileSystemObject: Set fso = CreateObject("Scripting.FileSystemObject")

Building the Automation Sample

1. Start Visual Basic and create a new Standard EXE project. Form1 is created by default. 2. ClickProject and then click References. The References dialog box appears. Scroll down the list until you find Microsoft Excel object library, and then select the item to add a reference to Excel. If the correct object library for your version of Excel does not appear in the list, make sure that you have your version of Excel properly installed. Notes If you are automating Microsoft Office Excel 2007, the type library appears as Microsoft Excel 12.0 Object Library in the References list. o If you are automating Microsoft Office Excel 2003, the type library appears as Microsoft Excel 11.0 Object Library in the References list. o If you are automating Microsoft Excel 2002, the type library appears as Microsoft Excel 10.0 Object Library in the References list o If you are automating Microsoft Excel 2000, the type library appears as Microsoft Excel 9.0 Object Library in the References list. o If you are automating Microsoft Excel 97, the type library appears as Microsoft Excel 8.0 Object Library in the References list 3. Click OK to close the References dialog box. 4. Add a CommandButton to Form1. 5. In the code window for Form1, insert the following code:
o 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. Option Explicit Private Sub Command1_Click() Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As Excel.Range 'On Error GoTo Err_Handler ' Start Excel and get Application object. Set oXL = CreateObject("Excel.Application") oXL.Visible = True ' Get a new workbook. Set oWB = oXL.Workbooks.Add Set oSheet = oWB.ActiveSheet ' Add table headers going cell by cell. oSheet.Cells(1, 1).Value = "First Name" oSheet.Cells(1, 2).Value = "Last Name" oSheet.Cells(1, 3).Value = "Full Name" oSheet.Cells(1, 4).Value = "Salary" ' Format A1:D1 as bold, vertical alignment = center.

33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52.

With oSheet.Range("A1", "D1") .Font.Bold = True .VerticalAlignment = xlVAlignCenter End With ' Create an array to set multiple values at once. Dim saNames(5, 2) As String saNames(0, 0) = "John" saNames(0, 1) = "Smith" saNames(1, 0) = "Tom" saNames(1, 1) = "Brown" saNames(2, 0) = "Sue" saNames(2, 1) = "Thomas" saNames(3, 0) = "Jane" saNames(3, 1) = "Jones" saNames(4, 0) = "Adam" saNames(4, 1) = "Johnson"

' Fill A2:B6 with an array of values (First and Last Names). 53. oSheet.Range("A2", "B6").Value = saNames 54. 55. ' Fill C2:C6 with a relative formula (=A2 & " " & B2). 56. Set oRng = oSheet.Range("C2", "C6") 57. oRng.Formula = "=A2 & "" "" & B2" 58. 59. ' Fill D2:D6 with a formula(=RAND()*100000) and apply format. 60. Set oRng = oSheet.Range("D2", "D6") 61. oRng.Formula = "=RAND()*100000" 62. oRng.NumberFormat = "$0.00" 63. 64. ' AutoFit columns A:D. 65. Set oRng = oSheet.Range("A1", "D1") 66. oRng.EntireColumn.AutoFit 67. 68. ' Manipulate a variable number of columns for Quarterly Sales Data. 69. Call DisplayQuarterlySales(oSheet) 70. 71. ' Make sure Excel is visible and give the user control 72. ' of Microsoft Excel's lifetime. 73. oXL.Visible = True 74. oXL.UserControl = True 75. 76. ' Make sure you release object references. 77. Set oRng = Nothing 78. Set oSheet = Nothing 79. Set oWB = Nothing 80. Set oXL = Nothing 81. 82. Exit Sub 83. Err_Handler: 84. MsgBox Err.Description, vbCritical, "Error: " & Err.Number 85. End Sub 86.

87.

Private Sub DisplayQuarterlySales(oWS As Excel.Worksheet) 88. Dim oResizeRange As Excel.Range 89. Dim oChart As Excel.Chart 90. Dim iNumQtrs As Integer 91. Dim sMsg As String 92. Dim iRet As Integer 93. 94. ' Determine how many quarters to display data for. 95. For iNumQtrs = 4 To 2 Step -1 96. sMsg = "Enter sales data for" & Str(iNumQtrs) & " quarter(s)?" 97. iRet = MsgBox(sMsg, vbYesNo Or vbQuestion _ 98. Or vbMsgBoxSetForeground, "Quarterly Sales") 99. If iRet = vbYes Then Exit For 100. Next iNumQtrs 101. 102. 103. sMsg = "Displaying data for" & Str(iNumQtrs) & " quarter(s)." 104. MsgBox sMsg, vbMsgBoxSetForeground, "Quarterly Sales" 105. 106. ' Starting at E1, fill headers for the number of columns selected. 107. Set oResizeRange = oWS.Range("E1", "E1").Resize(ColumnSize:=iNumQtrs) 108. 109. oResizeRange.Formula = "=""Q"" & COLUMN()-4 & CHAR(10) & ""Sales""" 110. 111. ' Change the Orientation and WrapText properties for the headers. 112. oResizeRange.Orientation = 38 113. oResizeRange.WrapText = True 114. 115. ' Fill the interior color of the headers. 116. oResizeRange.Interior.ColorIndex = 36 117. 118. ' Fill the columns with a formula and apply a number format. 119. Set oResizeRange = oWS.Range("E2", "E6").Resize(ColumnSize:=iNumQtrs) 120. oResizeRange.Formula = "=RAND()*100" 121. oResizeRange.NumberFormat = "$0.00" 122. 123. ' Apply borders to the Sales data and headers. 124. Set oResizeRange = oWS.Range("E1", "E6").Resize(ColumnSize:=iNumQtrs) 125. oResizeRange.Borders.Weight = xlThin 126. 127. ' Add a Totals formula for the sales data and apply a border. 128. Set oResizeRange = oWS.Range("E8", "E8").Resize(ColumnSize:=iNumQtrs) 129. oResizeRange.Formula = "=SUM(E2:E6)" 130. With oResizeRange.Borders(xlEdgeBottom) 131. .LineStyle = xlDouble 132. .Weight = xlThick

133. End With 134. 135. ' Add a Chart for the selected data 136. Set oResizeRange = oWS.Range("E2:E6").Resize(ColumnSize:=iNumQtrs) 137. Set oChart = oWS.Parent.Charts.Add 138. With oChart 139. .ChartWizard oResizeRange, xl3DColumn, , xlColumns 140. .SeriesCollection(1).XValues = oWS.Range("A2", "A6") 141. For iRet = 1 To iNumQtrs 142. .SeriesCollection(iRet).Name = "=""Q" & Str(iRet) & """" 143. Next iRet 144. .Location xlLocationAsObject, oWS.Name 145. End With 146. 147. ' Move the chart so as not to cover your data. 148. With oWS.Shapes("Chart 1") 149. .Top = oWS.Rows(10).Top 150. .Left = oWS.Columns(2).Left 151. 152. End With 153. 154. ' Free any references. 155. Set oChart = Nothing 156. Set oResizeRange = Nothing 157. 158. End Sub 159.

160.

Press F5 to run the project.

Mulai Visual Basic dan membuat proyek EXE Standar baru. Form1 dibuat secara default. ClickProject dan kemudian klik Referensi. Para Referensi kotak dialog muncul. Gulir ke bawah daftar sampai Anda menemukan Microsoft Excel perpustakaan objek, dan kemudian pilih item untuk menambahkan referensi ke Excel. Jika perpustakaan objek yang benar untuk versi Excel tidak muncul dalam daftar, pastikan bahwa Anda memiliki versi Excel Anda terinstal dengan benar. Catatan

Jika Anda sedang mengotomatisasi Microsoft Office Excel 2007, perpustakaan jenis muncul sebagai Microsoft Excel 12.0 Object Library dalam daftar Referensi. Jika Anda sedang mengotomatisasi Microsoft Office Excel 2003, perpustakaan jenis muncul sebagai Microsoft Excel 11.0 Object Library dalam daftar Referensi. Jika Anda sedang mengotomatisasi Microsoft Excel 2002, perpustakaan jenis muncul sebagai Microsoft Excel 10,0 Perpustakaan objek dalam daftar Referensi Jika Anda sedang mengotomatisasi Microsoft Excel 2000, perpustakaan jenis muncul sebagai Microsoft Excel 9.0 perpustakaan objek dalam daftar Referensi. Jika Anda sedang mengotomatisasi Microsoft Excel 97, perpustakaan jenis muncul sebagai Microsoft Excel 8.0 perpustakaan objek dalam daftar Referensi Klik OK untuk menutup kotak dialog Referensi. Tambah CommandButton pada Form1. Dalam jendela kode untuk Form1, masukkan kode berikut:

Вам также может понравиться