2016-05-27 317 views
4

必需:引用列表中的列值。使用vba增加excel列參考? Z到AA,AA到AB

在一個工作表中有n個行,每個單元格都有一個列表,這個列表是從另一個工作表中的列值引用的。我創建了下面的代碼,但它在Z之後中斷,因爲ASCII值不適用於AA,AB ......

如何使用VBA創建所有行的列表?

Sub createList() 
'creating custom list referencing cells from another sheet 

Sheets("Checklist").Select 
Dim i As Integer 

For i = 1 To 100 

    Dim k As String 
    k = "='Parameter Options'!$" & Chr(64 + i) & "$1:$" & Chr(64 + i) & "$10" 

    'Parameter Options is the sheet i am taking list values from 

    Range("A" & i & ":C" & i).Select 

    With Selection.Validation 
     .Delete 
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
     xlBetween, Formula1:=k 
    End With 

Next i 
End Sub 
+5

嘗試使用數列與函數將其轉換爲 - 更多信息一封信:?功能列數轉換爲字母(http://stackoverflow.com/questions/12796973/function-to-convert-column-number-to-letter) –

回答

5

使用Range.Address property外部:=真捕捉到工作表的名稱以及單元格區域地址。當你在循環中遞增時,Range.Offset property錯開了你的選擇。

Sub createList() 
    'don't declare your vars inside a loop!!! 
    Dim k As String, i As Long 

    For i = 1 To 100 

     With Worksheet("Parameter Options") 
      k = "=" & .Range("A1:A10").Offset(0, i - 1).Address(external:=True) 
      'debug.print k 
     End With 

     'Parameter Options is the sheet i am taking list values from 
     With Worksheets("Checklist").Range("A" & i & ":C" & i).Validation 
      .Delete 
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
       Operator:=xlBetween, Formula1:=k 
     End With 

    Next i 
End Sub 
3

使用你的代碼,如果添加此修改將正確地轉換雙字母列,但在評論所指出的,你最好使用列數,更簡單。

但是一個簡單快捷的解決方案,這將做到這一點:

Dim i As Integer 

    Dim k As String 
    Dim col As String 

For i = 1 To 100 

    If i < 27 Then 
     col = Chr(64 + i) 
    Else 
     col = Chr(64 + Int(i/26)) & Chr(64 + i - (Int(i/26) * 26)) 
    End If 

    k = "='Parameter Options'!$" & col & "$1:$" & col & "$10" 

    'Parameter Options is the sheet i am taking list values from 

    Range("A" & i & ":C" & i).Select 

    With Selection.Validation 
     .Delete 
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
     xlBetween, Formula1:=k 
    End With 

Next i 
+1

三字母列應該有第三種可能性嗎? – Jeeped

+0

@Jeeped - 我在Excel2003中測試它,並且2個字母都可以,但是如果在高版本中不是這種情況,那麼我肯定可以添加。但是根據OP,它是從1到100達到DD,而不是更多。 – ib11

+0

我在這裏發佈了3個字母的版本:http://stackoverflow.com/a/37504072/6201755 – ib11

5

don't need column letters使用代碼時。

Sub createList() 
    'creating custom list referencing cells from another sheet 

    Dim i As Long 

    For i = 1 To 100 
    Dim k As String 
    k = "='Parameter Options'!R1C" & i & ":R10C" & i 

    With Worksheets("Checklist").Range("A" & i & ":C" & i).Validation 
     .Delete 
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Application.ConvertFormula(k, xlR1C1, xlA1) 
    End With 
    Next i 

End Sub 
+0

還沒有看到[Application.ConvertFormula方法](https://msdn.microsoft.com/en-us/library /office/ff822751.aspx)一段時間。在這裏很好的使用它。 – Jeeped

1

這是我的解決方案。 我在另一個內部生成一個循環,以處理代碼ASCII中出現Z的值。

希望這可以幫助你:

For i = 0 To RecordSet.Fields.Count - 1 'This is my data source 
    If Ascii > 90 Then 
     Ascii = 65 
     For y = i To RecordSet.Fields.Count - 1 
      Hoja1.Range("A" & Chr(Ascii) & 3).Value = RecordSet.Fields(y).Name 
      Ascii = Ascii + 1 
     Next 
    Else 
     Hoja1.Range(Chr(Ascii) & 3).Value = RecordSet.Fields(i).Name 
     Ascii = Ascii + 1 
    End If 
Next 
0

溶液找到columnletters也爲使用模算術比702(=三個字母)更大的列。注意:它不檢查有效性。

代碼

Function colChar(colNo As Long) As String 
If colNo < 1 Then Exit Function 
Dim n As Long 
Dim c As Byte 
Dim s As String 

n = colNo 
Do 
    c = ((n - 1) Mod 26) ' locate within A-Z 
    s = Chr(c + 65) & s  ' combine characters 
    n = (n - c) \ 26  ' check the rest 
Loop While n > 0 
colChar = s     ' return character(s) 
End Function 
相關問題