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-Excel中未选择右行的范围方法_Vba_Excel - Fatal编程技术网

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

在VBA-Excel中未选择右行的范围方法,vba,excel,Vba,Excel,我一直在努力在VBA中为一个范围内的交替行着色。问题是Range方法似乎没有选择合适的范围,我最终给相邻的单元格着色。这是我想要的输出: 但是,这是我实际得到的: 这是我创建的代码: Sub limpar_aniversariantes() Worksheets("Aniversariantes").Range("B4:D900").ClearContents 'Worksheets("Aniversariantes").Range("B4:D900").Interior.Col

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

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

这是我创建的代码:

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,因此非常感谢您的帮助。我整个上午都在努力做这件事

编辑:我注意到从人员的数据表中选择一行可以使Range.Interior.Color功能正常工作,问题在范围内。

好的

感谢@Thomas Inzina的评论,我意识到公式=MODROW,20应该翻译成我的母语excel语言,即葡萄牙语。那么正确的翻译是=MODLIN;20但是,我仍然不明白为什么我会出现我在问题中表现出的奇怪行为,相反,我用这个解决方案和正确的公式来解决我的问题:


我对此很满意,因为我得到了我想要的,但如果有人发现我使用旧方法产生这种奇怪行为的原因,为了学习,我很乐意测试建议的解决方案。

在运行gerar_lista_aniversariantes之前,您是否重置颜色?我看到在limpar_aniversariantes中重置颜色的第二行被注释了。您应该使用条件格式来创建带状行:。或者,您可以将列表转换为一个表。您是对的@JohnyL,我评论说,因为这个命令给我带来了一个问题,因为每一行都变白了。但是为了测试,我正在手动重置颜色。