使用值将Range更改为First和Last列

时间:2017-03-14 02:44:50

标签: excel vba excel-vba

对我工作档案的一点帮助表示赞赏..

以下是我的代码, 我想要的是将此公式设置为固定范围.. Sheets("Unire").Range("CB4:HJ4")) 我的意思是范围将从第4行开始(第4行:Col CB)到第4行的最后一列。

Sub Unire()
    Dim cell As Range
    Application.ScreenUpdating = False
    Sheets("Unire").Activate
    For Each cell In Intersect(ActiveSheet.UsedRange, Sheets("Unire").Range("CB4:HJ4"))
        cell.EntireColumn.Hidden = cell.value <> Sheets("Command").Range("B5") And Not IsEmpty(cell)
    Next cell
    Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:1)

这应该做:

Sub Unire()
    Dim cell As Range
    Application.ScreenUpdating = False
    With Sheets("Unire")
        For Each cell In .Range("CB4", .Range("XFD4").End(xlToLeft)) ' <-- from CB4 to last non-empty on row 4
            cell.EntireColumn.Hidden = cell.value <> Sheets("Command").Range("B5").value And Not IsEmpty(cell)
        Next cell
    End With
    Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

如果您的第4行不是空单元格中填充了“常量”(即不是从公式派生)值,那么您可以通过SpecialCells()范围的Sheets("Command").Range("B5").Value方法仅通过非空单元格迭代来提高代码速度对象

此外,您最好一次性将Option Explicit Sub Unire() Dim cell As Range Dim refVal As Variant refVal = Sheets("Command").Range("B5").Value '<--| store reference value for subsequent comparisons On Error GoTo ExitSub '<--| make sure to properly exit this sub should sunsequent statements (namely 'SpecialCells' one) raise any error Application.ScreenUpdating = False With Sheets("Unire") For Each cell In .Range("CB4", .cells(4, .columns.Count).End(xlToLeft)).SpecialCells(xlCellTypeConstants) ' <--| reference not empty cells from CB4 to last not-empty cell in row 4 cell.EntireColumn.Hidden = cell.Value <> refVal Next End With ExitSub: Application.ScreenUpdating = True End Sub 存储在辅助变量中,而不是在每次迭代时访问它

public class StationsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public List<StationsModel> stationsModels;
public Context context;
private LayoutInflater inflater;
private String selectedStation = "";

public class GenericViewHolder extends RecyclerView.ViewHolder {
    public TextView stationName;
    public View top, bottom;

    public GenericViewHolder(View view) {
        super(view);

        stationName = (TextView) view.findViewById(R.id.station_item_name);
        top = (View) view.findViewById(R.id.top_line);
        bottom = (View) view.findViewById(R.id.bottom_line);

    }
}

public StationsAdapter(Context context, List<StationsModel> stationsModels, String selectedStation) {
    this.context = context;
    this.stationsModels = stationsModels;
    this.selectedStation = selectedStation;
    inflater = LayoutInflater.from(context);


}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.station_adapter_item, parent, false);
    GenericViewHolder holder = new GenericViewHolder(view);
    return holder;


}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof GenericViewHolder) {
        GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
        if (selectedStation.equals(stationsModels.get(position).getLabel())) {
            genericViewHolder.stationName.setTextSize(40);
            genericViewHolder.top.setVisibility(View.VISIBLE);
            genericViewHolder.bottom.setVisibility(View.VISIBLE);

        } else {
            genericViewHolder.stationName.setTextSize(20);
            genericViewHolder.top.setVisibility(View.GONE);
            genericViewHolder.bottom.setVisibility(View.GONE);
        }
  genericViewHolder.stationName.setText(stationsModels.get(position).getLabel());

    }
}

@Override
public int getItemCount() {
    return stationsModels.size();
}

public StationsModel getItem(int position) {
    return stationsModels.get(position);
   }
}