无法退出循环中的下一个循环直到循环

时间:2017-06-25 04:13:51

标签: vba excel-vba excel

无法退出for..next并使用do...loop until运行。此代码不会停止。我不知道代码或我的excel宏会发生什么。

   Do
    For z = 1 To 7
        z = z
        i = i + 1
    Next z
  Loop Until i = 2

1 个答案:

答案 0 :(得分:0)

如果要运行循环两次,请将代码更改为

RowItem::RowItem() : m_pParentItem( nullptr )
{
}

RowItem::~RowItem()
{
    qDeleteAll(m_pChildItems );
}

void RowItem::setParent(RowItem *p_pParent)
{
    m_pParentItem = p_pParent;
}

void RowItem::appendChild(RowItem *p_pChild)
{
    m_pChildItems.append(p_pChild);
    p_pChild->setParent( this );
}

void RowItem::insertChild(int p_nRow, RowItem *p_pChild)
{
    m_pChildItems.insert( p_nRow, p_pChild );
    p_pChild->setParent( this );
}

bool RowItem::removeChild(int p_nRow )
{
    if ( m_pChildItems.size() <= p_nRow ) {
        return false;
    }

    RowItem * pChild = m_pChildItems.at( p_nRow );
    delete pChild;
    pChild = nullptr;

    m_pChildItems.removeAt( p_nRow );

    return true;
}

bool RowItem::replaceChild(RowItem * p_pOldChild, RowItem * p_pNewChild )
{
    if ( ( p_pOldChild == nullptr ) || ( p_pNewChild == nullptr ) ) {
        return false;
    }

    int nIdx = m_pChildItems.indexOf( p_pOldChild );
    if ( nIdx == -1 ) {
        return false;
    }

    m_pChildItems.replace( nIdx, p_pNewChild );
    p_pNewChild->setParent( this );

    return true;
}

RowItem *RowItem::child(int p_nRow) const
{
    if ( ( p_nRow >= m_pChildItems.size() ) || ( p_nRow < 0 ) )
    {
        QLOG_ERROR() << "Requested child item not existent. m_pChildItems.size() = " << m_pChildItems.size() << "; requested item:" << p_nRow << Q_FUNC_INFO;
        return nullptr;
    }
    return m_pChildItems.at(p_nRow);
}

int RowItem::rowCount() const
{
    return m_pChildItems.count();
}

int RowItem::columnCount() const
{
    return m_grColList.count();
}

int RowItem::column(const QVariant &p_grData, int /*p_nRole*/) const
{
    for( int i = 0; i < m_grColList.size(); ++i ) {
        const QMap< quint8, QVariant > & grRoleMap = m_grColList.at( i );
        if( grRoleMap.values().contains( p_grData ) == true ) {
            return i;
        }
    }

    return -1;
}

QVariant RowItem::data(const int &p_nColumn, int p_nRole) const
{
    if ( ( p_nColumn < 0 ) || ( p_nRole < 0 ) ) {
        QLOG_ERROR() << "Requested invalid column or role data:" << p_nColumn << p_nRole << Q_FUNC_INFO;
        return QVariant();
    }

    if ( m_grColList.size() <= p_nColumn ) {
        QLOG_ERROR() << "Requested column does not exists. Column size is " << m_grColList.size() << "; requested was col" << p_nColumn << Q_FUNC_INFO;
        return QVariant();
    }

    const QMap< quint8, QVariant > & grRoleMap = m_grColList.at( p_nColumn );
    QVariant grValue = grRoleMap.value( p_nRole, QVariant() );

    if ( ( p_nRole == Qt::EditRole ) && ( ! grValue.isValid() ) ) {
        grValue = grRoleMap.value( Qt::DisplayRole, QVariant() );
    }

    return grValue;
}

int RowItem::row() const
{
    int nRow = 0;
    if ( m_pParentItem != nullptr ) {
        nRow = m_pParentItem->m_pChildItems.indexOf( const_cast< RowItem* >( this ) );
    }

    return nRow;
}

RowItem *RowItem::parentItem()
{
    return m_pParentItem;
}

bool RowItem::setData( const int & p_nColumn, const QVariant &p_grData , int p_nRole)
{
    QMap< quint8 , QVariant > grRoleMap;
    int nCol = p_nColumn;

    if ( p_nColumn == -1 ) {
        m_grColList.append( QMap< quint8 , QVariant >() );
        nCol = m_grColList.size() - 1;
    }
    else {
        // expand coloumns if required
        while( m_grColList.size() <= p_nColumn ) {
            m_grColList.append( QMap< quint8 , QVariant >() );
        }

        grRoleMap = m_grColList.at( p_nColumn );
    }

    grRoleMap.insert( p_nRole, p_grData );

    m_grColList.replace( nCol, grRoleMap );

    return true;
}

RowItem &RowItem::operator=(RowItem &p_grSrc)
{
    m_grColList = p_grSrc.m_grColList;
    return * this;
}

i = 0
Do
    '...
    i = i + 1
Loop Until i = 2