0

我创建了一个表单,用户通过单击表格中显示的单选按钮来评估某个事件的频率(总是、有时、很少、从不、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
4

1 回答 1

0

在 Word 中通过 VBA 打开和操作 excel,进入 VBA 编辑器:

Tools > References... > add Microsoft Excel <some version> Object Library

正如已经有一个对应的Microsoft Word <some version> Object Library已经与 aMicrosoft 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 的更详细答案,请参阅: 将特定数据从 Word 导入 Excel

要创建纯文本文件,请添加对以下内容的引用:

Microsoft.Scripting.Runtime

这将允许您与FileSystemObjectFileFolder其他人一起工作。有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

于 2013-07-01T15:23:11.247 回答