我正在开发一个宏,它将数据从一个工作簿(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
答案 0 :(得分:0)
尝试使用.formulaR1C1
代替.value
。您当前的代码应该运行到sourceCol
For currentRow = 3 To rowCount
currentRowValue = Cells(currentRow, sourceCol).formulaR1C1
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
End If
Next
您的上述代码(我从.value
更改为.formular1C1
)将遍历sourceCol中的每一行,如果当前单元格为空,则if语句将为true。循环遍历每行上的每个单元格(列),您需要另一个循环。 1表示下降,1表示正在进行
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
这样代码将循环遍历每一行,内部循环将循环遍历该行中的列,并且内部循环中的if语句在遇到空白单元格时将为true,您可以将任何代码放入if,然后它将退出内部for循环,以便只处理每一行中的第一个空白列。它不会退出外循环,它将继续前进到下一行。
让我知道这对你有用吗</ p>
后面的行:rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
添加:colCount = Cells.find("*", [A1], , , xlByColumns, xlPrevious).column
使用调试器时,您可以看到所有局部变量的值。所以,当这样的事情发生在什么都不做的时候,请确保检查那些被初始化。
抱歉不包括那个。现在内循环应该运行。另外,在配件中,这个:Worksheets("tuesday").Select
只需要在配件部分的顶部进行一次。执行此操作:DevicesStockedAcc = Range("A1")
不会取消选择工作表,因此您不需要每次都选择它。
另外,我注意到在设置你正在做的配件时:currentDateACC = Range("A1")
我还记得变量:currentDateACC
你有一个字符串。这个变量应该代表一个细胞的价值吗?还是细胞本身?仅使用.range
将获得对范围的引用,而不是单元格中的值。获取值使用:devicesStockedACC = Range("C12").value
将该单元格中的值保存为字符串,类似地,在进行偏移时使用:.Offset(ColumnCount, 1).value = devicesStockedACC
可以帮助一些:)