我创建了一个表单,用户通过单击表格中显示的单选按钮来评估某个事件的频率(总是,有时,很少,从不,N / A)。单选按钮是加权的,因此如果用户总是选择,则该问题的数值为4(有时= 3等)。我需要从调查中获取结果并将其放入一份excel文档中,该文档对许多调查的结果进行平均。目前我有一个文本框打印调查结束时的数字,我将它们复制并粘贴到我的Excel文档中,但我想找到一个更好的方法来做到这一点。理想情况下,如果我可以创建一个单独的纯文本文档,其中的单词只包含调查中的数字结果,可以导入到excel中,这将是完美的。下面是我的代码示例:如何计算单选按钮以及如何打印它们
Private Sub RadioButtonyj_Click()
If RadioButtonyj.Value = True Then
num33 = 2
UpdateRating
End If
End Sub
Private Sub RadioButtonnj_Click()
If RadioButtonnj.Value = True Then
num33 = 1
UpdateRating
End If
End Sub
Private Sub RadioButtonnj1_Click()
If RadioButtonnj1.Value = True Then
num33 = 0
UpdateRating
End If
End Sub
Private Sub UpdateRating()
TextBox2.Text = num1 & vbNewLine & num2 & vbNewLine & _
num3 & vbNewLine & num4 & vbNewLine & _
num5 & vbNewLine & num6 & vbNewLine & _
num7 & vbNewLine & num8 & vbNewLine & num9 & vbNewLine & num10 & vbNewLine & num11 _
& vbNewLine & num12 & vbNewLine & num13 & vbNewLine & num14 & vbNewLine _
& num15 & vbNewLine & num16 & vbNewLine & num17 & vbNewLine & num18 & vbNewLine & num19 & vbNewLine _
& num20 & vbNewLine & num21 & vbNewLine & num22 & vbNewLine & num23 & vbNewLine _
& num24 & vbNewLine & num25 & vbNewLine & num26 & vbNewLine & num27 & vbNewLine & num28 & vbNewLine _
& num29 & vbNewLine & num30 & vbNewLine & num31 & vbNewLine & num32 & vbNewLine & num33
End Sub
Dim FS As FileSystemObject
Set FS = New FileSystemObject
Dim MyFile As TextStream
Set MyFile = FS.CreateTextFile("C:\Folder\FileName.txt")
MyFile.Write num1 & vbNewLine & num2 & vbNewLine & _
num3 & vbNewLine & num4 & vbNewLine & _
num5 & vbNewLine & num6 & vbNewLine & _
num7 & vbNewLine & num8 & vbNewLine & num9 & vbNewLine & num10 & vbNewLine & num11 _
& vbNewLine & num12 & vbNewLine & num13 & vbNewLine & num14 & vbNewLine _
& num15 & vbNewLine & num16 & vbNewLine & num17 & vbNewLine & num18 & vbNewLine & num19 & vbNewLine _
& num20 & vbNewLine & num21 & vbNewLine & num22 & vbNewLine & num23 & vbNewLine _
& num24 & vbNewLine & num25 & vbNewLine & num26 & vbNewLine & num27 & vbNewLine & num28 & vbNewLine _
& num29 & vbNewLine & num30 & vbNewLine & num31 & vbNewLine & num32 & vbNewLine & num33
If FS.FileExists("C:\Folder\FileName.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName2.txt")
End If
If FS.FileExists("C:\Folder\FileName2.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName3.txt")
End If
If FS.FileExists("C:\Folder\FileName3.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName4.txt")
End If
If FS.FileExists("C:\Folder\FileName4.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName5.txt")
End If
If FS.FileExists("C:\Folder\FileName5.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName6.txt")
End If
If FS.FileExists("C:\Folder\FileName6.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName7.txt")
End If
If FS.FileExists("C:\Folder\FileName7.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName8.txt")
End If
If FS.FileExists("C:\Folder\FileName8.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName9.txt")
End If
If FS.FileExists("C:\Folder\FileName9.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName10.txt")
End If
If FS.FileExists("C:\Folder\FileName10.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName11.txt")
End If
If FS.FileExists("C:\Folder\FileName11.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName12.txt")
End If
If FS.FileExists("C:\Folder\FileName12.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName13.txt")
End If
If FS.FileExists("C:\Folder\FileName13.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName14.txt")
End If
If FS.FileExists("C:\Folder\FileName14.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName15.txt")
End If
If FS.FileExists("C:\Folder\FileName15.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName16.txt")
End If
If FS.FileExists("C:\Folder\FileName16.txt") Then
Set MyFile = FS.CreateTextFile("C:\Folder\FileName17.txt")
End If
End Sub
答案 0 :(得分:0)
要通过Word中的VBA打开并运行excel,请访问VBA编辑器:
Tools > References... > add Microsoft Excel <some version> Object Library
正如已经有一个Microsoft Word <some version> Object Library
已经与Microsoft Office <some version> Object Library
和其他一些人一起被引用一样。
然后您就可以在代码中使用excel对象,例如:
Dim ExApp as Excel.Application
Set ExApp = new Excel.Application
'Workbooks represent excel files
Dim MyBook as Workbook
Set MyBook = ExApp.Workbooks.Open(filename) 'or .Add and other options
'Worksheets represente excel sheets
Dim MySheet as Worksheet
Set MySheet = MyBook.Worksheets(some name or index)
'Ranges represent cells or a range of cells
Dim MyCell as Excel.Range
Set MyCell = MySheet.Range("A1") 'or .Cells(1,1) and others
'Notice that excel application will remain invisible until you do this
ExApp.Visible = true
有关从word到excel导入某些数据的更详细解答,请转到: Importing specific data from a Word to Excel
要创建纯文本文件,请转到并添加对
的引用Microsoft.Scripting.Runtime
这样您就可以使用FileSystemObject
,File
,Folder
和其他人。
FileSystemObject
有一个CreateTextFile
方法可以帮助您实现这一目标。
Dim FS as FileSystemObject
Set FS = new FileSystemObject
Dim MyFile As TextStream
Set MyFile = FS.CreateTextFile("C:\Folder\FileName.txt")
然后你可以进一步使用方法来添加行并保存它。
要检查文件是否存在且不覆盖它,请执行以下循环:
Dim FS As FileSystemObject
Dim MyFile As TextStream
Dim i As Integer
Dim FilePrefix As String, FileName As String, Extension As String
FilePrefix = "C:\Folder\FileName"
Extension = ".txt"
i = 1
FileName = FilePrefix & Trim(Str(i)) & Extension
Do While (FS.FileExists(FileName))
i = i + 1
FileName = FilePrefix & Trim(Str(i)) & Extension
Loop
Set MyFile = FS.CreateTextFile(FileName)
'Do the writings
请注意,您不必创建所有文件,只需在检查完毕后创建新文件即可。只有在设置MyFile