我负责记录俱乐部的费用和支付情况,所以我决定为它制作一个Excel页面。我是Excel VBA编程新手,所以我需要一些建议。
我的想法是:如果有人在场,但没有支付,我只是标记一个x,如果他在场并刚刚付款,我希望能够标记一个N,并使它能够弹出一个msgbox,询问支付的值,并直接将该值放在另一个页面的精确单元格中。
Sub Pay()
Dim Pay As String
Pay = InputBox("Enter A New Payment", "Payment", "Enter amount here")
Range("'versement adherent'!C15").Value = Pay
End Sub这就是我对msgbox的想法,但它太窄了,只有当被要求启动时(当写入值为N的单元格时不会自动启动),并且它写入值的单元格总是相同的。
Private Sub FindN(StrSearchQuery As String)
Set SearchRange = Range("versement adherent!F2:Y21")
FindN = StrSearchQuery
Function FindAll(SearchRange As Range, LookFor As String)
Dim FoundCell As Range
For Each area In SearchRange.Areas
With area
If .cell(.cell.Count).Row > MaxRow Then
MaxRow = .cell(.cell.Count).Column
End If
End With
Next area
Set lastcell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)
Do Until True
Set FoundCell = SearchRange.Find(N)
If FoundCell = False Then
Exit Do
If FoundCell = True Then
For Each FoundCell In Worksheet("versement adherent").Range(FoundCell)
Range("versement adherent !Foundcell").Value = Pay
Range(FoundCell).Value = X
Exit Do
End If
End Function
Set FoundCell = FindAll
If FoundCell = True Then
For Each FoundCell In Worksheet("versement adherent").Range(FoundCell)
Range("versement adherent !Foundcell").Value = Pay
End If我试着添加一些代码,让它找到一个数值为N的单元格,但它似乎不起作用。
发布于 2015-12-11 14:54:24
使用Worksheet_Change事件。
我承认,用您的示例代码,很难完全理解您想要做什么,但是我的代码是基于您的解释的。
下面的代码假设给定的人的名字在A列中,标记X或N的单元格在B列(所以就在此人的名字旁边)。
它还假设您只想在表中输入每个人一次的付款金额。它假定人名列在Y栏的表格中,它搜索Y栏以查找该姓名,并将付款金额放在该人姓名旁边的Z栏中。
这显然将需要调整为您的确切需求,但应该是容易做到这一点,鉴于我已经提出的意见。然而,如果我的假设是相当离谱的,请在评论中告诉我,我可能可以为您调整:)
将下面的代码放置在工作表模块中,在其中标记X或N,并根据特定范围、工作表名称等进行调整。代码将在每次为个人标记N时触发。
Private Sub Worksheet_Change(ByVal Target as Range)
If Target.Column = 2 And UCase(Target.Value2) = "N" ' assumes you enter x or N in column B
Dim Pay As String
Pay = InputBox("Enter A New Payment", "Payment", "Enter amount here")
'now find the name in versement adherent sheet
With Worksheets("versement adherent")
'first find last row to search
Dim lRow as Long
lRow = .Range("Y" & .Rows.Count).End(xlup).Row
'search column Y for the name
Dim rPayCell as Range
Set rPayCell = .Range("Y2:Y" & lRow).Find(Target.Offset(,-1).Value2
If Not rPayCell is Nothing Then
rPayCell.Offset(,1).Value = Pay 'if name is found place payment in column Z
Else 'otherwise alert
Msgbox "Name Not Found in Versement Adherent Sheet"
End If
End With
End If
End Subhttps://stackoverflow.com/questions/34225778
复制相似问题