PDA

View Full Version : VBA Coding needs help



fabianj
03-28-2016, 03:29 AM
I have written a program but there is a problem in that for which i need some immediate help and advise possible.

I have pasted the code below:

Problem: column E2 should be the product of D2-C2 but though it looks to be a simplest program the referencing thing not happening for successive row range.
Please help.

Option Explicit


Sub main()
Dim val As Double

Dim Firstrow As Integer
Dim Lastrow As Integer
Dim totscore1 As Integer
Dim i As Integer, j As Integer

Sheets("sheet1").Select
Range("D2").Select
Firstrow = ActiveCell.row
Selection.End(xlDown).Select
Lastrow = ActiveCell.row
totscore1 = Lastrow - Firstrow

Range("E2").Select
For i = 0 To totscore1
val = Cells(2, 3).Value - Cells(2, 4).Value
ActiveCell.Value = val
ActiveCell.Offset(1, 0).Select
Next i
End Sub




I get the same value -75 printed throughout.




Regards
Fabian J
[/B][/FONT]

Bob Phillips
03-28-2016, 02:39 PM
Maybe?


Sub main()
Dim val As Double

Dim Firstrow As Integer
Dim Lastrow As Integer
Dim totscore1 As Integer
Dim i As Integer, j As Integer

Sheets("sheet1").Select
Range("D2").Select
Firstrow = ActiveCell.Row
Selection.End(xlDown).Select
Lastrow = ActiveCell.Row
totscore1 = Lastrow - Firstrow

With Range("E2").Select

For i = 0 To totscore1

.Offset(i, 0).Value = Cells(2 + i, "C").Value - Cells(2 + i, "D").Value
Next i
End With
End Sub

fabianj
03-28-2016, 09:51 PM
Almost right, with few minor errors, which I rectified. Thanks a lot "Grand Master"... you gave me a start.