I have an Excel sheet with two sheets titled Finance and Invoice.
I want the Finance page to have cells which I would fill out by typing in the cells.
I want the Invoice page to be auto-populated from the information in the Finance sheet.
I want the code to populate cells on the Invoice sheet from the cells on the Finance sheet, based on cells a few spaces away from a cell that is specified by a user typing in a letter which is associated with a cell in column A of the Finance sheet.
How do I get the Finance range (which will be copied by the code) to reference a cell that is a few over from a user-input cell in a row in column A?
In the top part of the code, I am copying information from cells in the Finance sheet into the Invoice sheet. The code creates a pop-up box where the user is supposed to write in a letter 'A-Z', where 'AZ' are written in cells A 2-27 on the Finance sheet.
The code is copying cells from the Finance sheet into the Invoice sheet.
I want a pop-up box that asks a user for a letter, they type in a letter 'A-Z'- I'll call it 'A' (this would be in cell B2 on the Finance sheet).
The code will then copy the cell 1 to the right of cell B2 on the Finance sheet and paste it into cell D3 on the Invoice sheet.
I think I need to edit the Sheets("Finance").Range"B2")
portion of the code.
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")
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.