目标
我想循环浏览三张(我从一张开始),在C列中查找某个类型并自动填充/自动填充In-cell下拉列表(类似于数据验证设置中的那个)在列D中。单元格下拉列表应列出所有类型的值,但应自动填充属于类型的值。
问题
下面的代码使用相同的值填充每个In-cell下拉列表,即Type1的Item1 - Item2 - Item3 - Item4。
我不知道如何列出所有值,同时自动填充单元格。
所需的输出
代码
为简单起见,我在下面的代码中只添加了两个第一类。
Sub AutoDropdown()
Dim PersonSource As Range
Dim PersonSourceTotal As Range
Dim PersonCell As Range
'Dim ws As Worksheet
Dim i As Integer
Dim lastRow As Integer
Set PersonSourceTotal = Sheets("Sheet1").Range("D2:D200")
With PersonSourceTotal.Offset(0, -2)
lastRow = .Cells(.Rows.Count, PersonSourceTotal.Columns.Count).End(xlUp).Row
End With
Set PersonSource = Sheets("sheet1").Range("D2:D" & lastRow)
On Error Resume Next
For Each PersonCell In PersonSource
Name = PersonCell.Offset(0, -3)
ID = PersonCell.Offset(0, -2)
If Name <> "" And ID <> "" Then
For i = 0 To lastRow
If PersonCell.Offset(i, -1) = "Type1" Then
arr1 = Array("Item1", "Item2", "Item3", "Item4")
arr1Merged = Join(arr1, "--")
With PersonCell.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=arr1Merged
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
ElseIf PersonCell.Offset(i, -1) = "Type2" Then
arr2 = Array("Item5", "Item6", "Item7", "Item8", "Item9")
arr2Merged = Join(arr2, "--")
Debug.Print (arr2Merged)
With PersonCell.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=arr2Merged
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
Next i
Else
MsgBox "Remember to add Name and ID"
End If
Next PersonCell
End Sub
答案 0 :(得分:1)
修改强>
发表评论后,我更新了代码,以便更好地反映您的要求:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication71 extends Application
{
String[] questionBank =
{
"Connie has 15 red marbles and 28 blue marbles. How many more blue marbles than red marbles does Connie have?",
"Connie has 15 red marbles and some blue marbles. She has 13 more blue marbles than red ones. How many blue marbles does Connie have?",
"Connie has 28 blue marbles. She has 13 more blue marbles than red ones. How many red marbles does Connie have?"
};
String[] correctAnswer =
{
"28 - 15 = 13", "15 + 13 = 28", "28 - 13 = 15"
};
String[] answer1Bank =
{
"28 - 15 = 13", "28 - 13 = 15", "28 - 13 = 15"
};
String[] answer2Bank =
{
"15 + 13 = 28", "15 + 13 = 28", "28 - 15 = 13"
};
int currentQuestionCounter = -1;//Keeps up with what question is currently being asked
TextArea storyScreen = new TextArea();
TextField userInput = new TextField();
Button btnStart = new Button();
@Override
public void start(Stage primaryStage)
{
storyScreen.setWrapText(true);//Wrap the text in the TextArea
storyScreen.setEditable(false);//Don't allow useInput in the storyScreen
userInput.setOnAction(actonEvent ->//Retrieve user Input on Enter pressed
{
TextField tempUserInput = (TextField) actonEvent.getSource();//get a reference to the userInput TextField.
switch (tempUserInput.getText())//Switch on that input
{
case "1":
if (answer1Bank[currentQuestionCounter].equals(correctAnswer[currentQuestionCounter]))
{
storyScreen.appendText("\n\nYou got this right!");
btnStart.setDisable(false);
}
else
{
storyScreen.appendText("\n\nYou got this wrong!");
}
break;
case "2":
if (answer2Bank[currentQuestionCounter].equals(correctAnswer[currentQuestionCounter]))
{
storyScreen.appendText("\n\nYou got this right!");
btnStart.setDisable(false);
}
else
{
storyScreen.appendText("\n\nYou got this wrong!");
}
break;
default:
storyScreen.appendText("\n\nYou have to enter a 1 or 2!");
userInput.setText("");
}
});
btnStart.setText("Start");
btnStart.setOnAction(actionEvent ->
{
btnStart.setText("Next");
btnStart.setDisable(true);
userInput.requestFocus();//Move the cursor to the userInput TextField
getCurrentQuestionSetup(++currentQuestionCounter);
});
VBox root = new VBox();
root.getChildren().addAll(storyScreen, userInput, btnStart);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
void getCurrentQuestionSetup(int currentQuestion)
{
if (currentQuestion < questionBank.length)
{
userInput.setText("");//reset the TextField
storyScreen.clear();//reset the TextArea
//Add new Question and answer to Textarea
storyScreen.setText(questionBank[currentQuestion]);
storyScreen.appendText("\n\n1: " + answer1Bank[currentQuestion]);
storyScreen.appendText("\n2: " + answer2Bank[currentQuestion]);
}
else
{
storyScreen.appendText("\n\nYou have completed this story!");
userInput.setText("");
btnStart.setText("Start");
btnStart.setDisable(false);
currentQuestionCounter = -1;
}
}
}
<强>更新强>
要使上述代码在更改C列的值(即类型编号)时自动运行,则应在Sheet1下添加以下代码:
Sub AutoDropdown()
Dim PersonSource As Range
Dim PersonSourceTotal As Range
Dim PersonCell As Range
Dim i As Long
Dim lastRow As Long
Dim SelectionArray(1 To 4) As String
Set PersonSourceTotal = Sheets("Sheet1").Range("D2:D200")
With PersonSourceTotal.Offset(0, -2)
lastRow = .Cells(.Rows.Count, PersonSourceTotal.Columns.Count).End(xlUp).Row
End With
Set PersonSource = Sheets("Sheet1").Range("D2:D" & lastRow)
arr1 = Array("Item1", "Item2", "Item3", "Item4") 'Define your selections items
arr2 = Array("Item5", "Item6", "Item7", "Item8", "Item9")
arr3 = Array("ItemE", "ItemF", "ItemG", "ItemH")
arr4 = Array("ItemA", "ItemB", "ItemC", "ItemD")
SelectionArray(1) = Join(arr1, "--") 'join the selections into another array
SelectionArray(2) = Join(arr2, "--")
SelectionArray(3) = Join(arr3, "--")
SelectionArray(4) = Join(arr4, "--")
AllSelections = Join(SelectionArray, ",") 'group all selections for data validation
On Error Resume Next
For Each PersonCell In PersonSource
VarName = PersonCell.Offset(0, -3)
ID = PersonCell.Offset(0, -2)
If VarName <> "" And ID <> "" Then
Select Case PersonCell.Offset(i, -1).Value
Case "Type1"
With PersonCell.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=AllSelections
End With
PersonCell.Value = SelectionArray(1)
Case "Type2"
With PersonCell.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=AllSelections
End With
PersonCell.Value = SelectionArray(2)
Case "Type3"
With PersonCell.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=AllSelections
End With
PersonCell.Value = SelectionArray(3)
Case "Type4"
With PersonCell.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=AllSelections
End With
PersonCell.Value = SelectionArray(4)
Case Else
MsgBox "No Type was entered on Column C"
End Select
Else
MsgBox "Remember to add VarName and ID"
End If
Next PersonCell
End Sub