【问题标题】:Inputbox Count rows and highlight by color输入框计数行并按颜色突出显示
【发布时间】:2021-12-20 11:46:51
【问题描述】:

我需要用数字计算近 2000 行。

从头开始,从另一个工作簿添加更多数据,我必须应用排序、格式化、过滤和打印等等。

我需要一个代码来执行以下操作:

enter image description here

在 A 列中从第 1 行开始直到第 145 行 将格式颜色应用于每 x 行 = 查看图像。所以 14 行是黄色的,接下来的 14 行是蓝色的,以此类推。 (数字会一直变化)

InputBox 选择 x 行并着色,直到我们用上图为数字设置颜色。应用 sheet1 直到工作表上升...Sheet2...

下面的代码需要与您发布的代码一起使用。

Private Sub Worksheet_Activate()
Dim i, lastrow, code, rw As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
code = InputBox("Enter numbers of rows to color ", "Rows")
i = Range("A:A").Select

End Sub

enter image description here

【问题讨论】:

  • 那么你有什么尝试?似乎您实际上并没有对您想要在您的问题中做的事情进行任何尝试。
  • 感谢 Raymond Wu 的重播。我只是从代码开始,我尝试过多次更改代码,但我不是真正的专家,我真的很感激请帮我解决这个问题。
  • 这不是和你的previous question and an excellent answer was given相似吗?你为什么不从那个答案修改? @GoalExcel
  • 我更喜欢使用不同的代码。此代码需要根据组定义行数,请参见上图。
  • 我认为你的形象在解释任何事情时都不是很清楚。同一个号码=同一个组?您是提出问题的人,所以如果您清楚详细地解释事情,这将您的受益。

标签: excel vba


【解决方案1】:

由于您没有指定哪一列是您的输入,因此每个工作表中都有组 - 您必须采用以下代码并进行相应调整:

Option Explicit

Sub ColorByGroup()
    Const groupCol As Long = 5 'Column E
    Const groupStartRow As Long = 1
        
    Const inputCol As Long = 1 'Column A
    Const inputStartRow As Long = 5
    
    Dim colorArr As Variant
    'array of color to be used for highlight, add/remove as desired
    colorArr = Array(RGB(255, 230, 153), _
                    RGB(146, 208, 80), _
                    RGB(155, 194, 230), _
                    RGB(244, 176, 132))
    
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change accordingly
    
    '=== Get an array of the group values (excluding the last row as assumes that is the sum and is not part of the group)
    Dim groupLastRow As Long
    groupLastRow = ws.Cells(ws.Rows.Count, groupCol).End(xlUp).Row
    
    Dim groupArr As Variant
    groupArr = ws.Range(ws.Cells(groupStartRow, groupCol), ws.Cells(groupLastRow - 1, groupCol)).Value
        
    Dim inputRow As Long
    inputRow = inputStartRow
    
    Dim colorIndex As Long
    Dim i As Long
    
    For i = 1 To UBound(groupArr, 1)
        'Get the color in array based on i
        colorIndex = i Mod (UBound(colorArr) + 1)
        
        ws.Cells(inputRow, inputCol).Resize(groupArr(i, 1)).Interior.Color = colorArr(colorIndex)
                
        inputRow = inputRow + groupArr(i, 1)
    Next i

    Erase groupArr
    Erase colorArr
    Set ws = Nothing
End Sub

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2020-11-30
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多