0

我是 VBA 的新手。如果我的问题看起来很愚蠢,请原谅。

我想做的是在 Excel 中创建一个用于提取期货价格数据的用户表单界面。我有大约一百个不同的电子表格,其中期货价格命名为 08012、08011 等。我有类似的电子表格,其中包含对相同期货的预测,它们也以相同的方式命名。我想创建这样的图形用户界面,可以输入股票代码、日期范围以及预测或实际价格,并从正确的电子表格中获取所需的系列。我最大的问题是我是 VBA 的新手,所以我什至不知道这是否可以解决 Excel 中的这类问题。

我一直在阅读一些文献,我意识到我需要一个带有 2 个文本框的用户表单界面(一个用于股票代码查看,一个用于时间范围)以及一个选项框(用于决定是否显示预测或实际价格) . 但这就是我无法真正理解如何传递用户在程序中进一步输入的数据的全部内容,因此 Excel 确实从正确的电子表格中获取了正确的库存。

任何提示,类似问题的链接都将非常受欢迎!抱歉,问题描述不清楚。请询问您需要的任何详细信息。这是我的代码:

Private Sub CommandButton1_Click()
Dim a As String
Dim aa As String
Dim myP As Range
Dim myC As Range
Dim t1 As String
Dim tper() As Variant
Dim tper2() As Variant
Dim tper3() As Variant
Dim t2 As Long
Dim t3 As Long
Dim r As Range
Dim rr As Range
Dim rrr As Range
Dim myMax As Long
Dim FindMax As Long
Dim myMin As Long
Dim FindMin As Long
Dim strPath As String
a = TextBox1.Value
If TextBox1.Value = "" Then
MsgBox ("Ange Serie")
Exit Sub
End If
aa = TextBox4.Value
If TextBox4.Value = "" Then
MsgBox ("Ange Prognosår")
Exit Sub
t1 = TextBox2.Value
If TextBox2.Value = "" Then
MsgBox ("Ange Startdatum")
Exit Sub
End If
t2 = Val(t1)
t1 = TextBox3.Value
If TextBox3.Value = "" Then
MsgBox ("Ange Slutdatum")
Exit Sub
End If
t3 = Val(t1)
If OptionButton1.Value = True Then
'Sheets("utfall").Activate
strPath = "C:\Users\dmyshe\Desktop\Dmytro\Utfall.xls"
Workbooks.Open Filename:=strPath, UpdateLinks:=False
ElseIf OptionButton2.Value = True Then
'Sheets("prognoser").Activate
strPath = "C:\Users\dmyshe\Desktop\Dmytro\Prognoser.xls"
Workbooks.Open Filename:=strPath, UpdateLinks:=False
Else
MsgBox ("Fel")
End If
'Application.AskToUpdateLinks = False
Sheets("" & aa).Activate
tper = Range("A3:A500")
tper2 = Range("CL3:CL500")
tper3 = Range("CL3:CL500")
For i = 1 To UBound(tper, 1)
tper(i, 1) = year(tper(i, 1))
Next i
For i = 1 To UBound(tper, 1)
If tper(i, 1) >= t2 Then
tper2(i, 1) = i
End If
Next i
FindMin = Application.Min(tper2)
For i = 1 To UBound(tper, 1)
If tper(i, 1) <= t3 And tper(i, 1) <> 1899 Then
tper3(i, 1) = i
End If
Next i
FindMax = Application.Max(tper3)
Set r = Rows(2).Find("" & a, , xlValues, xlWhole).EntireColumn
Range("CM1:CM" & r.Rows.Count) = r.Value
Set rr = Range("CM" & (FindMin + 1) & ":CM" & (FindMax + 1))
Set rrr = Range("A" & (FindMin + 2) & ":A" & (FindMax + 2))
ThisWorkbook.Activate
Sheets("Blad3").Activate
Range("A:A").Cells.Clear
Range("B:B").Cells.Clear
Range("A1") = "Datum"
Range("B1") = "" & a
Range("A1").Font.Bold = True
Range("B1").Font.Bold = True
Range("B2:B" & (rr.Rows.Count + 1)) = rr.Value
Range("A2:A" & (rrr.Rows.Count + 1)) = rrr.Value
If OptionButton1.Value = True Then
Workbooks("Utfall").Close False
ElseIf OptionButton2.Value = True Then
Workbooks("Prognoser").Close False
End If
End Sub
4

1 回答 1

0

链接将教您如何创建表单和编写基本的 VBA 代码。对于您的用户表单界面,您将需要两个用于日期范围的文本框和用于提交代码的按钮。

于 2013-02-08T19:54:05.383 回答