将数据移至另一个工作表时,运行时错误“ 1004”

时间:2019-08-12 06:46:44

标签: excel vba

我正在分析一些光谱,并试图选择与特定波长相对应的强度读数,并将其粘贴到新的工作表中。该代码可以运行,但是会显示

  

运行时错误'1004'-Worksheets(2).Cells(i,“ B”)。Value = Worksheets(1).Cells(greenwl,columnnumber).Value的应用程序定义或对象定义的错误代码行

Sub GreenWlValues()

Dim targetwl As Long
Dim greenwl As Long
Dim columnnumber As Long
Dim i As Long 'used to denote the cell rows in the new Worksheet 

targetwl = 9 'example value
columnnumber = 2
i = 2

For greenwl = 1 To Cells(Rows.Count, 1).End(xlUp).Row

 Do While  Cells(greenwl, "A").Value = targetwl

  Worksheets(2).Cells(i, "B").Value = Worksheets(1).Cells(greenwl, 
  columnnumber).Value

  columnnumber = columnnumber + 1
  i = i + 1

  Loop

 Next greenwl


End Sub

1 个答案:

答案 0 :(得分:0)

您不能在“单元格”对象中将列名作为字母传递,因此必须将“ B”更改为2。在下面找到更新的代码:

Sub GreenWlValues()

Dim targetwl As Long
Dim greenwl As Long
Dim columnnumber As Long
Dim i As Long 'used to denote the cell rows in the new Worksheet

targetwl = 9 'example value
columnnumber = 2
i = 2

For greenwl = 1 To Cells(Rows.Count, 1).End(xlUp).Row

 Do While Cells(greenwl, 1).Value = targetwl

  Worksheets(2).Cells(i, 2).Value = Worksheets(1).Cells(greenwl,columnnumber).Value

  columnnumber = columnnumber + 1
  i = i + 1

  Loop

 Next greenwl


End Sub
相关问题