excel 根据用户输入弹出框自动填充不同工作表上的单元格

5jdjgkvh  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(192)

我有一个Excel工作表,其中有两个工作表,标题为“财务”和“发票”。
我希望财务页面有单元格,我将填写键入的单元格。
我希望“发票”页根据“财务”表中的信息自动填充。
我希望代码从Finance工作表上的单元格填充Invoice工作表上的单元格,基于用户键入与Finance工作表的A列中的单元格关联的字母时指定的单元格的几个空格以外的单元格。
如何使Finance区域(将由代码复制)引用的单元格与A列中某行的用户输入单元格相隔几个单元格?

发票单

财务表

在程式码的上方,我将Finance工作表中储存格的信息复制到Invoice工作表中。程式码会建立快显方块,让使用者在其中输入字母'A-Z',而'A-Z'会写入Finance工作表中的储存格A 2-27。
该代码将单元格从Finance工作表复制到Invoice工作表。
我想要一个弹出框,要求用户输入字母,他们键入字母“A-Z”-我将称之为“A”(这将在财务工作表的单元格B2中)。
然后,该代码将单元格1复制到Finance工作表上单元格B2的右侧,并将其粘贴到Invoice工作表上的单元格D3中。
我想我需要编辑代码的Sheets("Finance").Range"B2")部分。

Sub Macro2()

    'Ask user for input
    userinput = InputBox("Type Associated Letter corresponding to Desired Invoice Population:")
    'Copy Name
    Sheets("Finance").Range("B2").Copy Destination:=Sheets("Invoice").Range("D3")
    'Copy Email
    Sheets("Finance").Range("C2").Copy Destination:=Sheets("Invoice").Range("D4")
    'Copy Adress
    Sheets("Finance").Range("D2").Copy Destination:=Sheets("Invoice").Range("D5")
    'Copy Date
    Sheets("Finance").Range("E2").Copy Destination:=Sheets("Invoice").Range("B8")
    'Copy Amount Owed
    Sheets("Finance").Range("I2").Copy Destination:=Sheets("Invoice").Range("D8")
enyaitl3

enyaitl31#

匹配列中的值(Application.Match

Sub InvoiceWriter()

    ' Define constants.
 
    Const PROC_TITLE As String = "Invoice Writer"
    Const SRC_NAME As String = "Finance"
    Const SRC_FIRST_CELL As String = "A2"
    Const DST_NAME As String = "Invoice"

    Dim srcCols() As Variant, dstCells() As Variant
    ' Name, Email, Address, Date, Owed
    srcCols = Array("B", "C", "D", "E", "I")
    dstCells = Array("D3", "D4", "D5", "B8", "D8")

    ' Input.

    Dim UserInput As String: UserInput = InputBox("Type Associated Letter " _
        & "corresponding to Desired Invoice Population:", PROC_TITLE)

    If Len(UserInput) = 0 Then
        MsgBox "Dialog canceled.", vbExclamation, PROC_TITLE
        Exit Sub
    End If
    
    ' Reference the workbook.
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Reference the Source Input range.
    
    Dim sws As Worksheet: Set sws = wb.Sheets(SRC_NAME)
    
    Dim srg As Range
    With sws.Range(SRC_FIRST_CELL)
        Dim slCell As Range: Set slCell = .Resize(sws.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , , xlPrevious)
        If slCell Is Nothing Then
            MsgBox "No data found.", vbCritical, PROC_TITLE
            Exit Sub
        End If
        Set srg = .Resize(slCell.Row - .Row + 1)
    End With
    
    ' Retrieve the Source User Input row index.
    
    Dim srIndex As Variant: srIndex = Application.Match(UserInput, srg, 0)
    
    If IsError(srIndex) Then
        MsgBox "Could not find '" & UserInput & "'.", vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    srIndex = srIndex + srg.Row - 1 ' convert range row to worksheet row
    
    ' Write the values from the Source to the Destination cells.
    
    Dim dws As Worksheet: Set dws = wb.Sheets(DST_NAME)
    
    Dim n As Long
    For n = LBound(srcCols) To UBound(srcCols)
        dws.Range(dstCells(n)).Value = sws.Cells(srIndex, srcCols(n)).Value
    Next n
    
    ' Inform.
    
    MsgBox "Invoice populated.", vbInformation, PROC_TITLE

End Sub

相关问题