【发布时间】:2014-03-11 19:43:37
【问题描述】:
我正在开发一个宏,它将数据从一个工作簿(enterdata) 传输到另一个工作簿(extractdata),同时在extractdata 工作簿中收集大量公式。随着数据每天输入,我需要将数据提取到特定单元格中。这些数据需要编译,我在这部分遇到了麻烦。
我需要将数据提取到特定单元格中,因为如果我使用空行函数,我的行尾和列底部都有公式,因此我试图将数据输入到特定单元格中
我认为我在正确的轨道上,但我不断有Run-time error '1004': 声明select method of range class failed。
提前致谢, 安德森
'Next the transfers of the data from the daily quality activity to the specific worksheet of the unit charts'
'Accesories'
Set myData = Workbooks.Open("C:\database\Extractdata.xlsm")
Worksheets("ACC").Select
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 1 'column A has a value of 1
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 3 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
End If
Next
With Worksheets("ACC").Range("A3")
.Offset(ColumnCount, 0) = currentDate
.Offset(ColumnCount, 1) = devicesStockedACC
.Offset(ColumnCount, 2) = qipAttemptsACC
.Offset(ColumnCount, 3) = qipncproductCountACC
.Offset(ColumnCount, 4) = totalqcncCountACC
.Offset(ColumnCount, 5) = devicencidCountACC
.Offset(ColumnCount, 6) = componentncidCountACC
End With
NEW CODE 3/13/14
Dim currentDate As String
Dim devicesStockedACC As String
Dim qipAttemptsACC As String
Dim qipncproductCountACC As String
Dim totalqcncCountACC As String
Dim devicencidCountACC As String
Dim componentncidCountACC As String
'Acessories'
Worksheets("tuesday").Select
currentDateACC = Range("A1")
Worksheets("tuesday").Select
devicesStockedACC = Range("C12")
Worksheets("tuesday").Select
qipAttemptsACC = Range("D12")
Worksheets("tuesday").Select
qipncproductCountACC = Range("E12")
Worksheets("tuesday").Select
totalqcncCountACC = Range("H12")
Worksheets("tuesday").Select
devicencidCountACC = Range("L12")
Worksheets("tuesday").Select
componentncidCountACC = Range("M12")
'Next the transfers of the data from the daily quality activity to the specific worksheet of the unit charts'
'Accesories'
Set myData = Workbooks.Open("C:\database\Extractdata.xlsm")
Worksheets("ACC").Select
Worksheets("ACC").Range("A3").Select
Application.Run "Selectfirstblankcell"
Sheets("ACC").Activate
With Worksheets("ACC").Range(ActiveCell)
.Offset(ColumnCount, 0) = currentDate
.Offset(ColumnCount, 1) = devicesStockedACC
.Offset(ColumnCount, 2) = qipAttemptsACC
.Offset(ColumnCount, 3) = qipncproductCountACC
.Offset(ColumnCount, 4) = totalqcncCountACC
.Offset(ColumnCount, 5) = devicencidCountACC
.Offset(ColumnCount, 6) = componentncidCountACC
.Offset(ColumnCount, 8) = currentDate
.Offset(ColumnCount, 15) = currentDate
End With
Application.Run "Selectfirstblankcell" =
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 1 'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 3 To rowCount
For currentCol = 1 To ColCount
currentCellValue = Cells(currentRow, currentCol).FormulaR1C1
If IsEmpty(currentCellValue) Or currentCellValue = "" Then
Cells(currentRow, currentCol).Select 'do something else than select maybe?
'put code here to handle the empty cell
Exit For 'this will exit the inner loop, so it will only process the first blank cell on each row
End If
Next
Next
End Sub
【问题讨论】:
-
我在运行该代码时没有收到任何错误。你在哪里得到错误?末尾的
with块中的所有变量都未包含在发布的代码中的任何位置。此外,您不能使用.select一次选择多于一行...因此,每次选择新单元格时,都会取消选择前一个单元格。使用调试器单步执行您的代码并观察发生了什么 -
好的,感谢您的帮助。我现在已经让它运行了,它无论如何都要从 currentRow(3) 开始并转到第一个空单元格?目前它会循环,直到它下面有一个填充的单元格。你知道我怎样才能让它运行到第一个空白单元格,不管它下面是空单元格还是填充单元格。感谢您的帮助。
标签: vba excel rowcount is-empty