我要感谢大家提前关心我的问题。
如果下面发布的代码效率不高,请注意我刚开始使用vba而且从未有过编程课程。请原谅我的Englisch。另请注意,我在这里咨询了Google和内部搜索。
来到问题:
Userform visualization for understanding
(1) 我有一个combobx“CGselectionStrategies”,它应该是下面输入文本框的基础。启动用户窗体时,我希望它显示这些框的上一个输入,具体取决于Combobox选择。
使用以下代码将输入保存在工作表“商品组”中:
Private Sub SaveCGStrategies_Click()
'Just general stuff
Dim outputBook As Workbook
Set outputBook = ActiveWorkbook
'Note-fields for PU Strategies, incl. Authors
Dim CGselectionStrategies As String
Dim NoteTargetMarket As String
Dim AuthorTargetMarket As String
Dim NotePUMStrategy As String
Dim AuthorPUMStrategy As String
Dim NotePUSStrategy As String
Dim AuthorPUSStrategy As String
Dim NotePULStrategy As String
Dim AuthorPULStrategy As String
CGselectionStrategies = Me.CGselectionStrategies
NoteTargetMarket = Me.NoteTargetMarket
AuthorTargetMarket = Me.NoteAuthorMarketInfo
NotePUMStrategy = Me.NotePUMStrat
AuthorPUMStrategy = Me.NoteAuthorPUMStratInfo
NotePUSStrategy = Me.NotePUSStrat
AuthorPUSStrategy = Me.NoteAuthorPUSStratInfo
NotePULStrategy = Me.NotePULStrat
AuthorPULStrategy = Me.NoteAuthorPULStratInfo
'Save CG Strategies behind them in the List on CG Worksheet
outputBook.Activate
outputBook.Worksheets("Commodity Groups").Select
With Me.CGselectionStrategies
If Me.CGselectionStrategies.value = "Halbzeuge (und Rohstoffe)" Then
Range("K2").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Mechanische Konstruktionsteile" Then
Range("K62").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Norm- und Katalogteile (ausser Elektro)" Then
Range("K87").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Elektrische, elektronische und optische Komponenten und Baugruppen" Then
Range("K127").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Hilfs-, Betriebs- und Produktionshifsmittel" Then
Range("K180").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Subsysteme und Anlagen" Then
Range("K256").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Handelsware" Then
Range("K299").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Dienstleistungen" Then
Range("K310").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
If Me.CGselectionStrategies.value = "Allgemeines und Administration" Then
Range("K360").Select
ActiveCell.value = NoteTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = AuthorTargetMarket
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUMStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUMStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePUSStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPUSStratInfo
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NotePULStrat
ActiveCell.Offset(0, 1).Select
ActiveCell.value = NoteAuthorPULStratInfo
End If
End With
End Sub
我的方法如下......
'Show old Strategies when selecting a combobox-item
'Start with short Text "Please choose a Commodity Group"
If Me.CGselectionStrategies.value = "" Then
Me.NoteTargetMarket.Text = CStr(ThisWorkbook.Sheets("Commodity Groups").Range("K445").value)
Me.Next Variable
Me.Next Variable
Me.Next Variable
End If
If Me.CGselectionStrategies.value = "Halbzeuge (und Rohstoffe)" Then
Me.NoteTargetMarket.Text = CStr(ThisWorkbook.Sheets("Commodity Groups").Range("K2").value)
Me.Next Variable
Me.Next Variable
Me.Next Variable
End If
......等等。不用说它不起作用。我在网上发现了以下内容,并试图尽我所能调整它,但没有成功。
'Change Textbot Content based on Combobox selection
Dim wks As Excel.Worksheet
Dim selectedString As Variant
Dim row As Long
Dim value As Variant
Set wks = Worksheets("Commodity Groups")
If CGselectionStrategies.ListIndex <> -1 Then
selectedString = CGselectionStrategies.value
On Error Resume Next
row = Application.WorksheetFunction.Match(selectedString, wks.Columns(1), 0)
On Error GoTo 0
If row Then
value = wks.Cells(row, 2)
DomainOwnerTestBox.value = value
Else
'Value not found in the worksheet 'test'
End If
End If
End Sub
一个问题是,有多个输入值,不仅在第2列中,它们也被许多其他行分开。 我希望以一种可以理解的方式解释我的问题。
(2) 我的第二个问题是更短,关于如何避免必须填写用户表单中的所有文本框。问题是有超过200个输入要填写,每当我想测试,即输入在数据库中的位置时,我得到运行时13错误“类型不匹配”。但是,如果我在每个框中放置一个输入,它会顺利运行。这里的代码摘录了我如何从userform输入中保存数据:
Dim Datum As Date
Dim SName As String
Dim PotentialS As String
Dim SuppNr As Long
Dim Active As String
Datum = Me.TextBox117
SName = Me.SuppName
PotentialS = Me.PotentialS
SuppNr = Me.SuppNo
Active = Me.Active
'Go to the first empty line on the output sheet (Meta DB) in this workbook
outputBook.Activate
outputBook.Worksheets("Meta DB").Range("A3").Select
If outputBook.Worksheets("Meta DB").Range("A3").Offset(1, 0) <> "" Then
outputBook.Worksheets("Meta DB").Range("A3").End(xlDown).Select
End If
'Go to A4 and from there always one below the last filled cell in A
ActiveCell.Offset(1, 0).Select
DatabaseRow = ActiveCell.row
'Post Values for new Entry
'Add a New Supplier Tab - Supplier Profile
ActiveCell.value = Datum
ActiveCell.Offset(0, 1).Select
ActiveCell.value = SName
ActiveCell.Offset(0, 1).Select
ActiveCell.value = PotentialS
ActiveCell.Offset(0, 1).Select
ActiveCell.value = SuppNr
ActiveCell.Offset(0, 1).Select
欢迎任何帮助和提示。祝大家愉快。
答案 0 :(得分:0)
嗨,有一件事是肯定的 - 你需要摆脱所有这些<project name>.exe.config
的
他们难以理解。我自己也刚刚开始学习VBA(大约3周前)。请查看以下链接 - How to avoid using Select in Excel VBA macros。它应该可以帮助您提高VBA的可读性。它还将使您的代码速度提高数十亿倍。
这也可以帮助您不得不每隔一个命令使用.Select
。
另一个提示是,而不是你有
ActiveCell
作为您在开始时开始的一行代码 - 如果你想命名一个单元格的东西,只需给它的范围,然后使它=“字符串”,例如。
Dim *StringVariable* as string
我对VBA的经验不足以了解你的TextBox有什么问题,但我希望这是一个很好的开始,可以帮助你进行一般的VBA写作。
答案 1 :(得分:0)
首先我认为缩短SaveCGStrategies_Click
代码将有助于更好地理解VBA,你所做的是逐个检查每个保存值的选项,但考虑选择第一个选项,那么你永远不会需要检查其他人,因为你会发现你的匹配,代码也每次都重复,下面检查选择并运行一次代码实例,但是针对相关的单元格。
Private Sub SaveCGStrategies_Click()
Dim LngRow As Long
Dim outputBook As Workbook
Dim outputSheet As Worksheet
Set outputBook = ActiveWorkbook
Set outputSheet = outputBook.Worksheets("Commodity Groups")
'With Me.CGselectionStrategies
Select Case Me.CGselectionStrategies.Value
Case "Halbzeuge (und Rohstoffe)"
LngRow = 2
Case "Mechanische Konstruktionsteile"
LngRow = 62
Case "Norm- und Katalogteile (ausser Elektro)"
LngRow = 87
Case "Elektrische, elektronische und optische Komponenten und Baugruppen"
LngRow = 127
Case "Hilfs-, Betriebs- und Produktionshifsmittel"
LngRow = 180
Case "Subsysteme und Anlagen"
LngRow = 256
Case "Handelsware"
LngRow = 299
Case "Dienstleistungen"
LngRow = 310
Case "Allgemeines und Administration"
LngRow = 360
End Select
outputSheet.Cells(LngRow, 11) = Me.NoteTargetMarket
outputSheet.Cells(LngRow, 13) = Me.NoteAuthorMarketInfo
outputSheet.Cells(LngRow, 14) = Me.NotePUMStrat
outputSheet.Cells(LngRow, 15) = Me.NoteAuthorPUMStratInfo
outputSheet.Cells(LngRow, 16) = Me.NotePUSStrat
outputSheet.Cells(LngRow, 17) = Me.NoteAuthorPUSStratInfo
outputSheet.Cells(LngRow, 18) = Me.NotePULStrat
outputSheet.Cells(LngRow, 19) = Me.NoteAuthorPULStratInfo
Set outputSheet = Nothing
Set outputBook = Nothing
End Sub
与引用工作簿的方式相同,它还引用了工作表,使我们能够使用更少的代码写入我们想要的工作表范围。我没有使用您拥有的.Select
和Activate
函数,因为这些函数可能会出现性能问题。我还直接引用了这些值,而不是先将它们放在变量中,如果你打算在将它们写入单元格之前对它们进行操作,那么变量可能会有用,但如果它是从文本框到单元格的直接插入,我们可以直接通过。
您的第二个问题需要更多输入才能确定,但我怀疑与数据类型有关。
Dim Datum As Date
Datum = Me.TextBox117
Me.TextBox117
是否为有效日期格式的日期?这可以检查如下: -
If IsDate(Me.TextBox117) then Datum = CDate(Me.TextBox117)
函数CDate
确保将值作为日期传递给变量。
Dim SuppNr As Long
SuppNr = Me.SuppNo
Me.SuppNo
是有效号码吗?这可以检查如下: -
If IsNumeric(Me.SuppNo) then SuppNr = CLng(Me.SuppNo)
我的建议是在让它工作的同时将它们全部设置为String
或按原样传递它们。