2015-02-09 70 views
0

我是使用VBA的初学者。根据相邻单元中的条件粘贴范围中的下一个空白单元格中的数据

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

例如表1是总帐:

  • 细胞F14包含文本(出租,或现金,或应收账款等)
  • 细胞K14包含借方金额。单元格l14包含信用额度。

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

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

任何帮助,将不胜感激。

+0

请使用标签更作为技术使用方向。随意使用格式选项来改善问题的质量。同时建议您显示迄今为止编写的代码,以便人们有一个起点来帮助您。 – 2015-02-09 12:51:21

回答

0

我觉得这里是一些代码,在一个范围内获取下一个可用无细胞

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 
相关问题