将2D数组写入一系列单元格

时间:2017-11-06 18:46:35

标签: vba

我有一系列细胞阵列,我需要把它写回特定范围的细胞。在写回新的单元格范围时,我的前两列正在按预期工作,但接下来的两列正在为第3列和第4列镜像第2列。

数组范围:

public class Szal {
    static ArrayList<MyThread> myThread;
    static String[] names;

    public Szal() {
        myThread = new ArrayList<MyThread>();
        names = new String[]{"EZ", "AZ"};

        for (int i = 0; i < names.length; i++) {
            MyThread t = new MyThread(names[i]);
            myThread.add(t);
            t.start();
        }
    }

    public static void main(String[] args) {
        new Szal();

        Thread[] thread = new Thread [Thread.activeCount ()];
        int m = Thread.enumerate (thread);
        for (int i = 0; i < m; i++) {
            System.out.println (thread[i].getName());
        }

        // Why is this not working?
        for (Thread t : myThread) {
            if (t.getName().equalsIgnoreCase("EZ")) {
                t = Thread.currentThread();
                t.interrupt();
                myThread.remove(t);
            }
        }

        Thread[] threads = new Thread [Thread.activeCount ()];
        int c = Thread.enumerate (threads);
        for (int i = 0; i < c; i++) {
            System.out.println (threads[i].getName());
        }
    }

    class MyThread extends Thread {
        public MyThread(String name) {
            super(name);
        }

        public void run() {
            while (true) {

            }
        }
    }
}

将写为:

1,2,3,4

2,2,3,5

3,4,5,6

我想要的是:

1,2,2,2

2,2,2,2

3,4,4,4


1,2,3,4

2,2,3,5

3,4,5,6

我假设它与此部分有关,但我对维度数组不是很好。

Dim myRange As Range
Dim scriptDic As Variant
Dim arr As Variant
Dim i As Integer
Dim x As Integer

With ThisWorkbook.Sheets("AGGREGATE")

    Set myRange = .Range("H4:K19")

    Set scriptDic = CreateObject("Scripting.Dictionary")

    arr = myRange.Value

    For i = 1 To UBound(arr, 1)


        If arr(i, 1) <> "" Then

            scriptDic(arr(i, 1)) = scriptDic(arr(i, 1)) + arr(i, 2)

        End If

    Next

    Application.ScreenUpdating = False

    .Range("M4:P19").ClearContents

    myRange.Range("F1").Resize(scriptDic.Count, 1) = Application.WorksheetFunction.Transpose(scriptDic.keys)
    myRange.Range("G1").Resize(scriptDic.Count, 1) = Application.WorksheetFunction.Transpose(scriptDic.items)
    myRange.Range("H1").Resize(scriptDic.Count, 1) = Application.WorksheetFunction.Transpose(scriptDic.items)
    myRange.Range("I1").Resize(scriptDic.Count, 1) = Application.WorksheetFunction.Transpose(scriptDic.items)

    Application.ScreenUpdating = True

End With

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

为此,我将摆脱Dictionary,只需使用RemoveDuplicates来获取唯一键值。然后我会使用SUMIF来获得所需的答案:

Sub test()
    Dim numRows As Long

    Application.ScreenUpdating = False

    With ThisWorkbook.Sheets("AGGREGATE")

        'Clear existing contents of column M:P
        .Range("M4", .Cells(.Rows.Count, "M").End(xlUp).Offset(0, 3)).ClearContents

        'Copy keys to column M
        numRows = .Cells(.Rows.Count, "H").End(xlUp).Row - 3
        .Range("M4").Resize(numRows, 1).Value = .Range("H4").Resize(numRows, 1).Value
        'Generate unique list
        .Range("M4").Resize(numRows, 1).RemoveDuplicates Columns:=1, Header:=xlNo

        'Calculate answers in column N to P
        numRows = .Cells(.Rows.Count, "M").End(xlUp).Row - 3
        .Range("N4").Resize(numRows, 3).Formula = "=SUMIF($H:$H,$M4,I:I)"

        'Convert formulas to values
        .Range("N4").Resize(numRows, 3).Value = .Range("N4").Resize(numRows, 3).Value

    End With

    Application.ScreenUpdating = True
End Sub