简体   繁体   English

复制单元格,如果它包含文本

[英]copy cell if it contains text

Data is transferred from a web-form to Excel. 数据从Web表单传输到Excel。 Not every cell receives inputs. 并非每个单元都接收输入。 There are many cells, it is time consuming to scan each cell looking for text. 有许多单元格,扫描每个单元格寻找文本是耗时的。

How do I get the text automatically copied from sheet1 to sheet2. 如何将文本自动从sheet1复制到sheet2。 But I don't want the cells displayed in the same layout as the original sheet. 但我不希望单元格显示与原始图纸相同的布局。 I would like them to be grouped together, eliminating all of the empty cells in between. 我希望它们能够组合在一起,消除它们之间的所有空单元。 I would also like to grab the title from the row that contains the text. 我还想从包含文本的行中获取标题。

I found this macro: 我找到了这个宏:

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

It grabs only cells that contain text, but it displays it in the exact same layout that it found it in. Any help would be appreciated and save me a lot of scan time in the future, thanks in advance :) 它只抓取包含文本的单元格,但它以与它所在的完全相同的布局显示它。任何帮助都会受到赞赏并在将来节省大量的扫描时间,在此先感谢:)

I guess this is what you are looking for: 我想这就是你要找的东西:

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

Above code will copy range C1:C20 in Sheet1 to C1 in Sheet2 上面的代码将Sheet1范围C1:C20复制到Sheet2 C1

Got this from here . 这里得到这个。


EDIT : Following answer is based on your comment ________________________________________________________________________________ 编辑 :以下答案基于您的评论________________________________________________________________________________

If you'll write something like below 如果你写下面的东西

Set myRange = Sheet1.Range("G:G") 
Set myRange = Sheet2.Range("G:G") 

myRange will be first set to Sheet1.Range("G:G") and then to Sheet2.Range("G:G") that means current range that myRange will have is Sheet2.Range("G:G") . myRange将首先设置为Sheet1.Range("G:G") ,然后设置为Sheet2.Range("G:G") ,这意味着myRange将具有的当前范围是Sheet2.Range("G:G")

If you want to use multiple ranges you can go for UNION function but there's a limitation that using UNION, you can combine different ranges but of only one sheet. 如果你想使用多个范围你可以使用UNION功能,但是使用UNION有一个限制,你可以组合不同的范围但只有一个工作表。 And your requirement is to combine ranges from different sheets. 您的要求是组合不同表格的范围。 To accomplish that I am adding a new worksheet and adding your G:G ranges from all the sheets to it. 为了实现这一点,我正在添加一个新工作表,并将所有工作表中的G:G范围添加到其中。 Then after using newly added sheet I am deleting it. 然后在使用新添加的工作表后,我将其删除。

Following code will give you the desired output in the sheet named Result . 以下代码将在名为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

You can use arrays! 你可以使用数组!

Instead of copying information from one cell to another, you can store all your information in an array first, then print the array on another sheet. 您可以先将所有信息存储在数组中,然后将数组打印在另一个工作表上,而不是将信息从一个单元格复制到另一个单元格。 You can tell the array to avoid empty cells. 您可以告诉阵列避免空单元格。 Typically, using arrays is the best way to store information. 通常,使用数组是存储信息的最佳方式。 (Often the fastest way to work with info) (通常是使用信息的最快方式)

If you are only looking at one column, you could use a one-dimensional array. 如果您只查看一列,则可以使用一维数组。 If you are looking at multiple columns, and want to print the information into the corresponding column (but different cells) in another page then you could a multi-dimensional array to store column number/anything else you wanted. 如果您正在查看多个列,并且想要将信息打印到另一个页面中的相应列(但不同的单元格)中,那么您可以使用多维数组来存储列号/您想要的任何其他内容。

From your code, it could look like this: 从您的代码中,它可能如下所示:

Sub CopyC()  
Dim SrchRng As Range, cel As Range 

'Declare your 1-d array (I don't know what you are storing)
Dim myarray() as variant 
Dim n as integer
Dim i as integer

Set SrchRng = Range("C1:C10")
'define the number of elements in the array - 1 for now, increase it as we go
n = 0
Redim myarray(0 to n)

For Each cel In SrchRng  
    If cel.Value <> "" Then
        'redim preserve stores the previous values in the array as you redimension it
        Redim Preserve myarray(0 to n)
        myarray(n) = cel.Value  
        'increase n by 1 so next time the array will be 1 larger
        n = n + 1
    End If  
Next cel

'information is now stored, print it out in a loop
'this will print it out in sheet 2 providing it is called "Sheet2"
For i = 0 to ubound(myarray)
    Sheets("Sheet2").cells(i,1).value = myarray(i)
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果单元格包含特定文本,则复制整列 - if cell contains specific text, copy whole column 如果单元格包含特定文本,则复制整行 - if cell contains specific text, copy whole row 如果同一行中的不同单元格包含文本,则将单元格复制到另一张工作表 - copy cell to another sheet if a different cell in the same row contains text 如果另一个单元格包含特定的文本字符串,则复制一个单元格 - Copy a cell if another cell contains a specific text string 如果单元格包含某些文本,则复制文本的一部分并移至新表 - if cell contains certain text copy part of text and move to new sheet 如果单元格包含特定文本,则复制整行 + 下一行 - if cell contains specific text, copy whole row + next row 当单元格包含特定文本时不要复制(忽略) - do not copy (ignore) when cell contains specific text 如果单元格包含“文本”,则拖动复制值,直到下一个包含“文本”的单元格 - Drag copy value if cell contains “text” until next cell containing “text” 如果该单元格包含特定文本,则将单元格从一张工作表复制到另一张工作表 - Copy a cell from one sheet to another sheet if that cell contains a particular text Excel VBA-搜索范围-如果单元格包含文本,则复制单元格-粘贴偏移量2,1 - Excel VBA - Search Range - If Cell Contains Text Then Copy Cell - Paste Offset 2,1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM