[英]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 子波纹管执行以下操作
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”)
Worksheets("Core_Cutter_List") 工作表( “Core_Cutter_List”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.