简体   繁体   English

将工作表的选中项粘贴到 VBA Excel 中另一个工作表的彩色单元格

[英]Pasting the checked items of a Sheet to the colored cells of another sheet in VBA Excel

I have a VBA Excel program of copying the cell values to the colored cells of another sheet when the checkbox present in the next cell of sheet 1 is checked.我有一个VBA ZC1D81AF5835844B4E9D936910DEDED8FDCZ程序,将单元值复制到下一个单元格1的复选框中时,将单元值复制到另一张纸的有色单元格。 Now my problem is: 1. When the checkbox of the respective row is checked it copies the value of the above cell.(eg. When 21 no"s checkbox is checked, it copies the value 20) 2. When it reaches the uncolored cell it pastes a value and it moves to the next row.现在我的问题是:1。检查各个行的复选框时,它将复制上述单元格的值。(例如,当21个“ n no”“ s”检查框时,它复制了值20)2。当它到达未颜色时,单元格它粘贴一个值并移动到下一行。

  Sub executeCheckBoxes()


    Dim src As Worksheet
    Dim tgt As Worksheet
    Dim chkbx As CheckBox
    Dim i As Long

    Set src = ThisWorkbook.Worksheets("Sheet1")
    Set tgt = ThisWorkbook.Worksheets("Sheet2")
    srcLR = src.Cells(src.Rows.Count, 1).End(xlUp).Row
    tgtER = tgt.Cells(tgt.Rows.Count, 1).Row + 1
    i = 4
    j = 1
    For Each chkbx In src.CheckBoxes
        If chkbx.Value = xlOn Then
        tgt.Cells(i, j).Value = _
            src.Cells(chkbx.TopLeftCell.Row, 1).Value
        tgt.Activate
            If tgt.Cells(i, j).Interior.ColorIndex <> 0 Then
            'tgt.Cells(i, j).Select
             Debug.Print chkbx.Name
             j = j + 1

            Else
             i = i + 1
             j = 1
            End If

         End If
      Next chkbx

End Sub

So this is my sheet 2 appearance:所以这是我的表 2 外观: 表 2

I'm having 1 to 278 numbers along with the individual checkboxes in the "sheet 1".But I wanted them to be copied from left to right and that too it should get arranged within the colored cells depending on the selected cell values.我有 1 到 278 个数字以及“表格 1”中的各个复选框。但我希望它们从左到右复制,并且它也应该根据所选单元格值排列在彩色单元格中。

This is my Sheet 1:这是我的表 1: 表 1

Any help would be really appreciated.任何帮助将非常感激。

Function Filled(MyCell As Range)
If MyCell.Interior.ColorIndex > 0 Then
    Result = 1
Else
    Result = ""
End If
Filled = Result
End Function

This code was taken from a discussion here .此代码取自此处的讨论。

Using this function, you could write a while loop that starts at cell 1,1 which then goes horizontally until it reaches a cell with no color.使用此 function,您可以编写一个while循环,该循环从单元格1,1开始,然后水平执行,直到到达没有颜色的单元格。 Then it would go to the next line.然后它将 go 到下一行。 If it went to the next line and the cell had no color, then you could exit the while loop and send a message that says "run out of cells" or something like that.如果它转到下一行并且单元格没有颜色,那么您可以退出 while 循环并发送一条消息,上面写着“单元格用完”或类似的东西。

This assumes that the colored cells are all exactly the same color.这假设彩色单元都是完全相同的颜色。 Also it assumes that there are no breaks in color in the line (ie one continuous block of color).它还假设行中没有颜色中断(即一个连续的颜色块)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM