Vba 尝试将符合条件的某些数据行复制到新工作表
我是excel新手,正在尝试将符合条件(代码)的数据传输到另一个工作表 错误本身发生在线路上Vba 尝试将符合条件的某些数据行复制到新工作表,vba,excel,Vba,Excel,我是excel新手,正在尝试将符合条件(代码)的数据传输到另一个工作表 错误本身发生在线路上 a = Worksheets("ToExtract").Cells(Rows.Count, 1).End(xlUp).Row 我在哪能找到一份工作 运行时错误9 试试这个,我编辑了你的代码。 这对我来说很好: Private Sub CommandButton1_Click() 'Declare your workbook Dim wb As Workbook Set wb = ThisWorkbo
a = Worksheets("ToExtract").Cells(Rows.Count, 1).End(xlUp).Row
我在哪能找到一份工作
运行时错误9
试试这个,我编辑了你的代码。 这对我来说很好:
Private Sub CommandButton1_Click()
'Declare your workbook
Dim wb As Workbook
Set wb = ThisWorkbook
'Declare all your worksheet
Dim sh_extract, sh_tocopy As Worksheet
Set sh_extract = wb.Worksheets("Sheet1")
Set sh_tocopy = wb.Worksheets("Sheet2")
Dim a, b As Long
a = sh_extract.Range("B" & Rows.Count).End(xlUp).Row 'Change column letter if you need to check other column to have last row
For i = 2 To a
'If sh_extract.Cells(i, 2).Value = "20040155" or sh_extract.Cells(i, 2).Value = "23358308" Then
If sh_extract.Cells(i, 2).Value = "20040155" Then
'copy the row
sh_extract.Rows(i).Copy
'define the last row of 2nd sheet
b = sh_tocopy.Range("B" & Rows.Count).End(xlUp).Row + 1 'Change column letter if you need to check other column to have last row
'Copy the row
sh_tocopy.Rows(b).PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
sh_extract.Cells(1, 1).Select
End Sub
什么值是Rows.Count?将
Rows.Count
限定为一个显式工作表,它就会工作。例如:Worksheets(“ToExtract”).Cells(Worksheets(“ToExtract”).Rows.Count,1).End(xlUp).Row
a=Worksheets(“ToExtract”).Cells(Worksheets(“ToExtract”).Rows.Count,1.End(xlUp).Row。您将需要对b执行相同的操作。@dbmitch行。计数等于1048576@Cyril我尝试了这个方法,但同样的错误仍然发生在Guicelli感谢您的帮助,这对我来说似乎更有意义,但是在“Set sh_extract=wb.Worksheets(“ToExtract”)”和“Set sh_tocopy=wb.Worksheets(“Sheet2”)行中我得到了相同的运行时错误9,它说下标超出了range@JadenEllington你能查一下你床单的名字吗?如果我用随机名称更改“sheet2”,我也会遇到同样的错误.xlsx文件的名称是ToExtract.xlsx,该表名为Sheet1OK,因此在“Sheet1”上检查条件,“sheet2”粘贴行,对吗?好的,就这样。非常感谢你在这方面的帮助,因为这将花费我一生的时间。
Private Sub CommandButton1_Click()
'Declare your workbook
Dim wb As Workbook
Set wb = ThisWorkbook
'Declare all your worksheet
Dim sh_extract, sh_tocopy As Worksheet
Set sh_extract = wb.Worksheets("Sheet1")
Set sh_tocopy = wb.Worksheets("Sheet2")
Dim a, b As Long
a = sh_extract.Range("B" & Rows.Count).End(xlUp).Row 'Change column letter if you need to check other column to have last row
For i = 2 To a
'If sh_extract.Cells(i, 2).Value = "20040155" or sh_extract.Cells(i, 2).Value = "23358308" Then
If sh_extract.Cells(i, 2).Value = "20040155" Then
'copy the row
sh_extract.Rows(i).Copy
'define the last row of 2nd sheet
b = sh_tocopy.Range("B" & Rows.Count).End(xlUp).Row + 1 'Change column letter if you need to check other column to have last row
'Copy the row
sh_tocopy.Rows(b).PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
sh_extract.Cells(1, 1).Select
End Sub