Excel VBA从带有隐藏列的af过滤列表复制

发布于 2024-12-02 16:22:40 字数 1105 浏览 0 评论 0原文

试试这个。首先在sheet 1上建立以下数据表

A   B   C   D
a1  b1  c1  d1
a2  b2  c2  d2
a3  b3  c3  d3
a4  b4  c4  d4
a5  b5  c5  d5

创建一个命名区域“Dateset”A2:D6。 在表上放置过滤器并隐藏 B 列,然后过滤表以仅显示第一行。

现在在数据集上测试以下代码:

 Sub DataCopy()
   Dim r
   For Each r In Names("Dateset").RefersToRange.Rows
      r.Copy Worksheets(2).Range("a65536").End(xlUp).Offset(1, 0)
   Next
 End Sub

我得到以下结果:

a1  c1  d1  
a2  b2  c2  d2
a3  b3  c3  d3
a4  b4  c4  d4
a5  b5  c5  d5

没有过滤器,我得到(如预期):

a1  b1  c1  d1
a2  b2  c2  d2
a3  b3  c3  d3
a4  b4  c4  d4
a5  b5  c5  d5

你能帮助我找到一种方法来获取所有数据 - 也从可见行获取?

诗。我也尝试过:

Dim c as Range
For Each c In Range("A2", Range("A6")).Cells
    Range(c, c.End(xlToRight)).Copy Worksheets(2).Range("A65536").End(xlUp).Offset(1, 0)
    c.EntireRow.Copy Worksheets(2).Range("A65536").End(xlUp).Offset(1, 0)
    Range(c, c.Offset(0, 5)).Copy Worksheets(2).Range("A65536").End(xlUp).Offset(1, 0)
Next

但没有成功...... 我已经在 Excel 2007 和 2010 中进行了测试。

Try this. First establish the following datatable on sheet 1

A   B   C   D
a1  b1  c1  d1
a2  b2  c2  d2
a3  b3  c3  d3
a4  b4  c4  d4
a5  b5  c5  d5

Create a named area "Dateset" A2:D6.
Put a filter on the table and hide column B and filter the table to show only the first row.

Now test the following code on the dataset:

 Sub DataCopy()
   Dim r
   For Each r In Names("Dateset").RefersToRange.Rows
      r.Copy Worksheets(2).Range("a65536").End(xlUp).Offset(1, 0)
   Next
 End Sub

I get the following result:

a1  c1  d1  
a2  b2  c2  d2
a3  b3  c3  d3
a4  b4  c4  d4
a5  b5  c5  d5

Without the filter I get (as expected):

a1  b1  c1  d1
a2  b2  c2  d2
a3  b3  c3  d3
a4  b4  c4  d4
a5  b5  c5  d5

Can you help me with a way to get all the data - also from the visible row?

Ps. I have also tried:

Dim c as Range
For Each c In Range("A2", Range("A6")).Cells
    Range(c, c.End(xlToRight)).Copy Worksheets(2).Range("A65536").End(xlUp).Offset(1, 0)
    c.EntireRow.Copy Worksheets(2).Range("A65536").End(xlUp).Offset(1, 0)
    Range(c, c.Offset(0, 5)).Copy Worksheets(2).Range("A65536").End(xlUp).Offset(1, 0)
Next

but with no success...
I have tested in Excel 2007 and 2010.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰沐子 2024-12-09 16:22:40

这绝对看起来像一个错误。

由于似乎只有第一行有问题,因此您可以逐列复制该行,但按整行复制其余行:

Sub DataCopy()
    Dim r As Range
    Dim dest As Range
    For Each r In Names("DateSet").RefersToRange.Rows
        If r.Row = 2 Then
            For Each c In r.Columns
                Set dest = Worksheets(2).Cells(r.Row, c.Column)
                r.Cells(1, c.Column).Copy dest
            Next
        Else
            Set dest = Worksheets(2).Cells(r.Row, r.Column)
            r.Copy dest
        End If
    Next
End Sub

That definitely looks like a bug.

Since it's only the first row that seems to be the problem, you could copy THAT row column by column, but copy the remaining rows by entire row:

Sub DataCopy()
    Dim r As Range
    Dim dest As Range
    For Each r In Names("DateSet").RefersToRange.Rows
        If r.Row = 2 Then
            For Each c In r.Columns
                Set dest = Worksheets(2).Cells(r.Row, c.Column)
                r.Cells(1, c.Column).Copy dest
            Next
        Else
            Set dest = Worksheets(2).Cells(r.Row, r.Column)
            r.Copy dest
        End If
    Next
End Sub
薄暮涼年 2024-12-09 16:22:40

这是一个黑暗中的刺探:

 Sub DataCopy()

    Dim vArray As Variant

    vArray = Names("Dateset").RefersToRange.Value

    Worksheets(2).[A1].Resize(UBound(vArray, 1), UBound(vArray, 2)).Value = vArray

 End Sub

基本上,您不应该在这些情况下复制类似的行,即隐藏和过滤的行。

如果您想要更好的答案,您应该准确描述您想要做什么。你的例子和解释令人困惑。无论如何对我来说:)我只是不明白你真正想做什么。

Here is a stab in the dark:

 Sub DataCopy()

    Dim vArray As Variant

    vArray = Names("Dateset").RefersToRange.Value

    Worksheets(2).[A1].Resize(UBound(vArray, 1), UBound(vArray, 2)).Value = vArray

 End Sub

Basically you should not be copying rows like that in those circumstances ie hidden and filtered rows.

If you want a better answer you should describe EXACTLY what you are trying to do. Your examples and explanation are confusing. To me anyway :) I just don't understand what you are really trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文