我想通过输入框从用户那里获得超过1个输入并过滤表格。只过滤了一列。然后将整个行数据复制到另一个工作表。我使用下面的代码。问题是它可以用来过滤1个国家。
我在列F中有很多国家/地区。我需要在输入框中输入2个或更多国家/地区。然后复制并粘贴。我想添加Loop。但我不知道如何。帮帮我
Private Sub CommandButton1_Click()
Dim str1 As Variant
Dim Tbl As ListObject
Dim FiltRng As Range
Dim RngArea As Range
Set Tbl = Sheet1.ListObjects("DataTable")
str1 = Application.InputBox("Select the Country Code")
If str1 = False Then
MsgBox "Please select one Country", , "Input"
Exit Sub
Else
Tbl.Range.AutoFilter Field:=6, Criteria1:=str1
For Each RngArea In Tbl.Range.SpecialCells(xlCellTypeVisible).Rows
If RngArea.Row > 1 Then
If Not FiltRng Is Nothing Then
Set FiltRng = Application.Union(FiltRng, RngArea)
Else
Set FiltRng = RngArea
End If
End If
Next RngArea
If Not FiltRng Is Nothing Then
FiltRng.Copy Sheets("Sheet2").Range("A2")
End If
End If
Sheet1.ListObjects("DataTable").Range.AutoFilter Field:=6
End Sub
答案 0 :(得分:4)
您可以在循环中读取InputBox。请尝试以下代码。
public class TMPAdapter extends RecyclerView.Adapter<TMPAdapter.ViewHolder> {
private static final String TAG = LogUtils.makeLogTag(TMPAdapter.class);
private List<String> mListValue;
private Context mContext;
public TMPAdapter(Context context, List<String> listValue) {
this.mContext = context;
this.mListValue = listValue;
}
@Override
public void onViewAttachedToWindow(ViewHolder holder) {
super.onViewAttachedToWindow(holder);
}
@Override
public void onViewDetachedFromWindow(ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_like_list, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTxtUserName.setText(mListValue.get(position));
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public int getItemCount() {
return mListValue.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private VeniTextView mTxtUserName;
ViewHolder(View view) {
super(view);
mTxtUserName = view.findViewById(R.id.txt_user_name);
}
}
答案 1 :(得分:1)
使用以下子句,它采用两个条件来过滤表格并将过滤后的数据复制到sheet2
。您可以根据需要添加更多条件。
Sub Filter2Criteria()
Dim str1, str2 As Variant
Dim Tbl As ListObject
Dim FiltRng As Range
Dim RngArea As Range
Set Tbl = Sheet1.ListObjects("DataTable")
str1 = Application.InputBox("Select the Country Code")
str2 = Application.InputBox("Select the Country Code")
If str1 = False Then
MsgBox "Please select first Country", , "Input"
Exit Sub
ElseIf str2 = False Then
MsgBox "Please select second Country", , "Input"
Exit Sub
End If
Tbl.Range.AutoFilter Field:=6, Criteria1:=str1, Operator:=xlOr, Criteria2:=str2
Set FiltRng = Tbl.Range.SpecialCells(xlCellTypeVisible)
FiltRng.Copy Sheets("Sheet2").Range("A2")
End Sub