Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

add data validation to specific columns based on another sheet

0

hello

I  try  making  macro  by  vba   , what  I'm trying   link  and  add  datavaldition in sheet2  in COL B,C,D     based   what   it's  existed in  sheet1  as    you  see   in  sheet1    I add   data   continously    and  it  will  be  repeated  some  data   the  differences in  the  dates    then   when   bring   the  data  in   sheet2   should   not  be  duplicated  and  not  blanks   the  fill in data  validations   depends on  only    what  are  filled  in sheet 1   in COL B,C ,D   without  duplicated  and  without  blanks     and  it  should  automatically  show     new  data  in datavalidations  in sheet2   when  add  new  data  in sheet1     so   the  data validations  takes   the  data  based  what  enter in sheet1    

finally  after   link   them  when select   the   item   from  col B  in sheet2  then   would be depends on each other of them  COL C,D as  are existed in sheet1 


Sub dat()
Dim ws, ws1 As Worksheet
Dim range1 As Range, rng As Range
'change Sheet1 to suit
Set ws = ThisWorkbook.Worksheets("Sheet1")

Set range1 = ws.Range("b2:b6,c2:c6,d2:d6")

Set ws1 = ThisWorkbook.Worksheets("Sheet2")
Set rng = ws.Range("b2:b6,c2:c6,d2:d6")

With rng.Validation
    .Delete 'delete previous validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Formula1:="='" & ws.Name & "'!" & range1.Address
End With
End Sub
Answer
Discuss

Discussion

Tubrak I'm not sure I understand your problem (or goal) correctly but I don't think your approach will work.

Even if you add data validation in columns B,C and D of sheet 2 based on "existing" values in the same in sheet 1, the columns would be independently validated so you could pick an combination that does not exist in sheett 1 (e.g.asd-33, bt-tt3, aaa-126). Would that be a problem?
John_Ru (rep: 6142) Jun 7, '21 at 8:44 am
Hi  John ,
   this   is  an important  point  but  i  don't  think    that's  possible  the columns would be together  validated    if  that's  possible  ,  it will be thats   better ,   then  should  show  message   " these data  are  not  matched" and  clear  the  datavalidations in B,C,D    if  that's  not  possible   then the columns would be independently validated .
any  suggestion   I  accept  of it
tubrak (rep: 24) Jun 7, '21 at 9:11 am
Tubrak. Please edit your original question to explain what you are trying to achieve (e.g. new entries in Sheet 2 are to be selected from existing data sets in Sheet 1) so others understand your goal and suggest a solution. Do new entries in Sheet 2 reserve or take stock from those on Sheet 1 or is there some other relationship between the sheets?
John_Ru (rep: 6142) Jun 7, '21 at 10:01 am
John 
see  my  update   I  no  know    if  that's  help   , actually  I  do  my  best 
tubrak (rep: 24) Jun 7, '21 at 11:44 am
Add to Discussion

Answers

0

There are two principles to observe.

  1. A validation list can't have more than one column.
  2. A virtual (non-existant) list can't be assigned to a validation drop-down.

Therefore you will need to create inter-dependent validations. For example, when the user selects a "Code" from the first DD the second DD shows only those "Batches" which match the selected code. And similarly for "Type".

Also, you will have to plan on actually having the lists you want to show in your DDs somewhere. You may find it convenient to create a hidden worksheet especially for this purpose and keep all your lists there.

Now, this formula will create one such list. It will include only non-blank values and no duplicates. You can set the end of the range at 10000 or 100000 or 1000000. Just make it much bigger than what you expect to need so that you can add rows and find them included.

=UNIQUE(FILTER(sheet1!B2:B1000,sheet1!B2:B1000 <> ""))

Let us say, you created a worksheet "Lists" and pasted the above formula in Lists!A2. So, your list is in Lists!A2:A6. Now you can use that list in a validation DD.

=OFFSET(Lists!A2,0,0,COUNTA(Lists!A:A))

Observe that the array formula creating the list creates a list of dynamic length while the formula in the validation cuts off blank cells at the end of that list.

This will work perfectly but I wouldn't do it that way because I would want more transparency. Perhaps you feel the same way. This is how to do it.

Create a named range in the sheet Lists with the same above formula. Call the range "Codes". Now the formula in the validation is simply

=Codes
Discuss

Discussion

Tubrak. Please note that the great solution proposed by Variatus will work well for Excel 365 users but are you still using Excel 2019 (as your profile suggests)?

If so, you can try on the free web version of Excel 365 but you'll find the UNIQUE and FILTER functions aren't available in Excel 2019 (and earlier versions). Array formulae are but need to be entered differently (using Ctrl+Shift+Return).
John_Ru (rep: 6142) Jun 8, '21 at 4:38 am
Hi John,
I did consider that and thought 2019 wasn't so long ago and it should be OK. Can you clarify: Will my function work in Excel 2019 if entered as an array formula? Or is it that UNIQUE and FILTER aren't available?
Variatus (rep: 4889) Jun 8, '21 at 4:49 am
Variatus, Microsoft list those functions as 365 only (but I just editted my comment to mention that the web version is avaialble for free too). I no longer have a version of 2019 to try. Here are their articles: UNIQUE function and FILTER function
John_Ru (rep: 6142) Jun 8, '21 at 5:01 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login