热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Excel根据条件复制列-Excelcopycolumnsbasedonacondition

HiIhaveaspreadsheetwhichIneedtoextractsomedataandcopyittoanewsheet.嗨,我有一个电子表格,我需

Hi I have a spreadsheet which I need to extract some data and copy it to a new sheet.

嗨,我有一个电子表格,我需要提取一些数据并将其复制到新表。

Below is an image of the spreadsheet. What I want is a copy of all the yellow highlighted columns in a new sheet when column M & Column N contain numbers (in this case there are only 2 that will be on the second sheet- row 7 & row 26). This sheet is quite big so this is just a sample.

下面是电子表格的图像。当列M和列N包含数字时,我想要的是新表中所有黄色突出显示列的副本(在这种情况下,只有2个将在第二个表 - 第7行和第26行)。这张表非常大,所以这只是一个样本。

enter image description here

Here is the code I have tried to use:

    Sub CopyYesAdrian()
Dim a As Variant, o As Variant
Dim i As Long, j As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")
  a = .Cells(1, 1).CurrentRegion
  n = Application.CountIf(.Columns(9), "Yes")
  ReDim o(1 To n + 1, 1 To 11)

End With
j = j + 1: o(j, 1) = "CORP ID": o(j, 2) = "Application ID"
o(j, 3) = "Orig - Country": o(j, 4) = "Network DDI": o(j, 5) = "PTT ACCESS": 
  o(j, 6) = "TERM - COUNTRY": o(j, 7) = "TERM SW/TRUNK"
: o(j, 8) = "TERM DIGITS": o(j, 9) = "PSTN ROUTE": o(j, 10) = "PLAN": o(j,  
11) = "DNIS"
For i = 2 To UBound(a, 1)
If IsError(a(i, 6)) Then

 ElseIf a(i, 8) = "Yes" And a(i, 6) Like "[0-9][0-9][0-9][/][0-9]*" Then
    j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(i, 3)
    o(j, 3) = a(i, 4): o(j, 4) = a(i, 6): o(j, 5) = a(i, 8):  o(j, 6) = a(i,   
9):  o(j, 7) = a(i, 10):  o(j, 8) = a(i, 11)
:   o(j, 9) = a(i, 12):   o(j, 10) = a(i, 12):   o(j, 11) = a(i, 12)
  End If
Next i
With Sheets("Sheet2")
  .UsedRange.Clear
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

2 个解决方案

#1


0  

Replace your multi-statement lines with this:

用以下内容替换多语句行:

j = j + 1
o(j, 1) = "CORP ID"
o(j, 2) = "Application ID"
o(j, 3) = "Orig - Country"
o(j, 4) = "Network DDI"
o(j, 5) = "PTT ACCESS"
o(j, 6) = "TERM - COUNTRY"
o(j, 7) = "TERM SW/TRUNK"
o(j, 8) = "TERM DIGITS"
o(j, 9) = "PSTN ROUTE"
o(j, 10) = "PLAN"
o(j, 11) = "DNIS"
For i = 2 To UBound(a, 1)
If IsError(a(i, 6)) Then

 ElseIf a(i, 8) = "Yes" And a(i, 6) Like "[0-9][0-9][0-9][/][0-9]*" Then
    j = j + 1
    o(j, 1) = a(i, 1)
    o(j, 2) = a(i, 3)
    o(j, 3) = a(i, 4)
    o(j, 4) = a(i, 6)
    o(j, 5) = a(i, 8)
    o(j, 6) = a(i, 9)
    o(j, 7) = a(i, 10)
    o(j, 8) = a(i, 11)
    o(j, 9) = a(i, 12)
    o(j, 10) = a(i, 12)
    o(j, 11) = a(i, 12)
End If

Please try this and let me know if it works.

请尝试这个,让我知道它是否有效。

#2


0  

This can be accomplished without VBA (to an extent).

这可以在没有VBA的情况下完成(在某种程度上)。

  • Type the following formula into Sheet 2, cell A2: =IF(AND(ISNUMBER(Sheet1!$M2),ISNUMBER(Sheet1!$N2)),Sheet1!I2,"")

    在表2中键入以下公式,单元格A2:= IF(AND(ISNUMBER(Sheet1!$ M2),ISNUMBER(Sheet1!$ N2)),Sheet1!I2,“”)

  • Copy/Paste this formula into cells B2:H2.

    将此公式复制/粘贴到单元格B2:H2中。

  • Delete column C (it refers to column K in Sheet1, you don't want that).
  • 删除列C(它指的是Sheet1中的列K,您不希望这样)。

  • Select the entire row of formulas and copy down as many rows as are in Sheet 1.
  • 选择整行公式并向下复制Sheet 1中的行数。

This will get you as far as you've asked in the question, but I'm assuming you'll want to sort out the blank rows from there. In that case, you may want to make Sheet2 into a table so that you can filter out the blank rows.

这会让你尽可能地在问题中提到,但我假设你要从那里整理出空行。在这种情况下,您可能希望将Sheet2放入表中,以便过滤掉空行。


推荐阅读
author-avatar
赵庭洪
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有