如何使用偏移量将单个列中的单元格值“重定位”为单个行?
我是一个坏VBA的人。请帮帮我。如何使用偏移量将单个列中的单元格值“重定位”为单个行?
我想在一列中重新定位三个值,并使用偏移量将它们放在一行中。我需要将3行数据拼合成单行数据。
这里是代码 - 这是非常粗糙:
Sub Macro1()
'
' Macro1 Macro
'
'turn off display update
Application.ScreenUpdating = False
Dim CVFESUMMARY2(2000, 2000)
Dim MAXROW As Integer
Dim i As Integer
Dim r As Range
Dim x As Range
Dim y As Range
Dim z As Range
Set r = Range("BJ13:BJ512")
Set x = Range("BK13:BK512")
Set y = Range("BL13:BL512")
Set z = Range("BM13:BM512")
MAXROW = 300
'format "new" columns
Range("BK11").Select
ActiveCell.FormulaR1C1 = "NORM"
Range("BL11").Select
ActiveCell.FormulaR1C1 = "MIN"
Range("BM11").Select
ActiveCell.FormulaR1C1 = "MAX"
Columns("BJ:BM").Select
Selection.ColumnWidth = 12
'define the "COPY DATA FROM" starting cell location
Sheets("CVFESUMMARY2").Select
Range("BJ13").Select
'cycle through all of the rows in range r
For i = 1 To MAXROW
'copy "BJ13"
r.Select
Selection.Copy
'paste "value only" in column "BK13"
x.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'copy "BJ13+1"
Set r = r.Offset(1, 0)
r.Select
Selection.Copy
'paste "value only" in column "BL13"
y.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'copy "BJ13+2"
Set r = r.Offset(1, 0)
r.Select
Selection.Copy
'paste "value only" in column "BM13"
z.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'move active cell to "BJ13+4"
Set r = r.Offset(2, 0)
Set x = x.Offset(4, 0)
Set y = y.Offset(4, 0)
Set z = z.Offset(4, 0)
Next i
'turn on display update
Application.ScreenUpdating = True
End Sub
这个有点工作,但它是在排+2和+3添加值,我不希望;我认为循环是错误的。提前致谢!
以前
后
所需输出,可以将结果进行压缩? (删除所有空行,留下一块数据),还是在与之链接的列中有信息?
删除多余的行不会太多额外的工作。
使用下面的代码(我认为这是你想要的)MaxRows
值是不正确的。它的工作方式应该是MaxRecords
即:数据组的数量。
Sub Transpose()
Dim Position As Range
Dim Source As Range
Dim MaxRow As Integer
Dim Index As Integer
' set column titles
Range("BK11").Value2 = "NORM"
Range("BL11").Value2 = "MIN"
Range("BM11").Value2 = "MAX"
' set the width
Range("BJ:BM").ColumnWidth = 12
MaxRow = 512 ' see note below
Set Position = Range("BJ13") ' define the start position
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'For Index = 1 To MaxRow
Do
' create a range that contains your first 3 values
Set Source = Range(Position, Position.Offset(RowOffset:=2))
' copy it
Source.Copy
' paste and transpose the values into the offset position
Position.Offset(ColumnOffset:=1).PasteSpecial xlPasteValues, SkipBlanks:=False, Transpose:=True
' OPTIONAL - Clear the contents of your source range
Source.ClearContents
' re-set the position ready for the next iteration
Set Position = Position.Offset(RowOffset:=4)
'Next
Loop While Position.Row < RowMax
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
注:我没有用Select
和Selection
因为他们混淆了我!使用Range()
可以更容易地知道你在哪里。
更新我已经包括一个还压缩输出
Sub TransposeCompact()
Dim Position As Range
Dim Source As Range
Dim Destination As Range
Dim MaxRow As Integer
Dim Index As Integer
' set column titles
Range("BK11").Value2 = "NORM"
Range("BL11").Value2 = "MIN"
Range("BM11").Value2 = "MAX"
' set the width
Range("BJ:BM").ColumnWidth = 12
MaxRow = 512 ' see note below
' define the start position
Set Position = Range("BJ13")
' define the first output position
Set Destination = Position.Offset(ColumnOffset:=1)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'For Index = 1 To MaxRow
Do
' create a range that contains your first 3 values
Set Source = Range(Position, Position.Offset(RowOffset:=2))
' copy it
Source.Copy
' paste and transpose the values into the offset position
Destination.PasteSpecial xlPasteValues, SkipBlanks:=False, Transpose:=True
' OPTIONAL - Clear the contents of your source range
Source.ClearContents
' re-set the position ready for the next iteration
Set Position = Position.Offset(RowOffset:=4)
' increment the row on the output for the next iteration
Set Destination = Destination.Offset(RowOffset:=1)
'Next
Loop While Position.Row < RowMax
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
更新2 您在使用For Loop
变量i
没有实际使用,如果你的数据在行13至512那么我对上面的代码所做的编辑应该会有所帮助。
RowMax
变量现在将在Position.Row
超出它时停止宏。
谢谢尼克!我会给它一个旋转。你会告诉我如何删除“未使用”的行吗?这个例程不需要新的“组合”行下的2行。再次感谢!很高兴有人会帮助你。 – 2013-03-19 18:03:44
第二个示例'TransposeCompact'不需要删除行,因为它将输出放在特定行上。如果你想在每行之间留一行空白,你需要修改'Do Loop'最后一行的'RowOffset'。 (更改1到2) – NickSlash 2013-03-19 18:12:11
太棒了!再次感谢,尼克。 – 2013-03-19 18:20:53
你的问题有点令人困惑(阅读之后,代码;但这可能只是我!)。你能添加一些截图来显示你想要做什么之前和之后? – NickSlash 2013-03-19 14:26:51
请使用http://imageshack.us/或任何类似的网站上传截图,然后编辑您的问题并添加链接到您的屏幕截图。我认为你需要'10'分将图像直接上传到'stackoverflow'问题。 – Saju 2013-03-19 15:29:21
你可以,如果你想(我在nickslash.co.uk),但理想情况下,你可以上传他们的图像到imgur.com(或类似),并编辑您的文章,包括链接,以便每个人都可以看到他们。 – NickSlash 2013-03-19 15:30:55