简体   繁体   English

一旦单元格发生变化,如何让范围内的单元格进行硬编码?

[英]How can I get a cell within a range to hardcode once the cell changes?

I need to get a cell within a range to hardcode when a status is selected in another column (same row) on the worksheet.当在工作表上的另一列(同一行)中选择状态时,我需要获取范围内的单元格以进行硬编码。 The purpose is so I can measure how long a new hire candidate sits in each step (or status) of the hiring process.目的是让我可以衡量新入职候选人在招聘过程的每个步骤(或状态)中的停留时间。

I've tried writing code to get the macro to start once the workbook opens (module1), and then to loop through the specified range of cells (AC3:AQ5000) once the status in column J is updated using a worksheet_change event (sheet1).我已经尝试编写代码以在工作簿打开(module1)后启动宏,然后在使用 worksheet_change 事件(sheet1)更新列 J 中的状态后循环遍历指定的单元格范围(AC3:AQ5000) . Columns AC through AQ have a formula to date the cell once column J is updated to a status that matches each column (=IF($J5=AC$1,IF(AC5<>"",AC5,TODAY()),"")) .一旦列 J 更新为与每列匹配的状态(=IF($J5=AC$1,IF(AC5<>"",AC5,TODAY()),"")) Circular reference has been turned off.循环引用已关闭。

Private Sub Worksheet_change(ByVal Target As Range)

    Dim Target As Range
    Dim MyCell As Range
    Set Target = Sheet1.Range("ac3:aq5000")
    For Each MyCell In Target
        If MyCell.Value > "" Then
            MyCell.Copy
            MyCell.PasteSpecial Paste:=x1pasteformats
        End If
    Next MyCell

End Sub

I expect to get each column to hardcode the date once the cell changes from blank to (today's) date.一旦单元格从空白变为(今天的)日期,我希望让每一列对日期进行硬编码。 So far I receive errors for "ambiguous name detected worksheet_change", "compile error expected identifier", or nothing happens.到目前为止,我收到了“检测到的工作表更改名称不明确”、“编译错误预期标识符”的错误,或者什么也没有发生。

Failed to clearly understand the objective and following are assumed未能清楚地理解目标和以下假设

  1. Status will be updated in Column J only状态将仅在 J 列中更新
  2. If updated status match with Headers (Containing status text) of Column AC to AQ then the corresponding column and Row corresponding to Updated cell is to replaced with current date.如果更新状态与列 AC 到 AQ 的标题(包含状态文本)匹配,则更新单元格对应的相应列和行将替换为当前日期。 If I assumed it correctly then no need to loop through all the cells in Range("ac3:aq5000") instead loop all the headers (row 1) of Column AC to AQ only.如果我假设正确,则无需遍历Range("ac3:aq5000")所有单元格,而是仅将 AC 列的所有标题(第 1 行)循环到 AQ。 There is also no need to operate any macro in module 1.模块1中也不需要操作任何宏。

If the assumed output will be like below如果假设的输出如下所示

在此处输入图片说明

Then the code would be simply:那么代码将很简单:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Rng As Range, cel As Range, col As Long
Set Rng = Intersect(Range("J:J"), Target)
    If Not Rng Is Nothing Then
    Application.EnableEvents = False
        For Each cel In Rng
          For col = 29 To 43
            If cel.Value <> "" And cel.Value = Cells(1, col).Value Then
            Cells(cel.Row, col).Value = Now()
            End If
          Next
        Next cel
    Application.EnableEvents = True   
    End If
End Sub

Please try with following, and change the as per your need请尝试以下操作,并根据您的需要更改

Sub pastespecial()
Dim rangevalue, cellvalue As Range
Set rangevalue = Sheet1.Range("a1:a2")
For Each cellvalue In rangevalue
    If cellvalue > "" Then
        cellvalue.Copy
        cellvalue.pastespecial xlPasteValues
    End If
Next cellvalue


End Sub

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

相关问题 如何在单元格中获取包含换行符的单元格内容? - How can I get the cell contents including a linebreak within the cell? 如何在一个范围内获得单元格的位置? - How do I get a cell's position within a range? 如何在一个范围内的单元格中搜索项目? - How can I search for an item within a cell that's also in a range? 如何获得“选择案例以测试范围内的每个单元格? - How can I get 'Select Case to test each cell in range? 如何将单元格范围作为参数传递? - How can I pass a cell range as an argument? 当具有公式的单元格更改时,如何使用VBA以特殊方式复制特定范围? - How can I copy a certain range in a special way with VBA when a cell with a formula changes? 如何从下拉列表中计算单元格的平均值、最小值、最大值以及它在 Excel 中的变化 - How can I calculate the average, min, max, range for a cell from a dropdown and it changes in Excel 当单元格值更改时,如何向一系列行添加厚底边框? - How can I add a thick bottom border to a range of rows when the cell value changes? 如何在一个范围内的列中找到第一个空单元格,并在后续公式中使用行引用? - How can I find the first empty cell in a column within a range, and use the row-reference in subsequent formulas? 如何引用非连续命名范围内的特定单元格? - How can I refer to specific cell within non contiguous named range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM