背景:
我在Excel中设置了一个空数据库,我计划用它来跟踪我的预算用途的每月费用。我希望能够做的是打开我的电子表格,输入收据中的数据(购买日期,购买类型和代码'以及购买金额),然后点击我的&#34 ;发布数据"用于运行此宏的宏按钮。我知道如何设置所有这些,我只是在编写宏本身时遇到了麻烦。
这就是我想要的:
一个宏:
C2
中找到日期位于单元格E
中的正确行(例如,在我的示例中为row 8
)C2
范围内的单元格F2:M2
中的费用代码查找正确的列(例如我的column G
)C4
中输入的值粘贴到上述行和列的交集中(例如,在我的示例中为单元格G8
)什么没用?
我能够通过生成IF语句(在每个数据单元格中)获得所需的结果:
=IFERROR(IF(AND(E5=$C$2,F$2=$C$3),$C$4,""),"")
我能够记录这个宏,它将这个语句复制并粘贴到我的所有数据字段(我的电子表格整整一年)。之后,宏会在语句上复制和粘贴值。唯一的问题是这个宏会覆盖先前输入的数据,这就是为什么我觉得我需要一个更复杂的宏。
请指出我正确的方向或我可以找到如何做到这一点。
屏幕截图(我正在尝试做什么):
宏:
Sub CopyPasteData()
'
' CopyPasteData Macro
'
'
Range("F5").Select
ActiveCell.FormulaR1C1 = "=IFERROR(IF(AND(RC5=R2C3,R2C=R3C3),R4C3,""""),"""")"
Range("F5").Select
Selection.Copy
Range("F5:M32").Select
ActiveWindow.SmallScroll Down:=24
Range("F5:M32,F36:M66").Select
Range("F36").Activate
ActiveWindow.SmallScroll Down:=27
Range("F5:M32,F36:M66,F70:M99").Select
Range("F70").Activate
ActiveWindow.SmallScroll Down:=21
Range("F5:M32,F36:M66,F70:M99,F103:M133").Select
Range("F103").Activate
ActiveWindow.SmallScroll Down:=21
Range("F5:M32,F36:M66,F70:M99,F103:M133,F137:M166").Select
Range("F137").Activate
ActiveWindow.SmallScroll Down:=24
Range("F5:M32,F36:M66,F70:M99,F103:M133,F137:M166,F170:M200").Select
Range("F170").Activate
ActiveWindow.SmallScroll Down:=21
Range("F5:M32,F36:M66,F70:M99,F103:M133,F137:M166,F170:M200,F204:M234").Select
Range("F204").Activate
ActiveWindow.SmallScroll Down:=24
Range( _
"F5:M32,F36:M66,F70:M99,F103:M133,F137:M166,F170:M200,F204:M234,F238:M267"). _
Select
Range("F238").Activate
ActiveWindow.SmallScroll Down:=21
Range( _
"F5:M32,F36:M66,F70:M99,F103:M133,F137:M166,F170:M200,F204:M234,F238:M267,F271:M301" _
).Select
Range("F271").Activate
ActiveWindow.SmallScroll Down:=21
Range( _
"F5:M32,F36:M66,F70:M99,F103:M133,F137:M166,F170:M200,F204:M234,F238:M267,F271:M301,F305:M334" _
).Select
Range("F305").Activate
ActiveWindow.SmallScroll Down:=24
Range( _
"F5:M32,F36:M66,F70:M99,F103:M133,F137:M166,F170:M200,F204:M234,F238:M267,F271:M301,F305:M334,F338:M368" _
).Select
Range("F338").Activate
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveWindow.ScrollRow = 344
'deleted many lines...
ActiveWindow.ScrollRow = 5
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-12
Application.CutCopyMode = False
Range("F5:M32").Select
ActiveWindow.SmallScroll Down:=-9
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=18
Range("F36:M66").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-15
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=27
Range("F70:M99").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-12
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=27
Range("F103:M133").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-9
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=27
Range("F137:M166").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-9
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=27
Range("F170:M200").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-12
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=24
Range("F204:M234").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-15
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=36
Range("F238:M267").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-6
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=27
Range("F271:M301").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-12
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=33
Range("F305:M334").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-12
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=27
Range("F338:M368").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-12
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.ScrollRow = 332
'deleted many lines
ActiveWindow.ScrollRow = 7
ActiveWindow.ScrollRow = 5
Range("A1").Select
End Sub
答案 0 :(得分:0)
您需要使用Find()
找到正确的列和行:
Sub Test()
Dim sht As Worksheet
Dim fD As Range, fT As Range, dt
Set sht = ActiveSheet
dt = CDate(sht.Range("C2").Value)
Set fD = sht.Range("E5:E1000").Find(dt)
Set fT = sht.Range("F2:M2").Find(sht.Range("C3").Value, lookat:=xlWhole)
If Not fD Is Nothing And Not fT Is Nothing Then
With sht.Cells(fD.Row, fT.Column)
.Value = .Value + sht.Range("C4").Value
End With
End If
End Sub