使用vba增加excel列引用?Z到AA,AA到AB

使用vba增加excel列引用?Z到AA,AA到AB,vba,excel,Vba,Excel,必需:引用列表中的列值 一张工作表中有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

必需:引用列表中的列值

一张工作表中有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

使用您的代码,如果您添加此修改,它将正确转换双字母列,但正如注释中所述,您最好使用更直接的列编号

但是,对于简单快速的解决方案,这可以做到:

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
Dim i作为整数
调暗k为字符串
像线一样暗的颜色
对于i=1到100
如果我小于27,那么
col=Chr(64+i)
其他的
col=Chr(64+Int(i/26))和Chr(64+i-(Int(i/26)*26))
如果结束
k=“=”参数选项“!$”&列和“$1:$”&列和“$10”
'参数选项是我从中获取列表值的工作表
范围(“A”&i&“:C”&i)。选择
选择。验证
.删除
.Add类型:=xlValidateList,AlertStyle:=xlValidAlertStop,运算符:=_
xlBetween,公式1:=k
以
接下来我
使用with external:=true来捕获工作表名称以及单元格范围地址。在循环中递增时,将错开选择

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
您可以在使用代码时使用它

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
这是我的解决方案。 我在另一个循环中生成一个循环,以处理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

使用模运算法为大于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
函数colChar(与colNo一样长)作为字符串
如果colNo<1,则退出函数
长
作为字节的Dim c
像线一样变暗
n=colNo
做
c=((n-1)Mod 26)“位于A-Z范围内
s=Chr(c+65)&s'组合字符
n=(n-c)\26'检查其余部分
n>0时循环
colChar=s'返回字符
端函数

尝试使用带有函数的列号将其转换为字母-此处的详细信息:三字母列是否有第三种可能性?@Jeeped-我在Excel2003中测试了它,两个字母是可以的,但如果在更高版本中不是这样,我当然可以添加。但是根据OP,它是1到100,这取决于DD,而不是更多。已经有一段时间没有看到了。这里用得好。