a way to make this more compact ??

  • Thread starter Thread starter pls123
  • Start date Start date
P

pls123

aWS.Range("B2").Value = (aWS.Range("H7") - aWS.Range("A2").Value) * 100
aWS.Range("B4").Value = (aWS.Range("H7") - aWS.Range("A4").Value) * 100
aWS.Range("B6").Value = (aWS.Range("H7") - aWS.Range("A6").Value) * 100
aWS.Range("B8").Value = (aWS.Range("H7") - aWS.Range("A8").Value) * 100
aWS.Range("B10").Value = (aWS.Range("H7") - aWS.Range("A10").Value) * 100
aWS.Range("B12").Value = (aWS.Range("H7") - aWS.Range("A12").Value) * 100
aWS.Range("B14").Value = (aWS.Range("H7") - aWS.Range("A14").Value) * 100
etc etc....
 
something like this should work. just change the 20 to your last row or use a
lastrow variable if it's all the way down the sheet

Sub test()
Dim ws As Worksheet
Dim i As Long
Set ws = Worksheets("aWS")
For i = 2 To 20 Step 2
With ws
.Range("b" & i).Value = .Range("H7").Value - .Range("A" & i).Value * 100
End With
Next
End Sub

or using the lastrow variable

Sub test2()
Dim ws As Worksheet
Dim i As Long
Dim lastrow As Long
Set ws = Worksheets("aWS")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastrow Step 2
With ws
.Range("b" & i).Value = .Range("H7").Value - .Range("A" & i).Value * 100
End With
Next

End Sub
 
hi gary ty it seems to work well except for a parenthesis that i added and
works well..
i forgot to tell you how was the dim and set but now i fixed all ty !!
 
hi gary it works perfectly and it is easyest to reduce if u don't need 800...
but at equivalent parameters it uses 5.5 instead 4.5 cpu usage !!! ;)
i will keep it anyway and probably use it, byyy and ty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top