Hello Everyone,
I have a query in VBA multiple if conditions.
if you are writing vba code for sum and if any of the column have 0 then it will throw an error.
So, What i am tring to do is writing a vba code where any of the columns have "0" value then it should give "0" in the sum column. and if one column have value and another column have "0" then it should give the output as value in the sum column. and if both columns have value then it should give a sum in sum column.
below is the code it tried. but not sucessful
Sub Count_sum()
Dim x As Integer
Dim header1 As Long
Dim header2 As Long
header1 = Sheets("sheet2").range("K" & Rows.Count).End(xlUp).Row
header2 = Sheets("sheet2").range("L" & Rows.Count).End(xlUp).Row
Lastrow2 = Sheets("sheet2").range("J" & Rows.Count).End(xlUp).Row
With ThisWorkbook.Sheets("Sheet2")
For x = 2 To Lastrow2
If header1 = 0 And header2 = 0 Then
.range.Cells(x, "M").Value = 0
ElseIf header1 >= 1 And header2 = 0 Then
.range.Cells(x, "M").Value = .range.Cells(x, "K").Value
ElseIf header1 = 0 And header <= 1 Then
.range.Cells(x, "M").Value = .range.Cells(x, "L").Value
ElseIf header1 >= 1 And header2 >= 1 Then
.range.Cells(x, "M").Value = .range.Cells(x, "K").Value + .range.Cells(x, "L").Value
End If
Next
End With
End Sub
and same is needed to do division also
I have a query in VBA multiple if conditions.
if you are writing vba code for sum and if any of the column have 0 then it will throw an error.
So, What i am tring to do is writing a vba code where any of the columns have "0" value then it should give "0" in the sum column. and if one column have value and another column have "0" then it should give the output as value in the sum column. and if both columns have value then it should give a sum in sum column.
below is the code it tried. but not sucessful
Sub Count_sum()
Dim x As Integer
Dim header1 As Long
Dim header2 As Long
header1 = Sheets("sheet2").range("K" & Rows.Count).End(xlUp).Row
header2 = Sheets("sheet2").range("L" & Rows.Count).End(xlUp).Row
Lastrow2 = Sheets("sheet2").range("J" & Rows.Count).End(xlUp).Row
With ThisWorkbook.Sheets("Sheet2")
For x = 2 To Lastrow2
If header1 = 0 And header2 = 0 Then
.range.Cells(x, "M").Value = 0
ElseIf header1 >= 1 And header2 = 0 Then
.range.Cells(x, "M").Value = .range.Cells(x, "K").Value
ElseIf header1 = 0 And header <= 1 Then
.range.Cells(x, "M").Value = .range.Cells(x, "L").Value
ElseIf header1 >= 1 And header2 >= 1 Then
.range.Cells(x, "M").Value = .range.Cells(x, "K").Value + .range.Cells(x, "L").Value
End If
Next
End With
End Sub
and same is needed to do division also