Guest User

vba-psa

a guest
Feb 12th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. Many excel models use VBA code which loops and copies and pastes values down a spreadsheet (PSA/CEACs etc).
  2.  
  3. The standard/rubbish approach is something like this:
  4.  
  5. Do Until i > numpatients
  6.  
  7. Sheets("Sheet1").Select
  8. Range("B2:H2").Select
  9. Selection.Copy
  10. Range("B3").Select
  11. Selection.Offset(i - 1, 0).Select
  12. Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
  13.  
  14. i = i + 1
  15.  
  16. Loop
  17.  
  18. However this way is very slow. It's taught in the Briggs book which is probably why some of us do it.
  19.  
  20. You can speed it up significantly by using this
  21.  
  22. Do Until i > numpatients
  23.  
  24. Sheet1.Range("B3:H3").Offset(i - 1, 0).Value = Sheet1.Range("B2:H2").Value
  25.  
  26. i = i + 1
  27.  
  28. Loop
  29.  
  30. In a test simulation using a multivariate normal on 10000 patients, it used to take 34 seconds but now takes 4. A significant speed-up. It's also cleaner.
  31.  
  32. Note: This copies values only (not formulae).
Add Comment
Please, Sign In to add comment