PDA

View Full Version : VBA Row offset when dynamically changing cell value



mattadams84
12-05-2018, 02:13 AM
Hello

I have got a spreadsheet that with through various help from this forum is working perfectly. I am now in the process of 'improving' it. Here is how it works:

I have a 'data sheet' that is 294 columns wide, and contains lots of data for football matches. My spreadsheet currently goes through all of the data and when various criteria are met it adds the match to a different sheet. At the moment what happens is that each row on my selections sheet contains a match and the columns hold the data. This works perfectly.

So for example :

ROW 3, column A (home team), column B (away team), column C (some data), column D (some data) etc...

I want to change it so that instead of having the match on one row, id like to split it to two rows. The simple reason for this is so that i gain a bit of space and it becomes more readable.

So for example i want it to read :

ROW 3 (home team) and the columns hold all the data for the home team.
ROW 4 (away team) and the columns hold all the data for the away team.

Here is my original VBA code :


Sub LTATrades()

Application.ScreenUpdating = False
Dim LastRow As Long, fs As Worksheet, ds As Worksheet, x As Long
Set fs = Sheets("Filters")
Set ds = Sheets("Data")
LastRow = ds.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ClearSelections
SortData
DeleteCF
For x = 4 To LastRow
If ds.Cells(x, 1) = ds.Range("E1") And ds.Cells(x, 40) >= fs.Range("C2") And ds.Cells(x, 41) >= fs.Range("C2") Then
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "B").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 3)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "C").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 4)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "D").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 5)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "E").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 81)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "F").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 91)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "G").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 82)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "H").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 92)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "I").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 83)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "J").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 93)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "K").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 84)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "L").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 94)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "M").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 85)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "N").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 96)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "O").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 95)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "P").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 86)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "Q").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 88)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "R").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 98)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "S").End(xlUp).Offset(1, 0).Value = Round((ds.Cells(x, 57).Value / ds.Cells(x, 40).Value) * 100, 0) & "% (" & ds.Cells(x, 57).Value & "/" & ds.Cells(x, 40).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "T").End(xlUp).Offset(1, 0).Value = Round((ds.Cells(x, 71).Value / ds.Cells(x, 41).Value) * 100, 0) & "% (" & ds.Cells(x, 71).Value & "/" & ds.Cells(x, 41).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "U").End(xlUp).Offset(1, 0).Value = Round((ds.Cells(x, 58).Value / ds.Cells(x, 40).Value) * 100, 0) & "% (" & ds.Cells(x, 58).Value & "/" & ds.Cells(x, 40).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "V").End(xlUp).Offset(1, 0).Value = Round((ds.Cells(x, 72).Value / ds.Cells(x, 41).Value) * 100, 0) & "% (" & ds.Cells(x, 72).Value & "/" & ds.Cells(x, 41).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "W").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 229).Value + ds.Cells(x, 243).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 229).Value + ds.Cells(x, 243).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "X").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 257).Value + ds.Cells(x, 275).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 257).Value + ds.Cells(x, 275).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "Y").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 54).Value + ds.Cells(x, 68).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 54).Value + ds.Cells(x, 68).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "Z").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 55).Value + ds.Cells(x, 69).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 55).Value + ds.Cells(x, 69).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "AA").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 56).Value + ds.Cells(x, 70).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 56).Value + ds.Cells(x, 70).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "AB").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 59).Value + ds.Cells(x, 73).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 59).Value + ds.Cells(x, 73).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "AC").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 144).Value + ds.Cells(x, 159).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 144).Value + ds.Cells(x, 159).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "AD").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 147).Value + ds.Cells(x, 162).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 147).Value + ds.Cells(x, 162).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
End If
Next x
ResetCFLTA
Application.ScreenUpdating = True
End Sub

And now, here is what i have tried to make it work as i want...



Sub LTATradesTest()

Application.ScreenUpdating = False
Dim LastRow As Long, fs As Worksheet, ds As Worksheet, x As Long
Set fs = Sheets("Filters")
Set ds = Sheets("Data")
LastRow = ds.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ClearSelections
SortData


For x = 4 To LastRow
If ds.Cells(x, 1) = ds.Range("E1") And ds.Cells(x, 40) >= fs.Range("C2") And ds.Cells(x, 41) >= fs.Range("C2") Then
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "B").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 3)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "C").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 4)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "C").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 5)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "D").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 81)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "D").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 91)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "E").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 82)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "E").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 92)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "F").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 83)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "F").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 93)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "G").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 84)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "G").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 94)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "H").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 85)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "H").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 96)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "I").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 95)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "I").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 86)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "J").End(xlUp).Offset(1, 0).Value = ds.Cells(x, 88)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "J").End(xlUp).Offset(2, 0).Value = ds.Cells(x, 98)
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "K").End(xlUp).Offset(1, 0).Value = Round((ds.Cells(x, 57).Value / ds.Cells(x, 40).Value) * 100, 0) & "% (" & ds.Cells(x, 57).Value & "/" & ds.Cells(x, 40).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "K").End(xlUp).Offset(2, 0).Value = Round((ds.Cells(x, 71).Value / ds.Cells(x, 41).Value) * 100, 0) & "% (" & ds.Cells(x, 71).Value & "/" & ds.Cells(x, 41).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "L").End(xlUp).Offset(1, 0).Value = Round((ds.Cells(x, 58).Value / ds.Cells(x, 40).Value) * 100, 0) & "% (" & ds.Cells(x, 58).Value & "/" & ds.Cells(x, 40).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "L").End(xlUp).Offset(2, 0).Value = Round((ds.Cells(x, 72).Value / ds.Cells(x, 41).Value) * 100, 0) & "% (" & ds.Cells(x, 72).Value & "/" & ds.Cells(x, 41).Value & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "M").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 229).Value + ds.Cells(x, 243).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 229).Value + ds.Cells(x, 243).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "M").End(xlUp).Offset(2, 0).Value = Round(((ds.Cells(x, 257).Value + ds.Cells(x, 275).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 257).Value + ds.Cells(x, 275).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "N").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 54).Value + ds.Cells(x, 68).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 54).Value + ds.Cells(x, 68).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "N").End(xlUp).Offset(2, 0).Value = Round(((ds.Cells(x, 55).Value + ds.Cells(x, 69).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 55).Value + ds.Cells(x, 69).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "O").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 56).Value + ds.Cells(x, 70).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 56).Value + ds.Cells(x, 70).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "O").End(xlUp).Offset(2, 0).Value = Round(((ds.Cells(x, 59).Value + ds.Cells(x, 73).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 59).Value + ds.Cells(x, 73).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "P").End(xlUp).Offset(1, 0).Value = Round(((ds.Cells(x, 144).Value + ds.Cells(x, 159).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 144).Value + ds.Cells(x, 159).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
Sheets("LTA").Cells(Sheets("LTA").Rows.Count, "P").End(xlUp).Offset(2, 0).Value = Round(((ds.Cells(x, 147).Value + ds.Cells(x, 162).Value) / (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value)) * 100, 0) & "% (" & (ds.Cells(x, 147).Value + ds.Cells(x, 162).Value) & "/" & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
End If
Next x
Application.ScreenUpdating = True
End Sub


This does not work though. In fact it crashes excel. Can anyone help me achieve what i want?

There is a slight snag. As you can see the first peice of data is common to both teams (it is basically the name of the league) Is there anyway to tell excel to merge the two rows for column B?

Here is a link to my spreadsheet:

https://www.dropbox.com/sh/ddwr5tnchu5elnk/AABbEdY4j_Z316cjPJ9X_9zEa?dl=0

Regards

Fluff
12-05-2018, 05:46 AM
Cross posted https://www.mrexcel.com/forum/excel-questions/1079766-vba-row-offset-when-dynamically-changing-cell-value.html

Paul_Hossler
12-06-2018, 08:57 AM
@mattadams84 --

1. The Dropbox link only has a CSV file and Filters.xlam.

You should attach the sample workbook with macros to your post - note #2 in my signature


2. Please let us and any other forums know if you've posted the same question -- note #4 in my signature


3. This seems very similar to

http://www.vbaexpress.com/forum/showthread.php?64156-Ignoring-Error-VBA


except that the suggestions in that thread don't seem to be incorporated


4. As a suggestion to improve readability. This could be better, but I don't know what the data looks like to come up with better variable names




Option Explicit
Sub LTATradesTest()
Dim LastRow As Long, fs As Worksheet, ds As Worksheet, i As Long
Dim rLTA As Range, rDS As Range

Dim ds4041s As String
Dim ds4041 As Double, ds40 As Double, ds41 As Double

Set fs = Sheets("Filters")
Set ds = Sheets("Data")
LastRow = ds.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ClearSelections
SortData

Application.ScreenUpdating = False
With Sheets("LTA")
For i = 4 To LastRow

Set rLTA = .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0).EntireRow

Set rDS = ds.Rows(i)
ds40 = ds40
ds41 = ds41

If rDS.Cells(1) = ds.Range("E1") And rDS.Cells(40) >= fs.Range("C2") And rDS.Cells(41) >= fs.Range("C2") Then
ds4041s = "/" & (ds40 + ds41) & ")"
ds4041 = ds40 + ds41

rLTA.Cells(2).Value = rDS.Cells(3).Value
rLTA.Cells(3).Value = rDS.Cells(4).Value
rLTA.Cells(4).Value = rDS.Cells(5).Value
rLTA.Cells(5).Value = rDS.Cells(81).Value
rLTA.Cells(6).Value = rDS.Cells(91).Value
rLTA.Cells(7).Value = rDS.Cells(82).Value
rLTA.Cells(8).Value = rDS.Cells(92).Value
rLTA.Cells(9).Value = rDS.Cells(83).Value
rLTA.Cells(10).Value = rDS.Cells(93).Value
rLTA.Cells(11).Value = rDS.Cells(84).Value
rLTA.Cells(12).Value = rDS.Cells(94).Value
rLTA.Cells(13).Value = rDS.Cells(85).Value
rLTA.Cells(14).Value = rDS.Cells(96).Value
rLTA.Cells(15).Value = rDS.Cells(95).Value
rLTA.Cells(16).Value = rDS.Cells(86).Value
rLTA.Cells(17).Value = rDS.Cells(88).Value
rLTA.Cells(18).Value = rDS.Cells(98).Value
rLTA.Cells(19).Value = Round((rDS.Cells(57).Value / ds40) * 100, 0) & "% (" & rDS.Cells(57).Value & ds4041s
rLTA.Cells(20).Value = Round((rDS.Cells(71).Value / ds41) * 100, 0) & "% (" & rDS.Cells(71).Value & "/" & ds41 & ")"
rLTA.Cells(21).Value = Round((rDS.Cells(58).Value / ds40) * 100, 0) & "% (" & rDS.Cells(58).Value & "/" & ds40 & ")"
rLTA.Cells(22).Value = Round((rDS.Cells(72).Value / ds41) * 100, 0) & "% (" & rDS.Cells(72).Value & "/" & ds41 & ")"
rLTA.Cells(23).Value = Round(((rDS.Cells(229).Value + rDS.Cells(243).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(229).Value + rDS.Cells(243).Value) & ds4041s
rLTA.Cells(24).Value = Round(((rDS.Cells(257).Value + rDS.Cells(275).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(257).Value + rDS.Cells(275).Value) & ds4041s
rLTA.Cells(25).Value = Round(((rDS.Cells(54).Value + rDS.Cells(68).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(54).Value + rDS.Cells(68).Value) & ds4041s
rLTA.Cells(26).Value = Round(((rDS.Cells(55).Value + rDS.Cells(69).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(55).Value + rDS.Cells(69).Value) & ds4041s
rLTA.Cells(27).Value = Round(((rDS.Cells(56).Value + rDS.Cells(70).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(56).Value + rDS.Cells(70).Value) & ds4041s
rLTA.Cells(28).Value = Round(((rDS.Cells(59).Value + rDS.Cells(73).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(59).Value + rDS.Cells(73).Value) & ds4041s
rLTA.Cells(29).Value = Round(((rDS.Cells(144).Value + rDS.Cells(159).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(144).Value + rDS.Cells(159).Value) & ds4041s
rLTA.Cells(30).Value = Round(((rDS.Cells(147).Value + rDS.Cells(162).Value) / ds4041) * 100, 0) & "% (" & (rDS.Cells(147).Value + rDS.Cells(162).Value) & ds4041s
End If
Next i

End With

Application.ScreenUpdating = True
End Sub