最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - Add Cell validation - Stack Overflow

programmeradmin5浏览0评论

I want to add the following data validation to a cell (in this case cell N3) using VBA.

=INDIRECT("Cabletype_"&MATCH(M3;Cabletype_Names)&"_Size")

Next to the Cell "N3" in Cell "M3" is the name of the Cable. The formula searches for the position in the array "Cabletype_Names". Let's say it's in the third position, so it would construct the Name "Cabletype_3_Size". And that's the list of all sizes that specific cable can be, which should be my validation.

This works if I use the validation Pop-Up-window. However, if I try to set the validation with VBA, I just cannot get it to work.

Here is my VBA code that doesn't work.

Formula_str = "=INDIRECT(""Cabletype_""&MATCH(M3;Cabletype_Names)&""_Size"")"
   
   With Worksheets(Tablenname).Columns(13).Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Operator:= _
         xlEqual, Formula1:=Formula_str
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .errorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
   End With

If I switch the formula to something simple like just Formula_str = "=Cabletype_3_Size" the VBA-code works as well. So it has to do with the specific formula that I want to insert.

I want to add the following data validation to a cell (in this case cell N3) using VBA.

=INDIRECT("Cabletype_"&MATCH(M3;Cabletype_Names)&"_Size")

Next to the Cell "N3" in Cell "M3" is the name of the Cable. The formula searches for the position in the array "Cabletype_Names". Let's say it's in the third position, so it would construct the Name "Cabletype_3_Size". And that's the list of all sizes that specific cable can be, which should be my validation.

This works if I use the validation Pop-Up-window. However, if I try to set the validation with VBA, I just cannot get it to work.

Here is my VBA code that doesn't work.

Formula_str = "=INDIRECT(""Cabletype_""&MATCH(M3;Cabletype_Names)&""_Size"")"
   
   With Worksheets(Tablenname).Columns(13).Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Operator:= _
         xlEqual, Formula1:=Formula_str
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .errorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
   End With

If I switch the formula to something simple like just Formula_str = "=Cabletype_3_Size" the VBA-code works as well. So it has to do with the specific formula that I want to insert.

Share Improve this question edited 3 hours ago BigBen 50k7 gold badges28 silver badges44 bronze badges asked 4 hours ago NanskiNanski 211 silver badge1 bronze badge New contributor Nanski is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • 1 What happens if you switch the ; to a ,? – BigBen Commented 4 hours ago
  • It works after that change, cheers. See my other comment in this thread. – Nanski Commented 4 hours ago
Add a comment  | 

1 Answer 1

Reset to default 2

The issue is due to the use of semicolons in your formula. Even if Excel (depending on your regional settings) accepts semicolons in worksheet formulas, VBA requires the formula string to use commas (,) as argument separators. Also, it's a good idea to specify the match type for the MATCH function (commonly 0 for an exact match).

Try changing your formula string to:

Formula_str = "=INDIRECT(""Cabletype_"" & MATCH(M3, Cabletype_Names, 0) & ""_Size"")"
发布评论

评论列表(0)

  1. 暂无评论