我需要始终确保{C}位于C列,@Html.TextBox(item => item[i].yourObject, new { @readonly = "readonly" } )
位于D列。我使用下面的synatx扫描标题文本并确定每个标题的列位置in,但如果标题文本不符合强制位置,那么将其移动到正确位置的语法是什么?
name
答案 0 :(得分:1)
我只需移动值,如下所示:
Sub Test()
Dim colIndex As Long
With Worksheets("Sheet1")
CheckColumn .Rows(1), "Name", 3
CheckColumn .Rows(1), "Address", 4
End With
End Sub
Sub CheckColumn(rngHeaderRow As Range, colName As String, refColumnIndex As Long)
Dim columnIndex As Long
With rngHeaderRow.Parent
If GetColumnIndex(rngHeaderRow, colName, columnIndex) Then If columnIndex <> refColumnIndex Then MoveValues .Columns(columnIndex), .Columns(refColumnIndex)
End With
End Sub
Function GetColumnIndex(rngHeaderRow As Range, colName As String, columnIndex As Long) As Boolean
Dim rng As Range
Set rng = rngHeaderRow.Find(What:=colName, LookIn:=xlValues, lookat:=xlWhole)
If Not rng Is Nothing Then
columnIndex = rng.Column
GetColumnIndex = True
End If
End Function
Sub MoveValues(colToMoveFrom As Range, colToMoveTo As Range)
Dim arr As Variant
Dim maxSize As Long
ResizeColumn colToMoveFrom
ResizeColumn colToMoveTo
maxSize = WorksheetFunction.Max(colToMoveFrom.Count, colToMoveTo.Count)
With colToMoveFrom.Parent.UsedRange
arr = Application.Transpose(colToMoveFrom.Resize(maxSize))
colToMoveFrom.Resize(maxSize).Value = colToMoveTo.Resize(maxSize).Value
colToMoveTo.Resize(maxSize).Value = Application.Transpose(arr)
End With
End Sub
Sub ResizeColumn(rng As Range)
With rng.Parent
Set rng = .Range(rng.Cells(1, 1), .Cells(.Rows.Count, rng.Column).End(xlUp))
End With
End Sub
答案 1 :(得分:0)
基本上,你需要的是: -
With aCell
Sheet1.Cells(1, 3).Value = .Value
.Value = ""
End With
由于您的查找功能找到了&#34;名称&#34;在aCell中,上面的代码从aCell复制到第3列(&#34; C&#34;)并删除它之前的位置。请注意,Cell始终定义为Cells([Row],[Column])。 Column属性首选数字,但字母也可以使用。
顺便说一句,不要将列号转换为列名,因为Excel只能理解数字并将您的名字转换回数字。
Columns("AL").Column ' returns the column number of column "AL"
Columns(155).Address ' returns $EY:$EY