Results 1 to 18 of 18

A VBA code doesn't select the right columns in a sheet

Threaded View

  1. #1
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    A VBA code doesn't select the right columns in a sheet

    Hi,

    I am working on a code that is supposed to get the values from columns that correspond to the last three rows of a table I created. It needs to be under this form because the numbers will be random.

    I have a table in the first excel sheet with different values. I calculate how many rows and columns there are.

    Then I get the last three values from the second sheet, belonging to the column Index. I will use these indexes in order to construct a code that will obtain these indexes and indentify the columns they correspond to in the first excel sheet. THen, I want it to extract these values for me.

    The problem is it gets the three FIRST values not last

    How can I fix this?

    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 'The source of my problem is in the following lines
                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
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1