简体   繁体   中英

Create new range from 3 other column ranges in VBA Excel

I have 3 ranges in VBA:

range1 = Range("A:A")
range2 = Range("B:B")
range3 = Range("C:C")

i want to return a new range where each row in the three ranges are added together.

So if I have the data

A, B, C
=========
1, 2, 3
2, 4, 5
1, 1, 2

where each of the 3 ranges only consist of the values (the column names are just to explain). So the first range has values 1, 2, 1, the second range has values 2, 4, 1, and the third range has values 3, 5, 2.

I want to output the range consisting of

6
11
4

I guess it is something like

Dim newRange As Range
Dim RowNo    As Long

// make newRange as long as one of the other ranges

For Each RowNo in LBound(newRange)
    newRange(RowNo).Value = range1(RowNo).Value + range2(RowNo).Value + range3(RowNo).Value
Next RowNo

// return newRange

is this correct?

Try like this:

 Option Explicit

Public Sub MakeRanges()

    Dim lngCounterLeft  As Long
    Dim lngCounterDown  As Long
    Dim lngCounter      As Long
    Dim rngCell         As Range
    Dim varResult       As Variant


    lngCounterDown = last_row(ActiveSheet.Name)
    lngCounterLeft = last_col(ActiveSheet.Name)
    ReDim varResult(0)

    For lngCounter = 1 To lngCounterDown
        Set rngCell = ActiveSheet.Cells(lngCounter, lngCounterLeft + 1)
        rngCell.FormulaR1C1 = "=SUM(RC[-" & lngCounterLeft & "]:RC[-1])"
        'rngCell.Interior.Color = vbRed

        If lngCounter > 1 Then
            ReDim Preserve varResult(UBound(varResult) + 1)
        End If


        varResult(UBound(varResult)) = rngCell.Value
        rngCell.Clear

    Next lngCounter

    Stop

End Sub

Public Function last_row(Optional str_sheet As String, Optional column_to_check As Long = 1) As Long

    Dim shSheet  As Worksheet

    If str_sheet = vbNullString Then
        Set shSheet = ThisWorkbook.ActiveSheet
    Else
        Set shSheet = ThisWorkbook.Worksheets(str_sheet)
    End If

    last_row = shSheet.Cells(shSheet.Rows.Count, column_to_check).End(xlUp).Row

End Function

Public Function last_col(Optional str_sheet As String, Optional row_to_check As Long = 1) As Long

    Dim shSheet  As Worksheet

        If str_sheet = vbNullString Then
            Set shSheet = ActiveSheet
        Else
            Set shSheet = Worksheets(str_sheet)
        End If

    last_col = shSheet.Cells(row_to_check, shSheet.Columns.Count).End(xlToLeft).Column

End Function

Run MakeRanges . Pretty much, you receive a new column with the sum of the other columns. It is a good idea to change the ActiveSheet into something meaningful. If you don't like the formula, you may uncomment the rngCell.Value in my code. The array you need is varResult . Remove the stop and find a way to return it in a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM