Excel VBA - Dynamic value if a condition is true, add it

Oregon92

New Member
Joined
Jun 25, 2021
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I am trying to highlight each row where a value is present in another list of values (it doesn't need to be in the same order, just needs to be present)

This is a line of code in SAP that highlights the row(s). In this example, it highlights all rows 0-11 (starts at 0 because of a 0 index).
session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows = "0,1,2,3,4,5,6,7,8,9,10,11"

Assuming in this example that all values are present, the selected rows has to equal "0,1,2,3,4,5,6,7,8,9,10,11"
Any thoughts on how to produce code that does this?
  • In my attempt, the first loop highlights row 0 (visually speaking, this is row 1 because of the 0 index)
    session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows ="0,0,1"
    (I'm not sure why it's populating 0 twice, would ideally just be "0,1" but in any case, it seems to be working)

  • The second time through the loop, produces session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows ="0,1,2" which is exactly what I would hope to see

  • The third time through the loop is where we get lost. It produces session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows ="0,2,3" whereas I would hope to see session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows ="0,1,2,3"

  • By the time I reach the final loop, I get session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows ="0,10,11" as opposed to the ideal session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows ="0,1,2,3,4,5,6,7,8,9,10,11"

VBA Code:
overview_A = 0
overview_A_counter = 0
LRow2 = Cells(Rows.count, 10).End(xlUp).offset(4, 0).row

Do While overview_A <> 11
        If Cells(LRow2 + overview_A, 10).Value = Cells(8 + overview_A, 10) Then
            session.findById("wnd[0]/usr/cntlG_CUSTOM_CONTAINER/shellcont/shell[0]").SelectedRows = overview_A_counter
            overview_A_counter = Application.WorksheetFunction.concat("""", 0, ",", overview_A, ",", Application.WorksheetFunction.concat((Application.WorksheetFunction.Sum(overview_A + 1))))
            overview_A = overview_A + 1
        End If
   Loop

Thank you for any thoughts you might be able to provide!
 

Attachments

  • Exist.png
    Exist.png
    38.2 KB · Views: 16

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Why don't you try conditional formatting and use VLOOKUP wrap inside Not(isrror(vlookup)))
 
Upvote 0
Why don't you try conditional formatting and use VLOOKUP wrap inside Not(isrror(vlookup)))
Thanks for your reply. Conditional formatting doesn’t translate into SAP code and won’t produce a value of "0,1,2,3,4,5,6,7,8,9,10,11" Without that value, none of the rows get selected in SAP
 
Upvote 0
If i understood correctly the loop should generate strings like
0,1
0,1,2
....
....
0,1,2,3,4,5,6,7,8,9,10,11

So, maybe something like this

VBA Code:
Dim overview_A, overview_A_counter As String

overview_A = 0
overview_A_counter = 0
Do While overview_A <> 11
    'If some condition <-- insert the If here
    overview_A_counter = overview_A_counter & "," & overview_A + 1
    Debug.Print overview_A_counter
    overview_A = overview_A + 1
    'End if
Loop

M.
 
Upvote 0

Forum statistics

Threads
1,224,856
Messages
6,181,425
Members
453,039
Latest member
jr25673

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