Home > Back-end >  assign a result in a variable that i want to use later in the code vba
assign a result in a variable that i want to use later in the code vba

Time:10-20

I did this code and i don't know how could i store the result (Sheets(x 1).Cells(i, 5)), which is a column of numbers, in a variable that i want to use later in another function.

Maybe i should do a function and then add a sub to store the function.

 Sub sumV40()
 Dim i As Integer
 Dim x

 For x = 1 To Sheets.Count
    For i = 1 To 10
        Sheets(x   1).Cells(i, 5) = Sheets(1).Cells(i, 1)   Sheets(x   1).Cells(i, 1)
    Next i 
 Next x
 end sub()

CodePudding user response:

So all you need to do is Dim a new Integer variable and put it on the left of an assignment operator (in this case for VBA it is the = sign). Then all you need to do is surround your entire algebraic calculation in parenthesis (although that is not necessary in this case). It looks like you're actively putting the calculated value in another cell so I broke that into a second step.

Example:

 Dim i As Integer
 Dim x
 Dim total As Integer

 For x = 1 To Sheets.Count
    For i = 1 To 10
      total = (Sheets(1).Cells(i, 1)   Sheets(x   1).Cells(i, 1))
      Sheets(x   1).Cells(i, 5) = total
    Next i 
 Next x
  • Related