传递到工作表的1D数组偏移1

时间:2014-04-04 13:38:44

标签: excel vba

道歉,因为这可能是非常简单的事情,但我无法解决这个问题。我正在创建一个1d数组(1行,4列)然后传递给工作表。但是输出总是向右偏移一个位置。我得到的是:

 Physics Lecturer   Pimp Status Number of Underlings    Total Backhands Delivered   
                    Robbo       Very High               15                        677

我想要的是:

   Physics Lecturer Pimp Status Number of Underlings    Total Backhands Delivered   
   Robbo            Very High   15                      677

我的代码是:

    Sub Database_Appender()
    'Written by JDog's Underling on 01/04/2014.

        'Defining the sheet being work on and its number (in the order of sheets from first to last).
        Dim sht As Worksheet, sheet_number As Long

        'An array to contain all returned values (manufacturer, price, class, quotation date and region).

        no_of_entries = 4

        Dim Data_Array(4) As Variant

        sheet_number = 1
        Set sht = ThisWorkbook.Worksheets(sheet_number)


        Data_Array(1) = sht.Cells(6, 2) 'Class
        Data_Array(2) = sht.Cells(6, 3) 'Region
        Data_Array(3) = sht.Cells(6, 4) 'Quotation Date
        Data_Array(4) = sht.Cells(6, 5) 'Comparable Price


        Sheets("Database").Range("A2").Resize(1, no_of_entries + 1) = Data_Array
    End Sub

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

数组是从零开始的,因此请将数组声明更改为:

Dim Data_Array(3) As Variant

然后更改您的分配:

Data_Array(0) = sht.Cells(6, 2) 'Class
Data_Array(1) = sht.Cells(6, 3) 'Region
Data_Array(2) = sht.Cells(6, 4) 'Quotation Date
Data_Array(3) = sht.Cells(6, 5) 'Comparable Price