如何值返回到一个命名的范围,而不是采用一系列的失调()

问题描述 投票:1回答:1

警告的专家:我是一个“预初学者”中的VBA ...

我有我使用的基础上找到它的名称产品的属性的工作簿。例如。在一个名为范围细胞“ALBUM_NAME”与价值“我最爱的约翰·柯川”应该返回“约翰·柯川”在命名范围“音乐家”,在命名范围“Music_genre”,“爵士”的值的值(从命名范围偏移“由78列ALBUM_NAME)等

我目前使用这样的:

Sub UpdateAttributes()

    Dim c As Range
    Dim i As Integer
    Dim vezesQueEncontrouNumero As Integer
    Dim posicaoSegundoNumero As Integer

    For Each c In Range("Album_Name", Range("Album_Name ").End(xlDown))

    vezesQueEncontrouNumero = 0
    posicaoSegundoNumero = -1

    For i = 1 To Len(c)

        If IsNumeric(Mid(c, i + 1, 1)) Then
            vezesQueEncontrouNumero = vezesQueEncontrouNumero + 1
            If (vezesQueEncontrouNumero) = 2 Then
                posicaoSegundoNumero = i
            End If
        End If
    Next
**If InStr(UCase(c.Value), UCase("John Coltrane")) > 0 Then c.Offset(0, 78).Value = "Jazz"**

我的问题:由于属性的数量正在增加它得到“c.Offset”更加困难了正确的列数。正如有时我必须在两者之间添加列,它确实行不通的!有没有把c.Value在一个名为范围,而不是使用胶印的方法吗?

谢谢。


更新

特别 - - @nwhaught我已经重写了我的VBA,但仍无济于事@Luuklag,@wallyeye,@JvdV和后帮。我还是失去了一些东西。所述VBA改变细胞的同一列中的(“名称”)不是“体裁”或“艺术家”列中的值和。 (我没有把“名”的A1,“流派”在B1和“艺术家”在C1)。不知何故,“colNum”的“属性”是行不通的。

    Sub UpdateProductAttributes()

    Dim colNum As Integer
    For colNum = 1 To 100 'or however many populated columns you end up having...
        Select Case Sheet1.Cells(1, colNum) 'Look at the column header
            Case "Genre" 'If you've found the "Genre" column
                genreColumn=colNum 'Give the genreColumn variable the correct value
            Case "Artist"
                artistColumn=colNum
        End Select
    Next

   Dim c As Range
    Dim i As Integer
    Dim vezesQueEncontrouNumero As Integer
    Dim posicaoSegundoNumero As Integer

    For Each c In Range("name", Range("name").End(xlDown))
    vezesQueEncontrouNumero = 0
    posicaoSegundoNumero = -1
        For i = 1 To Len(c)

            If IsNumeric(Mid(c, i + 1, 1)) Then
            vezesQueEncontrouNumero = vezesQueEncontrouNumero + 1

            If (vezesQueEncontrouNumero) = 2 Then
            posicaoSegundoNumero = i

            End If
            End If

    Next i

        If InStr(UCase(c.Value), UCase("Coltrane")) > 0 Then
            c.Offset(0, genreColumn).Value = "Jazz"

            ElseIf InStr(UCase(c.Value), UCase("Brad Spreadsheet")) > 0 Then
            c.Offset(0, genreColumn).Value = "Indie Folk Grunge"

        End If
        Next c
End Sub

可能是什么问题?

excel vba named-ranges
1个回答
1
投票

要扩大一些评论:索引匹配是多数民众赞成通常用于在细胞公式为更灵活的同伴VLOOKUP模式。

它的工作原理是这样的:=INDEX(YourTotalRangeOfData,MATCH("YourSearchKey",TheColumnRangeOfYourSearchKey,0),TheNumberOfTheColumnInYourTotalRangeThatYouWantToReturn)

在实践中,它看起来像这样:=INDEX(C3:E11,MATCH("Frantz",B3:B11,0),2)

在细胞中,Excel将跟踪的变化为您服务。在代码中,你会继续与更改列参考号码碰到的问题。

wallyeye的有关设置列变量注释是一个很好的,你可以做这样的:

Dim genreColumn as Integer
genreColumn = 78

**If InStr(UCase(c.Value), UCase("John Coltrane")) > 0 Then c.Offset(0, genreColumn).Value = "Jazz"**

更妙的是,在我看来,将有跑在你的代码的开始“设置”一节。安装部分的目的是将所有的列变量的为您服务。就像是:

Dim colNum as Integer
For colNum = 1 to 100 'or however many populated columns you end up having...
    Select Case Sheet1.Cells(1, colNum) 'Look at the column header
        Case "Genre" 'If you've found the "Genre" column
            genreColumn = colNum 'Give the genreColumn variable the correct value
        Case "Artist"
            artistColumn = colNum
    End Select
Next

运行在你的代码的开头,你就再也不用担心改变列位置。只要你有正确的列(这是很容易检查)的标题你就会有正确的号码。

此外,为了呼应其他人说什么,你做的很好。这看起来并不像一个“预”初学者的代码。 :)

© www.soinside.com 2019 - 2024. All rights reserved.