Excel VBA基于值更改向左或向右移动单元格(步骤)

基本上这是一个用于规划的列表。 彩色条可根据开始date,结束date列进行手动设置。

这个计划会改变,所以彩条需要手动调整到右侧或左侧。 我可能会误导你,给我的想法,但无论如何可能有任何用处。 我想我做了一个额外的列来计算几个月的差异(这很简单)。 例如,如果我们有一个数字6这个列,计划移动到左边(重新计划),数字6将减less,所以根据这种减less,我想链接彩色条自动移动到(这就是我所说的一个步骤)。 如果截止date延长,则将彩条移到右侧。

我已经实现了一个简单的macros,popup日历以更专业的方式提取date(遗憾的是我的基本VBA技能),剩下的部分(复杂的部分)就剩下了。 🙂

我真的没有经验,只设法得到以下,根据数量的变化,但没有步骤,没有双向运动,真正复杂和担心,这是不可能的,移动单元格的权利。

Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then Application.EnableEvents = False ActiveCell.Insert Shift:=xlToRight Application.EnableEvents = True End If End Sub 

图片:

图片附在这里

正如其他人所指出的那样,在你的问题中有许多没有回答的问题,尽pipe这是一个有趣的问题。

我已经提供了下面的代码,将会给你一个很好的框架来工作。 你需要对范围引用进行修改,也许需要一些逻辑,但是我从你提供的小例子中把它们拼凑在一起,只是为了说明它是如何工作的。

 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) 'If Target.Address = "$A$1" Then 'will only fire if cell A1 is changed 'If Target.Column = 1 Then 'will only fire if any cell in column A is changed If Not Intersect(Target, Range("A1:Z10000")) Is Nothing Then 'will only fire if a cell in the A1:Z100000 is changed If Target.Cells.Count = 1 Then 'further refine to make sure only one cell changed Application.EnableEvents = False 'first lets get the difference of the old value versus the new value Dim iOld As Integer, iNew As Integer, iSize As Integer 'using byte for the assumption that the numbers will always be small iNew = Target.Value2 Application.Undo iOld = Target.Value2 Target.Value = iNew 'reset change back after getting old value If iNew - iOld > 0 Then 'check to make sure difference is positive iSize = iNew - iOld Select Case iSize ' here you can set your conditions based on the difference Case Is > 1 Target.Resize(1, iSize).Insert shift:=xlToRight Case Is = 1 With Target If .Column <> 1 Then 'because can't move anything to the left of column A .Cut .Offset(, -1).Insert shift:=xlToRight End If End With End Select End If Application.EnableEvents = True End If End If End Sub 

那么,这是我对这个问题的解释让我做的事:

 Option Explicit ' Store the current state of your cell here! ' ' The value defaults to Zero on the first run. ' If you don't like that, you should call a ' function that stores the current value in ' here first... Dim currentNum As Integer Private Worksheet_Change(ByVal Target As Range) ' Check if the changed value belonged to ' your modifier-cell. If it didn't, exit ' the sub. Otherwise continue with your ' operation. If Not Target.Address = "A1" Then Exit Sub End If ' Use a for-loop to iterate over all the cells ' within a specific range. Dim c As Integer ' Holds the current column Dim r As Integer ' Holds the current row '(inside the current column) ' Iterate over the columns... For c = 0 To <your_range>.Columns.Count ' Iterate over all rows in each column... For r = 0 To <your_range>.Rows.Count If Not IsEmpty(<your_range>.Cells(c,r)) Then ' I never know if c or r goes first. ' Make sure they're in the right order. ' Now, we're checking the current value ' of your cell against the nust updated new ' value: If GetModifierCellValue() > currentNum Then ' The GetModifierCellValue()-Function doesn't exist. I recommend you write one. ' Otherwise you could just query the value with the cell's address... If c+(ABS(currentNum-GetModifierCellValue()) <= <your_range>.Columns.Count Then ' Copy the value to the cell you'd like to shift to <your_range>.Cells(c+(ABS(currentNum-GetModifierCellValue())), r).Value =_ <your_range>.Cells(c,r).Value ' Again: maybe it has to be the other way round... ' Empty the current cell <your-range>.Cells(c,r).Value = "" End If ElseIf GetModifierCellValue() < currentNum Then If c-(ABS(currentNum-GetModifierCellValue()) >= 1 Then ' Copy the value to the cell you'd like to shift to <your_range>.Cells(c-(ABS(currentNum-GetModifierCellValue())), r).Value =_ <your_range>.Cells(c,r).Value ' Again: maybe it has to be the other way round... ' Empty the current cell <your-range>.Cells(c,r).Value = "" End If End If End If Next Next End Sub 

注意:您一定要阅读我所有的评论。 他们非常重要。 而且由于你是(VBA)编程的新手,所以在编程生涯中,他们实际上可能会帮助你很好。

编辑:

用上面的代码覆盖现有的单元格。 如果您想避免这种情况,请将更改(仅更改!!)应用于临时表,然后将此临时表复制到您的临时表中。

另外,你不能使用IsEmpty()来实现你想要的。 它没有考虑细胞的背景颜色(我相信…)! 改用单元格的背景颜色属性。