mercredi 9 août 2017

How to place IF statement in next available column in Excel VBA?

I am trying to place an IF statement in the next available column(dynamic) in row 2. Once the IF statement is in that cell, I want to add in the value from the last used row(dynamic) from that same column. I have been having trouble coding the IF statement into VBA. I also need help with coding it so it adds in the last row from the column into the cell with the IF statement. Here is my code so far:

Sub vlookupF5()
   Dim SourceLastRow As Long
        Dim OutputLastRow As Long
        Dim sourceSheet As Worksheet
        Dim outputSheet As Worksheet

        Dim NextColumn As Long
        Dim ColumnStr As String
         Dim ColumnStr2 As String
          Dim ColumnStr3 As String
        Set sourceSheet = Worksheets("Actuals")
        Set outputSheet = Worksheets("Pivot")
        With sourceSheet
             SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With

        With outputSheet
            NextColumn = .Cells(2, Columns.Count).End(xlToLeft).Column + 1
            If NextColumn + 1 < 27 Then
               ColumnStr = Chr(64 + NextColumn + 1)
            Else
                ColumnStr = Chr(64 + Int((NextColumn + 1) / 26)) & Chr(64 + ((NextColumn + 1) Mod 26))
            End If
            If NextColumn - 1 < 27 Then
               ColumnStr3 = Chr(64 + NextColumn - 1)
            Else
                ColumnStr3 = Chr(64 + Int((NextColumn - 1) / 26)) & Chr(64 + ((NextColumn - 1) Mod 26))
            End If
            If NextColumn < 27 Then
               ColumnStr2 = Chr(64 + NextColumn)
            Else
                ColumnStr2 = Chr(64 + Int((NextColumn) / 26)) & Chr(64 + ((NextColumn) Mod 26))
            End If
            OutputLastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
Worksheets("Pivot").Activate
'this is the part I need help with:

                .Range(Cells(2, NextColumn), .Cells(2, NextColumn)).Formula = _
                    "=IF($E$3="Feb",Actuals!$V$133,IF($E$3="Mar",SUM(Actuals!$V$133:$WB$133),IF($E$3="Apr", _
                     SUM(Actuals!$V$133:$X$133),IF($E$3="May",SUM(Actuals!$V$133:$Y$133),IF($E$3="Jun",SUM(Actuals!$V$133:$Z$133),IF($E$3="Jul",SUM(Actuals!$V$133:$AA$133),IF($E$3="Aug",SUM(Actuals!$V$133:$AB$133),IF($E$3="sep",SUM(Actuals!$V$133:$AC$133),IF($E$3="Oct",SUM(Actuals!$V$133:$AD$133),IF($E$3="Nov",SUM(Actuals!$V$133:$AE$133),IF($E$3="Dec",SUM(Actuals!$V$133:$AF$133),"")))))))))))*1000"

            End With
            End Sub

Can anyone help??

Aucun commentaire:

Enregistrer un commentaire