简体   繁体   English

如果列A包含文本,列G为空白,则将行复制到新电子表格

[英]if column A has text and column G is blank then copy row to new spreadsheet

I am trying to create a summary list for people in a downstream application to feed several of my production machines. 我正在尝试为下游应用程序中的人员创建摘要列表,以供多台生产机器使用。 Each machine is going to have their own tab to request material, and I want all of their requests to be summarized on one tab (called "Core_Cutter_List"). 每台机器都将有自己的标签来请求材料,我希望将所有请求汇总在一个标签上(称为“ Core_Cutter_List”)。

So basically I am trying to create a VBA that will copy over a row from spreadsheet "2" into the next blank line on spreadsheet "Core_Cutter_List". 因此,基本上,我试图创建一个VBA,该行将从电子表格“ 2”的一行复制到电子表格“ Core_Cutter_List”的下一个空白行。 I want it to copy if there is text in column A and column G is blank. 如果列A中有文本,列G为空白,我希望它复制。 I have limited knowledge of VBA. 我对VBA的了解有限。 The code that I found was able to only test for one of my criteria which was that column G is blank, but basically it runs through every single cell on my file. 我发现的代码只能测试我的条件之一,即G列为空,但基本上它遍历了文件中的每个单元格。 Do you know how I can add the other criteria of column A having text in it so that it doesn't look through every cell on my sheet? 您知道如何添加带有文本的A列的其他条件,以使它不会遍历工作表中的每个单元格吗? Thanks for any help! 谢谢你的帮助!

Sub Test()
'
' Test Macro
'

  Sheets("2").Select

For Each Cell In Sheets(1).Range("G:G")
    If Cell.Value = "" Then
        matchRow = Cell.Row
        Rows(matchRow & ":" & matchRow).Select
        Selection.Copy

        Sheets("Core_Cutting_List").Select
        ActiveSheet.Rows(matchRow).Select
        ActiveSheet.Paste
        Sheets("2").Select
    End If
Next
End Sub

If you need two conditions, then you should write them carefully in the IF statement with And : 如果需要两个条件,则应在IF语句中使用And小心地编写它们:

Something like If cell.Value = "" And Len(cell.Offset(0,-6)) Then should be workable. If cell.Value = "" And Len(cell.Offset(0,-6)) Then应该可行。

Using Select is a bit not advisable, but it works at the beginning - How to avoid using Select in Excel VBA 不建议使用“ Select ,但建议从一开始就可以使用 - 如何避免在Excel VBA中使用“选择”

The Sub bellow does the following 子波纹管执行以下操作

  • Determine the last used row in Worksheets("2") based on values in column A 根据列A中的值确定Worksheets(“ 2”)中最后使用的行
  • Determine the last used col in Worksheets("2") based on values in row 1 根据第1行中的值确定Worksheets(“ 2”)中最后使用的列
  • Determine the last used row in Worksheets("Core_Cutter_List") based on values in column A 根据列A中的值确定Worksheets(“ Core_Cutter_List”)中最后使用的行
  • Loop through all used rows in Worksheets("2") 遍历工作表中所有使用的行(“ 2”)
    • If the cell in col A is not empty And cell in col G is empty 如果列A中的单元格不为空且列G中的单元格为空
      • Copy entire row to next empty row in Worksheets("Core_Cutter_List") 将整行复制到工作表中的下一个空行(“ Core_Cutter_List”)
      • Increment next empty row for Worksheets("Core_Cutter_List") 增加工作表的下一个空行(“ Core_Cutter_List”)
    • Loop to the next used row in Worksheets("2") 循环到Worksheets(“ 2”)中的下一个使用的行

Option Explicit

Public Sub CopyRows()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws1r As Range, ws2r As Range
    Dim ws1lr As Long, ws1lc As Long, ws2lr As Long, i As Long

    Set ws1 = ThisWorkbook.Worksheets("2")
    Set ws2 = ThisWorkbook.Worksheets("Core_Cutter_List")

    ws1lr = ws1.Range("A" & Rows.Count).End(xlUp).Row        'last row in "2"
    ws1lc = ws1.Cells(1, Columns.Count).End(xlToLeft).Column 'last col in "2"
    ws2lr = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1    'last row in "Core_Cutter"

    For i = 1 To ws1lr

        If Len(ws1.Cells(i, "A")) > 0 And Len(ws1.Cells(i, "G")) = 0 Then

            Set ws1r = ws1.Range(ws1.Cells(i, 1), ws1.Cells(i, ws1lc))
            Set ws2r = ws2.Range(ws2.Cells(ws2lr, 1), ws2.Cells(ws2lr, ws1lc))

            ws2r.Value2 = ws1r.Value2
            ws2lr = ws2lr + 1
        End If

    Next i

End Sub

My test file 我的测试文件

Worksheets("2") 工作表( “2”)

工作表( “Core_Cutter_List”)

Worksheets("Core_Cutter_List") 工作表( “Core_Cutter_List”)

工作表( “Core_Cutter_List”)

暂无
暂无

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

相关问题 如果使用 vba 的 Excel 电子表格中的单元格为空白,则将一列中文本的最后 3 个字符复制到另一列 - Copy last 3 char of text in one column to another column if cell is blank in excel spreadsheet with vba 当第一列中的单元格具有特定文本时,将行复制到新工作表 - Copy a row to new sheet when cell in first column has specific text 复制列的第一个单元格并复制到每行前面的新列中,然后在空白后循环 - Copy the first cell of column and copy into a new column in front of each row and loop through after blank 当L列值与P列匹配时,VBA将行复制到新电子表格 - VBA to copy a row to new spreadsheet when column L value matches Column P Excel-将此行从工作表1复制到工作表2中的下一个可用行,如果H列具有空白卖出 - Excel - Copy this row from sheet 1 to next available line in sheet 2 if column H has blank sell 将包含指定行数据的最后一列复制到下一个空白列 - Copy last column with data on specified row to the next blank column 如果另一列有数据,则将一个单元格复制到列中的空白单元格 - Copy one cell to blank cells in a column, if another column has data 如果下一列在电子表格中有内容,则将单行扩展为多行 - Expand single row into multiple rows if next column has content in a spreadsheet 在列中搜索值并将行复制到新工作表 - Search for a value in a column and copy row to new sheet 将其下有多行数据但没有该名称的行名称复制到该行名称下的每一行的新列中 - Copy row name that has multiple rows of data under it without that name into new column for each row under that row name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM