Excel VBA编程执行VBA

我负责logging俱乐部的收费情况,所以我决定做一个Excel的页面。 我是Excel VBA编程的新手,所以我需要一些build议。

我的想法是:如果这个人在场,但没有付钱,我只是标记一个x ,如果他在场,只要付款,我想能够标记一个N并使其popup一个msgboxpopup询问付款的价值,并直接把这个价值在一个精确的单元格在另一个页面上。

 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 

我已经尝试添加一些代码,使它find一个值为N的单元格的部分,但似乎并没有工作。

使用Worksheet_Change事件。

我承认,用你的示例代码,很难完全理解你想要做什么,但是我把我的代码放在你的解释上。

我的代码如下所示,假设某人的姓名在列A中,而标记X或N的单元格在列B中(紧挨着该人的姓名)。

它也假定你只是想在每个人的input单中input一次付款金额。 它假定该人员的姓名在该表中的Y栏中列出。它在Y栏中search该姓名,并在该姓名旁边的栏Z中的付款金额中join。

这显然需要根据您的具体要求进行调整,但考虑到我所提出的意见,应该很容易做到这一点。 但是,如果我的假设是基础的,让我知道在评论中,我可以为你调整:)

将下面的代码放在工作表模块里面,标记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 Sub