Excel 移动列而不更改引用
我有一张Excel表格,是你从另一个程序中粘贴数据的。大约有二十列,我想找到其中的八列,并将它们一直移动到左侧,但这样做时,对它们的引用会发生变化。即使我加上美元符号 这是移动它们的代码:Excel 移动列而不更改引用,excel,vba,Excel,Vba,我有一张Excel表格,是你从另一个程序中粘贴数据的。大约有二十列,我想找到其中的八列,并将它们一直移动到左侧,但这样做时,对它们的引用会发生变化。即使我加上美元符号 这是移动它们的代码: For i = 1 To lastColumn If S.Cells(1, i) = "Status" Then S.Columns(i).Cut S.Columns(1).Insert Exit For End If Nex
For i = 1 To lastColumn
If S.Cells(1, i) = "Status" Then
S.Columns(i).Cut
S.Columns(1).Insert
Exit For
End If
Next i
For i = 1 To lastColumn
If S.Cells(1, i) = "Order" Then
S.Columns(i).Cut
S.Columns(2).Insert
Exit For
End If
Next i
For i = 1 To lastColumn
If S.Cells(1, i) = "Delivery" Then
S.Columns(i).Cut
S.Columns(3).Insert
Exit For
End If
Next i
For i = 1 To lastColumn
If S.Cells(1, i) = "Deliverymethod" Then
S.Columns(i).Cut
S.Columns(4).Insert
Exit For
End If
Next i
For i = 1 To lastColumn
If S.Cells(1, i) = "Deliverypriority" Then
S.Columns(i).Cut
S.Columns(5).Insert
Exit For
End If
Next i
For i = 1 To lastColumn
If S.Cells(1, i) = "Usertype" Then
S.Columns(i).Cut
S.Columns(6).Insert
Exit For
End If
Next i
For i = 1 To lastColumn
If S.Cells(1, i) = "Article" Then
S.Columns(i).Cut
S.Columns(7).Insert
Exit For
End If
Next i
For i = 1 To lastColumn
If S.Cells(1, i) = "Shelf" Then
S.Columns(i).Cut
S.Columns(8).Insert
Exit For
End If
Next i
在另一页中,我有几个公式引用这些列。例子:
=COUNTIF(S!$H:$H;“*”)-1
当我运行代码时,列移动到它们应该移动的位置,但公式更改为
=COUNTIF(S!$I:$I;“*”)-1
是否有一种方法可以在不更改引用的情况下移动列?如果需要初始引用,则应(在代码中)根据需要再次编写公式。。。移动列时,Excel会自动使引用适应其实际位置的移动范围。从原始列复制值,然后将这些值粘贴到目标列。清除原始列。感谢您的回答!事实上,我同意法内杜罗斯的建议。在代码中编写了公式。这是一点工作,但现在它的工作,因为它应该!