【问题标题】:How to copy and paste only filtered cells in excel using vba如何使用vba在excel中仅复制和粘贴过滤的单元格
【发布时间】:2020-01-10 12:03:39
【问题描述】:

我一直在尝试使用 vba 将一系列过滤后的单元格复制并粘贴到我的 Excel 工作表中的特定空间中(查看图像),但是当我尝试这样做时,

错误 1004

发生。我在很多论坛中搜索并尝试以不同的方式解决我的问题,但它不起作用,并且仍然出现错误 1004。

Sub arrumando_dados_pro_xml()
Dim n As Integer
Dim i As Integer
Dim m As Integer
Dim j As Integer



n = Cells(1000, 1).End(xlUp).Row



j = Cells(n - 1, 1).End(xlUp).Row


m = Cells(1, 50).End(xlLeft).Row




 Range(Worksheets("Planilha1").Cells(2, 1), Worksheets("Planilha1").Cells(j, m)).SpecialCells(xlCellTypeVisible).Copy
 '''Range("A2:P37").SpecialCells(xlCellTypeVisible).Select
 ''''Selection.SpecialCells(xlCellTypeVisible).Select
 '''Selection.SpecialCells(xlCellTypeVisible).Copy
 ''''Call Plan1.AutoFilter.Range.Copy

 Range(Worksheets("Planilha2").Cells(1, 1), Worksheets("Planilha2").Cells(1, m)).Paste

 Range(Worksheets("Planilha2").Cells(1, 1), Worksheets("Planilha2").Cells(1, m)).Copy

 Range(Worksheets("Planilha1").Cells(n, 1), Worksheets("Planilha2").Cells(n, m)).Copy

''' Range(Cells(n, 1), Cells(n, m)).Select
''' ActiveSheet.Paste



End Sub

【问题讨论】:

  • 你得到的错误来自m = Cells(1, 50).End(xlLeft).Row你不能使用xlLeft.Row它必须是xlUp或xlDown。看起来您正在尝试使用 m 来识别其余代码中的最后一列。您是否尝试使用 .End(xlToLeft).Column 设置最后一个 coumn?请澄清
  • 此外,如果 n 行上方的 A 列中没有空单元格,您的 j 将等于 1。如果您提供数据示例

标签: excel vba excel-formula


【解决方案1】:

由于您的代码有点混乱,我对其进行了简化。这是一个使用 cmets 的基本代码示例,用于复制范围内的可见单元格并粘贴。可以根据需要进行修改。

'Declare your variables
Dim ws1 As Worksheet, ws2 As Worksheet, As Range, lRow As Long, lCol As Long

'Assign your variables, you should always identify the workbook and worksheet
'ThisWorkbook refers to the workbook where your code resides
Set ws1 = ThisWorkbook.Sheets("Planilha1")
Set ws2 = ThisWorkbook.Sheets("Planilha2")

'Using your worksheet variable find the last used row and last used column
lRow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
lCol = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column

'Define your range by resizing using lRow and lCol.
Set rng = ws1.Cells(2, 1).Resize(lRow - 1, lCol)

    'Copy the visible cells in the range(normally used after filtering or with hidden rows/columns)
    rng.SpecialCells(xlCellTypeVisible).Copy
    'paste the copied range starting on row 1, after the last column with data, by using .Offset(, 1)
    ws2.Cells(1, 1).PasteSpecial xlPasteValues

如果您有任何问题,请提出,我会提供帮助。

Edited 我修改了你的代码,不得不做出改变,见 cmets

'Added worksheet variables
Dim ws1 As Worksheet, ws2 As Worksheet, n As Long, m As Long 'removed j As Long

Set ws1 = ThisWorkbook.Sheets("Planilha1")
Set ws2 = ThisWorkbook.Sheets("Planilha2")

n = ws1.Cells(1000, 1).End(xlUp).Row
'Removed [j = ws1.Cells(n - 1, 1).End(xlUp).Row] if there are no blank cells after "n" the new last used row then j = 1
m = ws1.Cells(1, 50).End(xlToLeft).Column 'you can't use .End(xlLeft).Row to get the last column

'changed j to n, if j = 1 then only the top two rows will be copied
ws1.Range(ws1.Cells(2, 1), ws1.Cells(n, m)).SpecialCells(xlCellTypeVisible).Copy

'when pasting, just use one cell
ws2.Cells(1, 1).PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False 'Exits the CutCopyMode, removes "Marching Ants"

【讨论】:

  • '''复制范围 Range(Worksheets("Planilha1").Cells(2, 1), Worksheets("Planilha1").Cells(j, m)).SpecialCells(xlCellTypeVisible)。复制 ''' 粘贴到我想要的位置 Cells(n, m).Offset(, 1).PasteSpecialxlPasteValues
  • 这就是我想要做的
  • @JooVictorCarvalhoDeOliveir 我在下面编辑并添加了您的代码,并进行了修改和 cmets,我不会像您那样设置范围,因为从开始单元格调整到最后一行和最后一行更容易列
  • 谢谢,我可以解决我的问题,真正的问题在于命令 lCol = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column 我使用行而不是列那是错误谢谢伙计!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多