0

Am not expert but need one help... Iam developing one excel macro for data entry as part of my project.

  1. whatever data I enter in sheet1 (data entry form) should save in sheet2.
  2. whenever I enter the existing employee id in sheet2, I need to get pop up window with msg "data available" and should reflect in the respective columns
  3. whenever I enter data for above case " data already exist in sheet2), despite same information, remaining values should save in sheet 2 under same header by adding to existing info by separating comma.
  4. duplicate record should not create for same employee id except adding the info to existing one

Excel VBA macro I tried

I need below details in sheet2 by entering info in sheet1

Enter Ticket # Enter Employee ID Select Gate Keeper Assign ticket to (1st level) 1st Level Val Status Assign ticket to (2nd level) 2nd Level Val Status QA Check done By Detailed Remarks Sent Remarks

Code:

Private Sub CommandButton1_Click()
Dim TicketID As String, Dat As Date, Clientname As String
Dim EmpID As Double, Gatekeep As String, fisrtlevelname As String
Dim firstlevelStatus As String, secondlevelname As String, Secondlevelstatus As String, QA As String, Remarks As String
Worksheets("Sheet1").Select
TicketID = Range("B2")
Dat = Range("B3")
Clientname = Range("B4")
EmpID = Range("B5")
Gatekeep = Range("B6")
fisrtlevelname = Range("B7")
firstlevelStatus = Range("B8")
secondlevelname = Range("B9")
Secondlevelstatus = Range("B10")
QA = Range("B11")
Remarks = Range("B12")
Worksheets("Sheet2").Select
Worksheets("Sheet2").Range("A1").Select
If Worksheets("Sheet2").Range("A1").Offset(1, 0) <> "" Then
Worksheets("Sheet2").Range("A1").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = TicketID
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Dat
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Clientname
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = EmpID
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Gatekeep
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = fisrtlevelname
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = firstlevelStatus
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = secondlevelname
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Secondlevelstatus
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = QA
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Remarks
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("B2").Select
End Sub
Mikku
  • 6,538
  • 3
  • 15
  • 38
Ram
  • 1
  • 1
  • Code I used below – Ram Jan 05 '19 at 17:59
  • Why don't you just copy the range B2:B12 in sheet1 and paste to the next empty row in sheet2. See the answer to this [SO Question](https://stackoverflow.com/questions/17976329/copy-and-paste-a-set-range-in-the-next-empty-row) – GMalc Jan 05 '19 at 19:15
  • First sheet I used for data entry... I have considered B2:B12 is the field where agent can enter data. And on a click update, it should save in sheet2 A2: J2. Similarly this activities goes on for each entry. but need to identify the existing record and append the data against the line item already created without any duplicate entry – Ram Jan 05 '19 at 21:27

1 Answers1

0

New Code to Try: Assumes Employee ID needs to overwrite the rest of an input, only comparison made to existing data.

Sub TryThis()
Dim Data As Variant
'Loads your data into an array beginning at (1,1) and ending at (11,1)
Worksheets("Sheet1").Activate
Data = Range("B2", "B12")
'Selects worksheet 2 and puts in your data. I personally don't like the .activate, but for a simple program
'that this seems to be, it shouldn't hurt your performance.
Worksheets("Sheet2").Activate
    'evaluates if it is the first entry by determing if cell is empty
    If Range("A2") <> "" Then
        'If it is not empty, sheet2 is put into an array (an array is overkill unless you have a lot of data)
        Dim Comp As Variant
        Comp = Range("A2", Range("A1").End(xlDown).End(xlToRight))
        'looks at each employee ID already existing in sheet2
        For i = 1 To UBound(Comp)
            'If the employee Id exists, it will write over it here.
            If Data(4, 1) = Comp(i, 4) Then
                MsgBox "Employee ID Exists" & vbNewLine & "Employee Information Updated"
                Dim CCount As Long
                CCount = 1
                Do Until CCount = 11
                    'used i + 1 because of your header on sheet2 and was too lazy to create a new variable
                    Cells(i + 1, CCount).Value = Data(CCount, 1)
                    CCount = CCount + 1
                Loop
            Worksheets("Sheet1").Activate
            'Resets your input range
            Range("B2:B17").Value = ""
            'Since the information is written here, it will exit sub for next entry
            Exit Sub
            End If
        Next i
    End If

        Dim RCount As Long
        RCount = 2
        Do Until Cells(RCount, 2) = ""
            RCount = RCount + 1
        Loop

        CCount = 1
        Do Until CCount = 11
            Cells(RCount, CCount).Value = Data(CCount, 1)
            CCount = CCount + 1
        Loop
        MsgBox "New Employee Id" & vbNewLine & "New Information Added"
        Worksheets("Sheet1").Activate
        Range("B2:B12").Value = ""
End Sub    

Original Code Given

Sub TryThis()
Dim Data As Variant
'Loads your data into an array beginning at (1,1) and ending at (11,1)
Worksheets("Sheet1").Activate
    Data = Range("B2", "B18")

'Selects worksheet 2 and puts in your data. I personally don't like the .activate, but for a simple 
'program that this seems to be, it shouldn't hurt your performance.
Worksheets("Sheet2").Activate
    Dim RCount As Long
    RCount = 2
        Do Until Cells(RCount, 2) = ""
        RCount = RCount + 1
        Loop
    Dim CCount As Long
    CCount = 1
        Do Until CCount = 17
            Cells(RCount, CCount).Value = Data(CCount, 1)
            CCount = CCount + 1
        Loop
End Sub
Jon Dee
  • 31
  • 4
  • For the purpose of the duplicate, we can loop through the values in `sheet2` with the data array from `sheet1`. To make this more efficient though, instead of looking at all 17 pieces of data, are there a few that if they are equal, all the other will be equal? I use a similar loop that has 40 pieces of data but if 2 pieces are the same, the rest will be the same, which makes the comparison and logic easier to follow. – Jon Dee Jan 06 '19 at 00:45
  • First sheet I used for data entry... I have considered B2:B12 is the field where agent can enter data. And on a click update, it should save in sheet2 A2: J2. Similarly this activities goes on for each entry. but need to identify the existing record and append the data against the line item already created without any duplicate entry – Ram Jan 06 '19 at 07:06
  • could you please help me here.. little urgent – Ram Jan 06 '19 at 08:22
  • It seems that you want to append the data but there is a write over. Essentially, are you looking at `if EmployeeId Exists Then Overwrite that line`? – Jon Dee Jan 06 '19 at 21:23
  • Update Code for you to try. The only comparison made is employee ID. If there are more comparisons needed, then an `and` with the `comparison` just need to be added in. – Jon Dee Jan 07 '19 at 00:02
  • Yes.. I made it.. Thanks a lot for support – Ram Feb 06 '19 at 17:50