0

我正在编写一个代码,该代码应该从与我创建的表的最后三行相对应的列中获取值。它需要在这种形式下,因为数字是随机的。

我在第一个 Excel 表中有一个不同值的表。我计算有多少行和列。

然后我从第二张表中获取最后三个值,属于索引列。我将使用这些索引来构建一个代码,该代码将获取这些索引并在第一个 Excel 表中识别它们对应的列。然后,我希望它为我提取这些值。

问题是它得到的三个 FIRST 值不是最后的

我怎样才能解决这个问题?

Option Explicit
Option Base 1

Sub ThreeBest()
Dim i As Integer, j As Integer, N As Integer
Dim Valeurs As Integer

Dim nb_Cells As Integer
Dim nb_Actions As Integer


nb_Cells = Worksheets("Actions").Cells(Rows.Count, 2).End(xlUp).Row - 1
nb_Actions = Worksheets("Actions").Cells(1, Columns.Count).End(xlToLeft).Column - 1
N = 3 'We want to choose the three last ones

ReDim ValeursAction(nb_Cells) As Variant

For i = 1 To N
        Valeurs = Worksheets("Performance").Cells(nb_Actions + 7 - (i - 1), 9).Value
        'I place the value from the column corresponding to Valeurs in Performance
        For j = 1 To nb_Cells
            ValeursAction(j) = Worksheets("Actions").Cells(j + 1, Valeurs + 1)

        With Sheets("Performance")
                       .Cells(5 + j, 5 - i) = ValeursAction(j)
        End With
Next j
Next i

End Sub
4

1 回答 1

0

除非我完全误解了您的要求,否则我认为您需要使用 xlToRight 和 xlDown 分别查找最后的列和行。
尝试这个:

Option Explicit
Option Base 1

Sub ThreeBest()
Dim i As Integer, j As Integer, N As Integer
Dim Valeurs As Integer

Dim nb_Cells As Integer
Dim nb_Actions As Integer


nb_Cells = Worksheets("Actions").Cells(Rows.Count, 2).End(xlDown).Row - 1
nb_Actions = Worksheets("Actions").Cells(1, Columns.Count).End(xlToRight).Column - 1
N = 3 'We want to choose the three last ones

ReDim ValeursAction(nb_Cells) As Variant

For i = 1 To N
        Valeurs = Worksheets("Performance").Cells(nb_Actions + 7 - (i - 1), 9).Value
        'I place the value from the column corresponding to Valeurs in Performance
        For j = 1 To nb_Cells
            ValeursAction(j) = Worksheets("Actions").Cells(j + 1, Valeurs + 1)

        With Sheets("Performance")
                       .Cells(5 + j, 5 - i) = ValeursAction(j)
        End With
Next j
Next i

End Sub
于 2013-05-06T17:13:26.570 回答