在VBA中不选择右行的范围方法 - Excel

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

我一直在为VBA范围内的替代行着色。问题是Range方法似乎没有选择适当的范围,我最终着色相邻的单元格。这是我想要的输出:

desired output. Alternate rows colored by grey

但是,这就是我实际得到的:

enter image description here

这是我创建的代码:

Sub limpar_aniversariantes()
    Worksheets("Aniversariantes").Range("B4:D900").ClearContents
    'Worksheets("Aniversariantes").Range("B4:D900").Interior.Color = RGB(255, 255, 255)
End Sub


Sub gerar_lista_aniversariantes()
    limpar_aniversariantes
    Dim newrange As Range, rw As Range
    Sheets("Base de Alunos").Select
    Set newrange = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
    CountInicioMes = 12
    CountInicio = 16
    Count = CountInicio
    count_first_line = 0
    For Each rw In newrange.Rows
        Worksheets("Aniversariantes").Range("B" & Count).Value = rw.Cells(2).Value
        Worksheets("Aniversariantes").Range("C" & Count).Value = Left(rw.Cells(5).Value, 2)
        Worksheets("Aniversariantes").Range("C" & Count).NumberFormat = "DD"
        Worksheets("Aniversariantes").Range("D" & Count).Value = UCase(rw.Cells(15).Value)
        'Worksheets("Aniversariantes").Range("A" & Count).Value = Count
        'pintar linhas alternadamente de cinza
        If Count Mod 2 = 0 Then
            Worksheets("Aniversariantes").Range("B" & Count & ":D" & Count).Interior.Color = RGB(211, 211, 211)
            Debug.Print ("B" & Count & ":D" & Count)
        End If
        'pegar mes dos aniversariantes, eh preciso pular header
        If count_first_line < 2 Then
            count_first_line = count_first_line + 1
            If count_first_line = 2 Then
               my_date = rw.Cells(5).Value
            End If
        End If
        'Debug.Print Left(rw.Cells(5).Value, 2)
        Count = Count + 1
        'Debug.Print rw.Cells(2).Value
    Next rw
    Worksheets("Aniversariantes").Range("B" & (CountInicio + 1) & ":D" & Count).Sort key1:=Worksheets("Aniversariantes").Range("C" & (CountInicio + 1) & ":C" & Count), _
    Header:=xlNo
    'limpar bordas anteriores
    Worksheets("Aniversariantes").Columns("B:D").Borders.LineStyle = xlNone
    Worksheets("Aniversariantes").Range("B" & (CountInicio) & ":D" & (Count - 1)).Borders.LineStyle = xlContinuous
    '.Weight = xlThin.ColorIndex = 3
    my_month = Mid(my_date, 4, 2)
    my_month_written = RetornarMes(CInt(my_month))
    Worksheets("Aniversariantes").Range("B" & CountInicioMes).Value = UCase(my_month_written)
    Worksheets("Aniversariantes").Range("B" & CountInicio).Value = "NOME"
    Worksheets("Aniversariantes").Range("C" & CountInicio).Value = "DIA"
    Worksheets("Aniversariantes").Range("D" & CountInicio).Value = "MODALIDADE"
    'mudar cor de fundo
    'Worksheets("Aniversariantes").Range("B" & CountInicio & ":D" & CountInicio).Interior.Color = RGB(255, 255, 0)
    'Debug.Print my_month_written

End Sub

我正在处理从一个工作表到另一个工作表的值,我使用MOD函数只对颜色均匀线条。每次我复制这些值时,行数都会改变,这就是我需要通过VBA来实现的原因。我没有在VBA中掌握,所以任何帮助都表示赞赏。我整个上午都在努力做到这一点。

编辑:我注意到从PERSON的数据表中选择一行使得Range()。Interior.Color函数正常工作,问题在范围内。

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

行,

感谢@Thomas Inzina评论,我意识到公式“= MOD(ROW(),2)<> 0”应该翻译成我的母语excel语言,e.i:葡萄牙语。然后右翻译是“= MOD(LIN(); 2)<> 0”。但是,我仍然没有弄清楚为什么我会在我的问题中表现出那种奇怪的行为,而是使用这个解决方案(使用正确的公式)来解决我的问题:

https://stackoverflow.com/a/15957075/1171721

我很满意,因为我得到了我想要的东西,但是如果有人发现为什么我使用旧方法得到这种奇怪的行为,我会很乐意为了学习而测试所提出的解决方案。

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