Excel - 创建更复杂表格的简化“视图”(宏?)

[英]Excel - Creating a simplified “view” of a more complex table (macro?)


Introduction: Sheet1 in my excel workbook contains a "complex" table with about 130 columns and 2,5k rows.

简介:我的excel工作簿中的Sheet1包含一个“复杂”表,其中包含大约130列和2,5k行。

First issue: Based on this table i want to create a simplified table("View") in Sheet2, which contains only e.g. columns A,C,F,G,I from the table in Sheet1.

第一个问题:基于此表,我想在Sheet2中创建一个简化表(“View”),它仅包含例如Sheet1中的表中的列A,C,F,G,I。

This new "simplified" table should be updated when the table in Sheet1 is changed, e.g. in the form of a cell beeing edited, or a new row beeing inserted. Another way of approaching this would be to auto generate the table in Sheet2 everytime the user saves the workbook, or something similar.

当Sheet1中的表格被更改时,应该更新这个新的“简化”表格,例如以编辑的单元格的形式,或插入新的行。另一种接近这种方法的方法是每次用户保存工作簿或类似的东西时自动生成Sheet2中的表。

Second issue: The rows which contains a specific value in column F, lets say the value "XXX", should not be included in the Sheet2 table.

第二个问题:在F列中包含特定值的行,可以说值“XXX”,不应包含在Sheet2表中。

I'm not very experienced in more "advanced" excel functionality, but after researching the matter im leaning towards approaching this by creating a macro. Is this assumption correct? Or does excel have any other functionality to perform this task?

我对更高级的“高级”excel功能并不是很有经验,但在研究了这个问题后,我倾向于通过创建一个宏来接近它。这个假设是否正确?或者excel是否有任何其他功能来执行此任务?

If creating a macro is the best approach, then any help would be much appreciated. I am currently playing arround with a test table with 4 rows, and copying two of these into a new worksheet. But i am having troubles with how to do this automatically, and exluding the rows with a specific value(Issue #2)

如果创建一个宏是最好的方法,那么任何帮助将非常感激。我正在玩一个带有4行测试表的arround,并将其中的两个复制到一个新的工作表中。但我遇到了如何自动执行此操作并排除具有特定值的行的问题(问题#2)

2 个解决方案

#1


5  

Try the following code: (Placed in the simple sheets module)

请尝试以下代码:(放置在简单的工作表模块中)

Private Sub Worksheet_Activate()

    Application.ScreenUpdating = False

    With ActiveSheet
        .UsedRange.ClearContents
        '// Copy from data sheet too presentation sheet
        Sheet1.UsedRange.Copy .[a1]
        '// Filter all rows with xxx
        .Range("F:F").AutoFilter Field:=1, Criteria1:="xxx", Operator:=xlAnd
        '// Delete all rows with xxx excluding the header
        .UsedRange.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        '// Remove the autofilter
        If .AutoFilterMode Then .AutoFilterMode = False
        '// Delete unwanted columns
        .Range("B:B,D:E,H:H").Delete
     End With

    Application.ScreenUpdating = True

End Sub

#2


3  

The way I do this is using some code in the on_activate method of the worksheet.

我这样做的方法是在工作表的on_activate方法中使用一些代码。

That way, as soon as the user selects that sheet, the data is refreshed.

这样,只要用户选择该工作表,数据就会刷新。

Here's pseudo code:

这是伪代码:

option explicit 'but thats just me.

Private Sub Worksheet_Activate()  <<-- event of the simplified sheet.
    Worksheets("simplesheet").Cells.ClearContents
    Worksheets("alldata").Columns("A:A").Copy
    Worksheets("simplesheet").Columns("A:A").Select
    Worksheets("simplesheet").Paste
    'etc for all the relevant rows
End Sub

Second issue: The rows which contains a specific value in column F, lets say the value "XXX", should not be included in the Sheet2 table.

第二个问题:在F列中包含特定值的行,可以说值“XXX”,不应包含在Sheet2表中。

Add the following to the event

将以下内容添加到事件中

    dim cel as range

    'for each goes in the wrong direction, we need to start down and go up.
    '1-select the REAL last cell in a range.
    Worksheets("simplesheet").Range("F65536").End(xlup).Select 'edit for Excel 2007
    Set cel = selection
    while cel.row > 0 
      if cel.value = "xxxx" then cel.entirerow.delete
      set cel = cel.offset(-1,0)
    wend
    Worksheets("simplesheet").Range("a1").select

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/09/01/295916b1df85f0c13505f6864522857d.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号