Excel 复制包含文本的单元格
数据从web表单传输到Excel。并非每个单元都接收输入。有许多单元格,扫描每个单元格查找文本非常耗时 如何将文本从sheet1自动复制到sheet2。但我不希望单元格显示在与原始图纸相同的布局中。我希望将它们分组在一起,消除中间的所有空单元格。我还想从包含文本的行中获取标题 我发现这个宏:Excel 复制包含文本的单元格,excel,vba,copy,cells,Excel,Vba,Copy,Cells,数据从web表单传输到Excel。并非每个单元都接收输入。有许多单元格,扫描每个单元格查找文本非常耗时 如何将文本从sheet1自动复制到sheet2。但我不希望单元格显示在与原始图纸相同的布局中。我希望将它们分组在一起,消除中间的所有空单元格。我还想从包含文本的行中获取标题 我发现这个宏: Sub CopyC() Dim SrchRng As Range, cel As Range Set SrchRng = Range("C1:C10") For Each cel In Srch
Sub CopyC()
Dim SrchRng As Range, cel As Range
Set SrchRng = Range("C1:C10")
For Each cel In SrchRng
If cel.Value <> "" Then
cel.Offset(2, 1).Value = cel.Value
End If
Next cel
Sub-CopyC()
尺寸SrchRng As范围,cel As范围
设置SrchRng=范围(“C1:C10”)
对于SrchRng中的每个cel
如果单元格值为“”,则
单元偏移量(2,1)。值=单元值
如果结束
下一个细胞
它只抓取包含文本的单元格,但以与找到文本时完全相同的布局显示文本。任何帮助都将不胜感激,并在将来为我节省大量扫描时间,提前谢谢:)我想这就是您要找的:
Sub CopyNonBlankCells()
Dim cel As Range, myRange As Range, CopyRange As Range
Set myRange = Sheet1.Range("C1:C20") '---> give your range here
For Each cel In myRange
If Not IsEmpty(cel) Then
If CopyRange Is Nothing Then
Set CopyRange = cel
Else
Set CopyRange = Union(CopyRange, cel)
End If
End If
Next cel
CopyRange.Copy Sheet2.Range("C1") '---> enter desired range to paste copied range without blank cells
End Sub
上述代码会将Sheet1中的范围C1:C20
复制到Sheet2中的C1
这是我从你那儿弄来的
编辑:以下答案基于您的评论
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu强
如果你写下面这样的话
Set myRange = Sheet1.Range("G:G")
Set myRange = Sheet2.Range("G:G")
myRange
将首先设置为Sheet1.Range(“G:G”)
,然后设置为Sheet2.Range(“G:G”)
,这意味着myRange
将具有的当前范围是Sheet2.Range(“G:G”)
如果要使用多个范围,可以使用UNION
函数,但使用UNION有一个限制,即可以组合不同的范围,但只能组合一张图纸。您的要求是将不同板材的范围组合在一起。为此,我添加了一个新的工作表,并添加了所有工作表的G:G
范围。然后在使用新添加的工作表后,我将其删除
以下代码将在名为Result
的工作表中为您提供所需的输出
Sub CopyNonBlankCells()
Dim cel As Range, myRange As Range, CopyRange As Range
Dim wsCount As Integer, i As Integer
Dim lastRow As Long, lastRowTemp As Long
Dim tempSheet As Worksheet
wsCount = Worksheets.Count '--->wsCount will give the number of Sheets in your workbook
Set tempSheet = Worksheets.Add '--->new sheet added
tempSheet.Move After:=Worksheets(wsCount + 1)
For i = 1 To wsCount
If Sheets(i).Name <> "Result" Then '---> not considering sheet "Result" for taking data
lastRow = Sheets(i).Cells(Rows.Count, "G").End(xlUp).Row '--->will give last row in sheet
lastRowTemp = tempSheet.Cells(Rows.Count, "G").End(xlUp).Row '--->will give last row in newly added sheet
Sheets(i).Range("G1:G" & lastRow).Copy _
tempSheet.Range("G" & lastRowTemp + 1).End(xlUp)(2)
End If
Next i
lastRowTemp = tempSheet.Cells(Rows.Count, "G").End(xlUp).Row
Set myRange = tempSheet.Range("G1:G" & lastRowTemp) '--->setting range for removing blanks cells
For Each cel In myRange
If Not IsEmpty(cel) Then
If CopyRange Is Nothing Then
Set CopyRange = cel
Else
Set CopyRange = Union(CopyRange, cel)
End If
End If
Next cel
CopyRange.Copy Sheets("Result").Range("G1") '---> enter desired range to paste copied range without blank cells
Application.DisplayAlerts = False
tempSheet.Delete '--->deleting added sheet
Application.DisplayAlerts = True
End Sub
子副本非空白单元格()
Dim cel作为范围,myRange作为范围,CopyRange作为范围
Dim wsCount为整数,i为整数
将lastRow变长,将lastRowTemp变长
将临时工作表设置为工作表
wsCount=工作表。Count'-->wsCount将给出工作簿中的工作表数
设置临时工作表=工作表。添加“--->添加新工作表
tempSheet.Move After:=工作表(wsCount+1)
对于i=1到wsCount
如果表(i).名称为“结果”,则'-->不考虑获取数据的表“结果”
lastRow=Sheets(i)。单元格(Rows.Count,“G”)。End(xlUp)。Row'-->将给出工作表中的最后一行
lastRowTemp=tempSheet.Cells(Rows.Count,“G”).End(xlUp)。Row'-->将给出新添加的工作表中的最后一行
图纸(i).范围(“G1:G”和lastRow).复制_
温度表范围(“G”和lastRowTemp+1)。结束(xlUp)(2)
如果结束
接下来我
lastRowTemp=tempSheet.Cells(Rows.Count,“G”).End(xlUp).Row
Set-myRange=tempSheet.Range(“G1:G”&lastRowTemp)”->删除空白单元格的设置范围
对于myRange中的每个cel
如果不是空的(cel),那么
如果CopyRange为空,则
设置CopyRange=cel
其他的
设置CopyRange=Union(CopyRange,cel)
如果结束
如果结束
下一个细胞
CopyRange.Copy Sheets(“Result”).Range(“G1”)”-->输入所需的范围以粘贴复制的范围(不带空白单元格)
Application.DisplayAlerts=False
tempSheet.Delete'-->删除添加的工作表
Application.DisplayAlerts=True
端接头
我想这就是你想要的:
Sub CopyNonBlankCells()
Dim cel As Range, myRange As Range, CopyRange As Range
Set myRange = Sheet1.Range("C1:C20") '---> give your range here
For Each cel In myRange
If Not IsEmpty(cel) Then
If CopyRange Is Nothing Then
Set CopyRange = cel
Else
Set CopyRange = Union(CopyRange, cel)
End If
End If
Next cel
CopyRange.Copy Sheet2.Range("C1") '---> enter desired range to paste copied range without blank cells
End Sub
上述代码会将Sheet1中的范围C1:C20
复制到Sheet2中的C1
这是我从你那儿弄来的
编辑:以下答案基于您的评论
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu强
如果你写下面这样的话
Set myRange = Sheet1.Range("G:G")
Set myRange = Sheet2.Range("G:G")
myRange
将首先设置为Sheet1.Range(“G:G”)
,然后设置为Sheet2.Range(“G:G”)
,这意味着myRange
将具有的当前范围是Sheet2.Range(“G:G”)
如果要使用多个范围,可以使用UNION
函数,但使用UNION有一个限制,即可以组合不同的范围,但只能组合一张图纸。您的要求是将不同板材的范围组合在一起。为此,我添加了一个新的工作表,并添加了所有工作表的G:G
范围。然后在使用新添加的工作表后,我将其删除
以下代码将在名为Result
的工作表中为您提供所需的输出
Sub CopyNonBlankCells()
Dim cel As Range, myRange As Range, CopyRange As Range
Dim wsCount As Integer, i As Integer
Dim lastRow As Long, lastRowTemp As Long
Dim tempSheet As Worksheet
wsCount = Worksheets.Count '--->wsCount will give the number of Sheets in your workbook
Set tempSheet = Worksheets.Add '--->new sheet added
tempSheet.Move After:=Worksheets(wsCount + 1)
For i = 1 To wsCount
If Sheets(i).Name <> "Result" Then '---> not considering sheet "Result" for taking data
lastRow = Sheets(i).Cells(Rows.Count, "G").End(xlUp).Row '--->will give last row in sheet
lastRowTemp = tempSheet.Cells(Rows.Count, "G").End(xlUp).Row '--->will give last row in newly added sheet
Sheets(i).Range("G1:G" & lastRow).Copy _
tempSheet.Range("G" & lastRowTemp + 1).End(xlUp)(2)
End If
Next i
lastRowTemp = tempSheet.Cells(Rows.Count, "G").End(xlUp).Row
Set myRange = tempSheet.Range("G1:G" & lastRowTemp) '--->setting range for removing blanks cells
For Each cel In myRange
If Not IsEmpty(cel) Then
If CopyRange Is Nothing Then
Set CopyRange = cel
Else
Set CopyRange = Union(CopyRange, cel)
End If
End If
Next cel
CopyRange.Copy Sheets("Result").Range("G1") '---> enter desired range to paste copied range without blank cells
Application.DisplayAlerts = False
tempSheet.Delete '--->deleting added sheet
Application.DisplayAlerts = True
End Sub
子副本非空白单元格()
Dim cel作为范围,myRange作为范围,CopyRange作为范围
Dim wsCount为整数,i为整数
将lastRow变长,将lastRowTemp变长
将临时工作表设置为工作表
wsCount=工作表。Count'-->wsCount将给出工作簿中的工作表数
设置临时工作表=工作表。添加“--->添加新工作表
tempSheet.Move After:=工作表(wsCount+1)
对于i=1到wsCount
如果表(i).名称为“结果”,则'-->不考虑获取数据的表“结果”
lastRow=Sheets(i)。单元格(Rows.Count,“G”)。End(xlUp)。Row'-->将给出工作表中的最后一行
lastRowTemp=tempSheet.Cells(Rows.Count,“G”).End(xlUp)。Row'-->将给出新添加的工作表中的最后一行
图纸(i).范围(“G1:G”和lastRow).复制_
温度表范围(“G”和lastRowTemp+1)。结束(xlUp)(2)
如果结束
接下来我
lastRowTemp=tempSheet.Cells(Rows.Count,“G”).End(xlUp).Row
Set-myRange=tempSheet.Range(“G1:G”&lastRowTemp)”->删除空白单元格的设置范围
对于myRange中的每个cel
如果不是空的(cel),那么
如果是CopyRan