创建一个行数组VBA

时间:2018-08-14 17:56:59

标签: arrays excel vba excel-vba

VBA的新功能。我正在尝试创建一个行数组。

基本上,我有一个完整的工作表,并且希望采用第8列中所有以某个值(“ MA”)开头的行。

我最终想要操纵该数组(好像是一个范围),并将其粘贴到工作表中的其他位置。有人可以帮忙吗?到目前为止,这是我的代码:

Dim top0M As Variant
ReDim top0M(1 To 1) As Variant

For i = 4 To Rows.Count
    If Cells(i, 8).Value Like "MA*" Then
        top0M(UBound(top0M)) = Rows(i)
        ReDim Preserve top0M(1 To UBound(top0M) + 1) As Variant
    End If
Next i

此代码可以运行,但是我不确定如何调试它以了解我是否具有正确的行。我可以像粘贴行一样粘贴这些行吗?

2 个答案:

答案 0 :(得分:2)

这将设置范围并将整体加载到数组中,然后使用所需的行加载另一个数组:

With ActiveSheet 'This should be changed to the name of the worksheet: Worksheets("MySheet")
    Dim rng As Range
    Set rng = .Range(.Cells(4, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, .Cells(4, .Columns.Count).End(xlToLeft).Column))


    Dim tot As Variant
    tot = rng.Value

    Dim top0M As Variant
    ReDim top0M(1 To Application.CountIf(.Range("H:H"), "MA*"), 1 To UBound(tot, 2)) As Variant
    Dim k As Long
    k = 1
    Dim i As Long
    For i = LBound(tot, 1) To UBound(tot, 1)
        If tot(i, 8) Like "MA*" Then
            Dim j As Long
            For j = LBound(tot, 2) To UBound(tot, 2)
                top0M(k, j) = tot(i, j)
            Next j
            k = k + 1
        End If
    Next i
End With

'to print to a sheet just assign the values:

Worksheets("sheet1").Range("A1").Resize(UBound(top0M, 1), UBound(top0M, 2)).Value = top0M

答案 1 :(得分:0)

尝试此代码

#include <iostream>
#include <vector>

using namespace std;

class observer
{
    public:
        observer() = default;
        ~observer() = default;

        virtual void notify() = 0;
};

class subject
{
    vector <observer *> vec;

    public:
        subject() = default;
        ~subject() = default;

        void _register(observer *obj)
        {
            vec.push_back(obj);
        }

        void unregister(observer *obj)
        {
            int i;
            for(i = 0; i < vec.size(); i++)
            {
                if(vec[i] == obj)
                {
                    cout << "found elem. unregistering" << endl;
                    vec.erase(vec.begin() + i);
                    break;
                }
            }

            if(i == vec.size())
            {
                cout << "elem not found to unregister" << endl;
            }
        }

        void notify()
        {
            vector <observer *>::iterator it = vec.begin();
            while(it != vec.end())
            {
                (*it)->notify();
                it ++;
            }
        }
};

class obsone : public observer
{
    void notify()
    {
        cout << "in obsone notify" << endl;
    }
};

class obstwo : public observer
{
    void notify()
    {
        cout << "in obstwo notify" << endl;
    }
};

int main()
{
    subject sub;

    obsone *one = new obsone();
    obstwo *two = new obstwo();

    sub._register(one);   
    sub._register(two);
    sub.notify();

    sub.unregister(one);
    sub.notify();

    //delete two;
    //sub.notify();

    return 0;
}