【问题标题】:Transferring data from one workbook to another workbook daily into specific/empty cells每天将数据从一个工作簿传输到另一个工作簿到特定/空单元格
【发布时间】: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


【解决方案1】:

尝试使用.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 语句将为真。要遍历每一行上的每个单元格(列),您需要另一个 for 循环。 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 语句在遇到空白单元格时为真,您可以将任何代码放入if,然后退出内部for循环,只处理每一行的第一个空白列。它不会退出外循环,它会移动到下一行。

告诉我这对你有什么作用

编辑

行后: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,这应该会有所帮助:)

【讨论】:

  • 所以我让工作表从“A2”范围开始,它不是空的,然后运行您提供的应用程序。执行整个代码时,我似乎没有动。现在我无法使用我正在使用的偏移命令设置单元格中的值。我是 VBA 新手,很抱歉给您带来麻烦,非常感谢您的帮助。
  • 在您的帮助下,我在上面的对话框中添加了我现在的代码。
  • 哈哈我没有在我的代码中设置 colCount 所以它是 0。我编辑了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多