【问题标题】:Pasting data in next blank cell in range based on criteria in adjacent cell根据相邻单元格中的条件将数据粘贴到范围内的下一个空白单元格中
【发布时间】:2015-02-09 12:40:31
【问题描述】:

我是使用 VBA 的初学者。

我想将一个值复制并粘贴到特定范围内第一个空白单元格中的另一个工作表中。

例如表 1 是总帐:

  • 单元格 F14 包含文本(租金、现金或应收帐款等)

  • 单元格 K14 包含借方金额。单元格 l14 包含信用额度。

    我想使用基于 F14 中的文本的下一个可用单元格将 K14 或 K15 中的金额复制并粘贴到特定范围内的工作表 2。

如果是“现金”,则范围 = sheet2 D1:D10。如果 Rent 然后粘贴到范围 sheet2 D20:D30 等。

任何帮助将不胜感激。

【问题讨论】:

  • 请多使用标签作为使用技术的方向。随意使用格式选项来提高问题的质量。此外,建议显示您到目前为止编写的代码,以便人们有一个起点来帮助您。

标签: vba excel


【解决方案1】:

我认为这里有一些代码可以获取范围内下一个可用的空闲单元格

Sub capturedata()

Dim sheet1, sheet2 As Worksheet
Dim testValue As String
Dim cashRange, rentRange As Range

Set sheet1 = ActiveWorkbook.Sheets("Sheet1") ' general ledger
Set sheet2 = ActiveWorkbook.Sheets("sheet2")

testValue = sheet1.Range("F14") ' the cell with rent, cash, etc in it

'the ranges
Set cashRange = sheet2.Range("D1:D10")
Set rentRange = sheet2.Range("D20:D30")

Select Case testValue 'based on what is in "f14"
    Case "Rent"
        'paste from k14
        '****I believe the below is the part you're really concerned with***

        For Each cell In cashRange
            If cell.Value = "" Then 'first empty cell
                    cell.Value = sheet1.Range("k14") 'make this more dynamic with a for loop if needed
                Exit For
            End If
        Next cell
    Case "Cash"

        'paste from k15
        For Each cell In rentRange ' make into a function to avoid repeated code
            If cell.Value = "" Then 'first empty cell
                    cell.Value = sheet1.Range("k14")
                Exit For
            End If
        Next cell

    Case "Accounts Receivable"
       'add a for loop here  based on other criteria
    Case Else
    'case not met

End Select


End Sub

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 2021-10-03
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多