VBA颜色填充宏从行中的一个工作表到

时间:2017-08-11 14:54:18

标签: excel vba excel-vba

Excel VBA还是一个新手。我需要有条件地从另一个从左到右读取的工作表上的单元格中复制颜色填充,并将这些颜色复制到列中的另一个工作表上。我遇到的最大问题是组织我的嵌套for循环。我们的想法是用一个用颜色表示的输入来填充7x7数组。 7x7数组本身将完全由列填充,但它会读取另一个工作表中的行。下面的代码不起作用。

Sub colorfillcopy()


  Dim i As Integer, j As Integer, k As Integer, l As Integer, m As Integer, 
  lastrow As Long


Dim Sourcews As Worksheet, Transferws As Worksheet
Dim row1 As Range, row2 As Range, row3 As Range, row4 As Range, row5 As 
Range, row6 As Range, row7 As Range
Dim rngCopy As Range
Dim rngPaste As Range




Set Sourcews = ThisWorkbook.Sheets("Source")
Set Transferws = ThisWorkbook.Sheets("Transfer")

Transferws.Range("row1:row7").Interior.Color = xlNone

lastrow = Sourcews.Cells(Sourcews.Rows.Count, "A").End(xlUp).row
lastrowgrid = Transferws.Cells(Rows.Count, 3).End(xlUp).row

For i = 2 To lastrow



         If Sourcews.Range("AA" & i).Value = " Conditionally data" And Transferws.Cells(18, 
                        Sourcews.Columns.Count).End(xlToLeft).Column Then

        For j = 1 To 7

                   For k = 1 To 7

                 Sourcews.Range("8 ,j+1").Interior.Color = 
                            Transferws.Range("P:V").Interior.Color



                Next k

         Next j

    End If



Next i

End Sub

1 个答案:

答案 0 :(得分:0)

再次,Chicken_Milk;我最近在这里帮了你一点!

我看到的第一个问题是“k”的循环,但没有看到k。你所拥有的循环应该足够了,因为你正在为i使用循环,如果是,则为循环j。我已经纠正了一些错误,例如将项目放在不同的行上(如果你想把东西转移到另一行,你需要使用下划线)。 j循环中的Range已更改为Cells,并且“”被删除(如果这是范围,它将类似于Range(“A”& j + 1),而不是逗号。

Dim i As Integer, j As Integer, k As Integer, l As Integer, m As Integer, lastrow As Long
Dim Sourcews As Worksheet, Transferws As Worksheet
Dim row1 As Range, row2 As Range, row3 As Range, row4 As Range, row5 As Range, row6 As Range, row7 As Range, rngCopy As Range,  rngPaste As Range

Set Sourcews = ThisWorkbook.Sheets("Source")
Set Transferws = ThisWorkbook.Sheets("Transfer")

Transferws.Range("row1:row7").Interior.Color = xlNone

lastrow = Sourcews.Cells(Sourcews.Rows.Count, "A").End(xlUp).row
lastrowgrid = Transferws.Cells(Rows.Count, 3).End(xlUp).row

For i = 2 To lastrow
    If Sourcews.Range("AA" & i).Value = " Conditionally data" And Transferws.Cells(18,Sourcews.Columns.Count).End(xlToLeft).Column Then
        For j = 1 To 7
            Sourcews.Cells(8 ,j+1).Interior.Color =Transferws.Range("P:V").Interior.Color
            Next j
        End If
    Next i

我会说,我对你的If语句有点困惑,对第二个标准的要求是什么(“如果rng = B和x则是”而不是“如果rng = B且x = A那么”)