[英]Copying a Range of Cells using Empty Cells as Parameters -> PasteSpecial New Worksheet
My data is all in one column and grows downward. 我的数据全部集中在一列,并且向下增长。 There are only a few rows of data and then blank spaces ( the # of blank spaces varies ).
只有几行数据,然后是空格(空格的数量各不相同)。
I'm trying to select each group of data and transpose it automatically onto the next sheet in the next available row, and continue until there is no more data in the column. 我正在尝试选择每组数据并将其自动转置到下一个可用行的下一个工作表上,并继续操作直到该列中没有更多数据为止。
Please forgive my ignorance of the below, I pieced it together from many hours of googling and searching this site. 请原谅我对以下内容的无知,我通过数小时的搜索和搜索此网站将其拼凑而成。
Here is what I have so far, and it kind of works… but I think I need another integer to be defined, so I can get a range to copy, like 到目前为止,这是我的工作,并且可以正常工作……但是我认为我需要定义另一个整数,以便可以复制一个范围,例如
Sheets("Sheet1").Range(A & I “:” A & X ).Copy
Then, a similair operation to paste: 然后,进行类似操作以粘贴:
Sheets("Sheet2").End(xlUp).Row.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Complete Macro I'm working with: 我正在使用的完整宏:
Sub PadOut()
Application.ScreenUpdating = False
Dim i As Integer, j As Integer
j = 1
'loops from 1 to the last filled cell in column 1 or "A"
For i = 1 To Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).row
'checks if the cell has anything in it
If IsEmpty(Sheets("Sheet1").Range("A" & i)) = False Then
'this is where the copying and pasting happens (well basically)
Sheets("Sheet1").Range(A & i).copy
Sheets("Sheet2").End(xlUp).Row).PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
j = j + 1
End If
Next i
Application.ScreenUpdating = True
End Sub
The code would to be like this. 该代码将是这样的。
Sub PadOut()
Application.ScreenUpdating = False
Dim i As Long
Dim n As Long
n = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
'loops from 1 to the last filled cell in column 1 or "A"
For i = 1 To n
'checks if the cell has anything in it
If IsEmpty(Sheets("Sheet1").Range("A" & i)) = False Then
'this is where the copying and pasting happens (well basically)
Sheets("Sheet1").Range("A" & i).Copy Sheets("Sheet2").Range("a" & Rows.Count).End(xlUp)(2)
End If
Next i
Application.ScreenUpdating = True
End Sub
Here I define a Source range and then using the Range's SpecialCells method I break the Source into Areas. 在这里,我定义了一个源范围,然后使用范围的SpecialCells方法将源划分为区域。 Next I iterate over the Areas of the Source range and transpose them to the next empty cell on Sheet2.
接下来,我遍历Source区域的Areas并将它们转置到Sheet2上的下一个空单元格中。
Sub PadOut()
Application.ScreenUpdating = False
Dim Source As Range, Target As Range
Dim i As Long
With Sheets("Sheet1")
On Error Resume Next
Set Source = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
Set Source = Source.SpecialCells(xlCellTypeConstants)
On Error GoTo 0
End With
If Not Source Is Nothing Then
With Sheets("Sheet2")
For i = 1 To Source.Areas.Count
Source.Areas(i).Copy
Set Target = .Range("A" & Rows.Count).End(xlUp)
If Target.Value = "" Then
Target.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Else
Target.Offset(1).PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End If
Next
End With
End If
Application.ScreenUpdating = True
End Sub
Function to skip blank cells will be useful in your case: 跳过空白单元格的功能在您的情况下将很有用:
Function SkipBlanks(start As Range) As Long
Dim r, c As Long
r = start.Row
c = start.Column
'we make sure, that we won't exceed the number of rows
Do While IsEmpty(Cells(r, c)) And r < Rows.Count
r = r + 1
Loop
SkipBlanks = r
End Function
It takes cell as parameter, and looks for next non-empty cell. 它以单元格为参数,并查找下一个非空单元格。 If given cell is not empty, it will return its row, if it is empty, the function will return row of next non-empty cell.
如果给定的单元格不为空,则将返回其行;如果为空,则函数将返回下一个非空单元格的行。 Using that function, we can write the following:
使用该函数,我们可以编写以下内容:
Sub s()
Dim startingRow, i, j As Long
j = 3
i = 1
'we will through all rows
Do While i < Rows.Count
'we skip blanks
startingRow = SkipBlanks(Cells(i, 1))
i = startingRow
Do While Not IsEmpty(Cells(i, 1))
Cells(i - startingRow + 1, j).Value = Cells(i, 1).Value
i = i + 1
Loop
'we move to next column (here you can place code, which will choose next sheet to use
j = j + 1
Loop
End Sub
This subroutine takes first block of data, puts it in C column, then skips blanks until next block of data and puts it in D column, etc. Instead of going to another column, you can go as well to another sheet. 该子例程获取第一个数据块,将其放入C列,然后跳过空格,直到下一个数据块并将其放入D列,依此类推。您也可以转到另一张表,而不必转到另一列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.