VBA Row offset when dynamically changing cell value

mattadams84

Board Regular
Joined
Oct 30, 2016
Messages
54
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 :

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...


Code:
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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Cross posted http://www.vbaexpress.com/forum/sho...w-offset-when-dynamically-changing-cell-value

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Thanks for letting me know. Just an update, i ran the code on a different machine, it actually works, but it does not produce the desired result at all.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top