Auto filling a cell in a table which has a list using data validation

rjn008

New Member
Joined
May 12, 2023
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Good Afternoon,

Is it possible to auto populate a cell in a table where by the cell adjacent left has a format on it. The only issue I think I have is I use a list on the cell under data validation, but the list does contain the value I wish to have auto populated?

This is the mini sheet code and image of the style of sheet I am using;

The New LMS ^N4.xlsm
IJKLMNO
5Week 1Week 2Week 3Week 4Week 5Week 6Week 7
6ArsenalMan CityChelseaxxxx
7Man UtdNewcastle Utdxxxxx
8LiverpoolFulhamArsenalxxxx
9ArsenalMan CityChelseaxxxx
10ArsenalMan CityChelseaxxxx
11ArsenalMan CityBrentfordAston VillaLiverpoolAFC BournemouthWest Ham Utd
12ArsenalMan CityChelseaxxxx
13Newcastle UtdLiverpoolChelseaxxxx
14Man UtdMan CityBrentfordBrighton & HAxxx
15Newcastle UtdTottenhamChelseaxxxx
16ArsenalChelseaFulhamxxxx
17ArsenalMan CityChelseaxxxx
18Newcastle UtdTottenhamBrentfordBrighton & HAxxx
19Newcastle UtdLiverpoolArsenalxxxx
20ArsenalTottenhamBrentfordAFC Bournemouthxxx
21LiverpoolMan CityArsenalxxxx
22ArsenalMan CityChelseaxxxx
23LiverpoolTottenhamChelseaxxxx
24LiverpoolMan CityChelseaxxxx
25LiverpoolMan CityArsenalxxxx
26Newcastle UtdLiverpoolAston VillaBrighton & HAxxx
27ArsenalTottenhamChelseaxxxx
28Man UtdLiverpoolChelseaxxxx
29ArsenalTottenhamBrentfordBrighton & HAxxx
30Newcastle UtdTottenhamAston VillaMan UtdLiverpoolArsenalWest Ham Utd
31Man UtdFulhamNotts Forestxxxx
32Newcastle UtdTottenhamMan CityAston VillaMan Utdxx
33LiverpoolMan CityAston VillaBrighton & HAxxx
34ArsenalTottenhamAston VillaMan CityNewcastle Utdxx
35Newcastle UtdLiverpoolArsenalxxxx
36Newcastle UtdMan CityChelseaxxxx
37ArsenalMan CityBrentfordBrighton & HAxxx
38Newcastle UtdMan CityAston VillaMan UtdTottenhamArsenalChelsea
39Newcastle UtdLiverpoolAston VillaMan CityTottenhamChelseaArsenal
40LiverpoolMan CityFulhamxxxx
41Man UtdFulhamChelseaxxxx
42LiverpoolTottenhamBrentfordBrighton & HAxxx
43ArsenalChelseaAston VillaMan UtdLiverpoolMan Cityx
44ArsenalMan CityChelseaxxxx
45Newcastle UtdTottenhamChelseaxxxx
46Newcastle UtdLiverpoolChelseaxxxx
47Newcastle UtdTottenhamBrentfordAston VillaLiverpoolArsenalAFC Bournemouth
48ArsenalNewcastle Utdxxxxx
49Newcastle UtdMan CityBrentfordCrystal Palacexxx
50ArsenalTottenhamChelseaxxxx
51Newcastle UtdTottenhamBrentfordBrighton & HAxxx
52Tottenhamxxxxxx
53ArsenalMan CityAston VillaBrighton & HAxxx
54Newcastle UtdFulhamChelseaxxxx
55Newcastle UtdFulhamBrentfordBrighton & HAxxx
56Newcastle UtdTottenhamAston VillaMan CityLiverpoolAFC BournemouthChelsea
LMS #4
Cells with Conditional Formatting
CellConditionCell FormatStop If True
H3:XFD3,1:2,A3:E3,4:1048576Expression=ROW(A1)=CurrentRowtextNO
Cells with Data Validation
CellAllowCriteria
I6:O56List=$H$139:$H$159



I am looking to auto populate the 'x' in any cell that has a strikethrough on the adjacent left cell? Obviously as the weeks move on I would need this to happen in any cell adjacent to a strikethrough no matter what column it was in?

1731334229474.png
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I suspect you could do this with VBA (a macro) but the sheet events are limited. Perhaps could use right click or double click on a cell but a button on the sheet would be better, I think. Or you call the code from the QAT or Macros option on the Code tab. My thinking is that the logic is like so:
- code loops through all cells in a range (don't know if that's every column you've shown or not)
- it determines if the cell font is strikethrough or not; e.g. ( .range("G4").characters.font.strikethrough ) for example
- - if not it moves on to the next cell
- - if true, it tests if next cell to right contains any value.
- - - if true then do nothing. Either there is already an x there, or you have 2 adjacent cells with strikethrough.
- - - if false, put an x in the cell to the right and each cell to the last column. Again, not sure where that will be in the future.

If there is a cell formula or CF formula that will do what you want there's no way I could figure that out.
 
Upvote 0
I suspect you could do this with VBA (a macro) but the sheet events are limited. Perhaps could use right click or double click on a cell but a button on the sheet would be better, I think. Or you call the code from the QAT or Macros option on the Code tab. My thinking is that the logic is like so:
- code loops through all cells in a range (don't know if that's every column you've shown or not)
- it determines if the cell font is strikethrough or not; e.g. ( .range("G4").characters.font.strikethrough ) for example
- - if not it moves on to the next cell
- - if true, it tests if next cell to right contains any value.
- - - if true then do nothing. Either there is already an x there, or you have 2 adjacent cells with strikethrough.
- - - if false, put an x in the cell to the right and each cell to the last column. Again, not sure where that will be in the future.

If there is a cell formula or CF formula that will do what you want there's no way I could figure that out.
Thanks @Micron, I aren't a VBA person, are you? Could you draft me the code up to test?
 
Upvote 0
Thanks @Micron, I aren't a VBA person, are you?
I dabble in Excel VBA; I know Access VBA better.
I probably can come up with something in between doing other things on my plate, so just not right away. It would be nice to know
- where the right most column would be for inserting X (or will it be dynamic?)
- where the left most column is - can't tell if Week1 is col A or something else
- OK if I code it so that X is put in every cell to the right most column in your range (i.e. if there's anything else in there it will get over written)?

That's all I can think of that is unclear at this point. If you can answer those questions I'll take a stab at it. I'd prefer to not just do it and have you ask for a bunch of tweaks that I could have prevented.
EDIT - even better if you could upload a file copy to a file sharing service and post a link (but still clear up where the right most column will be). You can't upload files here.
 
Upvote 0
I had some time (and a nice glass of Shiraz to keep me going). This works for me:
VBA Code:
Private Sub CommandButton1_Click()
Dim Lrow As Long, Lcol As Long
Dim rng As Range, rng2 As Range, cel As Range
Dim row

On Error GoTo errHandler
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

With ActiveSheet
'find last row and column with data
    Lrow = .Cells.Find(what:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).row
    Lcol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End With
'create range to loop over based on last row & column with data
Set rng = Range(Cells(2, 1), Cells(Lrow, Lcol))
'loop over rows
For Each row In rng.rows
    'loop over cells in the current row
    For Each cel In row.Cells
        'if cel characters font setting is strikethrough:
        If Not cel Is Nothing And cel.Characters.Font.Strikethrough Then
            'create range from next column to right to last column (based on what Lcol is)
            Set rng2 = Range(Cells(cel.row, cel.Column + 1), Cells(cel.row, Lcol))
            'remove potential strikethrough setting from rng2 lest a cell have previously been strikethrough
            rng2.Characters.Font.Strikethrough = False
            'set rng2 cells to equal X
            rng2 = "X"
        End If
    Next
Next

exitHere:
'clean up
Set rng = Nothing
Set rng2 = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
Exit Sub 'exit if no error

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere

End Sub
I have done limited testing. I can see that if you delete a value that was strikethrough and don't remove that formatting, it will cause x's to be placed in that row. I think you can avoid that if you want to change a line to this:
If Not cel Is Nothing And cel.Characters.Font.Strikethrough And cel <> "" Then

HTH

EDIT - I know a lot of coders don't like looping over rows/columns but I don't see how to avoid that given that for any row, the starting point can be any column. I did manage to avoid looping over a range to set the X though. With your data sample, it is very fast for me, so unless you find it slow, I wouldn't worry about that.
 
Upvote 0
Thanks @Micron I will give it a try. For clarity the cell week 1 on my sheet is actually 'I5' so I trust I just change 'A1' to 'I5' in your coding?

1731655044734.png


I can use it on sample data myself before putting it into my actual working sheet.

Thanks again, I will be back later on today with the outcome.

One quick question, is this working code or does it need a button like I have on my 'Teams Count' above?
 
Upvote 0
As per item number 1) in my tips in my signature, what is "isn't working"?
The code should be on a sheet since the button is on a sheet. I didn't write it for use in a standard module so that may be the issue. Might not be the issue but that's what I suggest you try first.
Your link requires authorization, so I clicked the option to ask for it. Not sure what that all means.

EDIT - After a few sips of coffee, I can see that as it is, the code will have no idea what sheet the Range is on because it wasn't written to be used in a standard module.
 
Last edited:
Upvote 0
Sorry for being daft...

How would I assign your code to the button then, when it isn't from a module? I can add the code to the sheet code, but I can't get my head round how it would work.

I have given you permission to the file now, so hopefully you can update for me and I can understand what you have done.
 
Upvote 0
How would I assign your code to the button then, when it isn't from a module? I can add the code to the sheet code, but I can't get my head round how it would work.
The code behind a sheet is in a sheet module. There are 3 types of modules in Excel (that I know of): class, standard, sheet and maybe Workbook (not sure of that). Look beside the code in the image you posted - you have 15 sheet modules; the last in the list is the workbook module. Double click the name of the sheet that this code is to be used on to open it's module and paste the code into there somewhere.

That link is for editing the data in a Google sheet. You'd need to upload a file as a file share to anyone with the link, unless you know how to give permissions for a file download to a particular person. I don't but I suppose it might depend on the file share application. Or maybe there is a way to get at the code behind a Google sheet but I've never used them.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

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