2017-04-14 52 views
0

我在获取数据验证列表时遇到问题,该列表要根据行5中的最后一列进行调整。excel vba用于更新验证列表的最后一列

这是我目前的。

Sub DataRange_F() 'Foundation Drop Down List 
Application.ScreenUpdating = False 

Dim LastCol As Long 
Dim Rng As Range 
Dim WholeRng As Range 
Dim ws As Worksheet 
Dim wsR As Worksheet 

Set ws = ThisWorkbook.Worksheets("Add New") 
Set wsR = ThisWorkbook.Worksheets("Foundation Plates") 

wsR.Activate 
Set Rng = Cells 

    LastCol = Rng.Find(What:="*", After:=Rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column 

    Set WholeRng = Range(Cells(5, "C"), Cells(5, LastCol)) 

    ws.Activate 
    With ws.Range("E8").Validation 
    .Delete 
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
    Operator:=xlBetween, Formula1:=WholeRng 
    .IgnoreBlank = True 
    .InCellDropdown = True 
    .InputTitle = "" 
    .ErrorTitle = "" 
    .InputMessage = "" 
    .ErrorMessage = "" 
    .ShowInput = True 
    .ShowError = True 
    End With 

Set ws = Nothing 
Application.ScreenUpdating = True 
End Sub 

它一直停在Formula1:=部分。这是我卡住的地方。我怎样才能在该公式中添加我的范围?还是有另一种方式?

感谢

+0

也许'Formula1:=“=”&WholeRng.Address'? – Jeeped

+0

'Operator:= xlBetween'不需要'Formula1'和'Formula2'吗? – Jeeped

回答

0

这是我开始工作。

Function GetColumnLetter(colNum As Long) As String 
    Dim vArr 
    vArr = Split(Cells(1, colNum).Address(True, False), "$") 
    GetColumnLetter = vArr(0) 
End Function 

Sub DataRange() 
Application.ScreenUpdating = False 

Dim startCol As String 
Dim startRow As Long 
Dim lastCol As Long 
Dim myCol As String 
Dim rng As Range 
Dim cell As Range 

Dim sht2 As Worksheet 
Set sht2 = ThisWorkbook.Worksheets("Foundation Plates") 
Dim sht7 As Worksheet 
Set sht7 = ThisWorkbook.Worksheets("Legend") 

Call Unprotect 
sht2.Activate 

startCol = "C" 
startRow = 5 
lastCol = sht2.Cells(5, sht2.Columns.Count).End(xlToLeft).Column 
myCol = GetColumnLetter(lastCol) 

Set rng = sht2.Range(startCol & startRow & ":" & myCol & "5") 

'For error checking the range 
'MsgBox rng.Address 

    sht7.Activate 
    With sht7.Range("F8").Validation 
    .Delete 
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
    Operator:=xlBetween, Formula1:="=" & "'" & sht2.Name & "'!" & rng.Address 
    .IgnoreBlank = True 
    .InCellDropdown = True 
    .InputTitle = "" 
    .ErrorTitle = "" 
    .InputMessage = "" 
    .ErrorMessage = "" 
    .ShowInput = True 
    .ShowError = True 
    End With 

Call Protect 
sht2.Activate 

Set sht2 = Nothing 
Set sht7 = Nothing 
Set rng = Nothing 

Application.ScreenUpdating = True 
End Sub 
1

尝试像这样...

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
    Operator:=xlBetween, Formula1:="=" & "'" & wsR.Name & "'!" & WholeRng.Address 
+0

Thx,这样做。它向数据列表添加了几列,但不是全部。所以现在即时调试我的'WholeRng'。它没有选择范围,直到结束。你有机会看到我搞砸了的东西吗? –

+1

尝试使用LastCol = wsR.UsedRange.Columns.Count同时检查数据验证,哪些范围用作源? – sktneer

1

尝试,因为,

..., Formula1:=Chr(61) & WholeRng.Cells(1).Address(external:=true), Formula2:=Chr(61) & WholeRng.Cells(WholeRng.Cells.Count).Address(external:=true) 
相关问题