Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 循环遍历列以根据标题获取列号_Vba_Excel - Fatal编程技术网

Vba 循环遍历列以根据标题获取列号

Vba 循环遍历列以根据标题获取列号,vba,excel,Vba,Excel,我有一个模板,有一组170列和标题标题行1单元格名。这始终是相同的,直到用户在它们之间添加列,并被指示不要更改标题。这样做的目的是使其在添加列时能够防篡改 我想用变量来保存一些标题,这些标题可以保存所有标题,并用模板检查这些标题,以找出我估计的循环中的列数。做一个函数调用它可能是最明智的 Dim ColHeader1Str as string 'literal row 1, column 1 value (which is always 'the same string and positio

我有一个模板,有一组170列和标题标题行1单元格名。这始终是相同的,直到用户在它们之间添加列,并被指示不要更改标题。这样做的目的是使其在添加列时能够防篡改

我想用变量来保存一些标题,这些标题可以保存所有标题,并用模板检查这些标题,以找出我估计的循环中的列数。做一个函数调用它可能是最明智的

Dim ColHeader1Str as string 'literal row 1, column 1 value (which is always 
'the same string and position in the template)
Dim iColHeader1 as integer 'holds the (to be set) value of the column number

Set ColHeader1Str = "ColHeader1"
现在我想要一个循环,它在最后一列=200的所有列中循环,检查与ColHeader1Str匹配的列号,并将其存储在iColHeader1中

比如:

Function find_columnNmbr
  Dim i As Integer
    For i = 1 To 200 Step 1
     If 'ColHeader1Str matches actual column header name
        'set found integer as iColHeader1 and so forth
        Exit For
     End If
    Next
End Function`
我知道我错过了一些步骤,我希望你们能帮助我

更新:模板已设置列标题。当用户与它交互时,结果可能是列移动了位置,或者他们添加了更多。我有一个工作簿,需要从用户修改过的模板中加载数据

也就是说,模板有第1、2、3、4列,名称是第1、2列等。用户广告一个随机列,所以现在有5个。循环需要循环遍历列标题的名称,并根据具有原始名称的字符串变量标识原始模板列1、2等的列号,这是我之前硬编码的。这些是公共常量

不确定你在寻找什么,但这里有一个例子

LookForHeaders函数的作用是:输入一个字符串,然后在usersheet.range1:1中搜索该字符串。如果找到,则返回该单元格的列号,否则返回0

Private Function LookForHeaders(ByVal headerName As String) As Long
    Dim rng As Range
    Dim userSheet As WorkSheet
    Set userSheet = 'userSheet here'

    On Error GoTo NotFound
    LookForHeaders = userSheet.Range("1:1").Find(headerName).Column
    Exit Function
NotFound:
    LookForHeaders = 0
End Function

Private Sub Test()
    Dim rng As Range
    Dim template As WorkSheet
    Set template = 'template here'
    For Each rng In template.Range(Cells(1,1), Cells(1,200))
        iColHeader1 = LookForHeaders(rng.Value)
        'Do something with iColHeader1
    Next rng
End Sub

不确定你想要达到什么。假设A1=标题A,B1=标题B等等,您是否希望找到特定的标题名称,如标题G,并在iColHeader1中存储7,因为G列是第7列?如果是,Range1:1.FindColHeader1Str可以返回找到ColHeader1Str的范围。更新了原始问题@newacc2240@newacc2240:对第一个问题回答“是”。因此,您正在用户编辑的图表和模板之间绘制参考图表?已更新问题
Private Function LookForHeaders(ByVal headerName As String) As Long
    Dim rng As Range
    Dim userSheet As WorkSheet
    Set userSheet = 'userSheet here'

    On Error GoTo NotFound
    LookForHeaders = userSheet.Range("1:1").Find(headerName).Column
    Exit Function
NotFound:
    LookForHeaders = 0
End Function

Private Sub Test()
    Dim rng As Range
    Dim template As WorkSheet
    Set template = 'template here'
    For Each rng In template.Range(Cells(1,1), Cells(1,200))
        iColHeader1 = LookForHeaders(rng.Value)
        'Do something with iColHeader1
    Next rng
End Sub