Change Code from New to Update

Status
Not open for further replies.

MyForcey

New Member
Joined
Oct 22, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi, I've tried various ways but nothing seems to work for the code I have.
The below code allows me to add data to a new row from UserForm1.
I need assistance with changing the code so that I can select the row which will populate the UserForm and allow me to amend/update the data.

The code below is for userForm1, I need it to apply to UserForm3 if possible?

VBA Code:
Option Explicit

Dim ary1 As Variant, ary2 As Variant, ary3 As Variant, aryRISK As Variant
Dim tbl1 As ListObject, tbl2 As ListObject, tbl3 As ListObject, tblRISK As ListObject
Dim ws1 As Worksheet, ws2 As Worksheet
Dim newrow As ListRow
Dim reins As String
Dim msgValue As VbMsgBoxResult
Dim X As Long

Private Sub UserForm_Initialize()
  Set ws1 = LIVE
  Set tblRISK = ws1.ListObjects("FAC_RISKS")
  Set ws2 = Sheet2
  Set tbl1 = ws2.ListObjects("Table1")
  Set tbl2 = ws2.ListObjects("Table2")
  Set tbl3 = ws2.ListObjects("Table3")
  With tbl1
    If .ListRows.Count = 1 Then
      Me.cedent.AddItem .DataBodyRange(1)
    Else
      ary1 = .DataBodyRange
      Me.cedent.List = ary1
    End If
  End With
  With tbl2
    If .ListRows.Count = 1 Then
      Me.status.AddItem .DataBodyRange(1)
    Else
      ary1 = .DataBodyRange
      Me.status.List = ary1
    End If
  End With
  With tbl3
    If .ListRows.Count = 1 Then
      Me.reinsurers.AddItem .DataBodyRange(1)
    Else
      ary1 = .DataBodyRange
      Me.reinsurers.List = ary1
    End If
  End With
End Sub

Private Sub ADDCommandButton1_Click()
  msgValue = MsgBox("Save & Add This Risk Now?", vbYesNo + vbInformation, "Confirmation")
  If msgValue = vbNo Then Exit Sub
  reins = ""
  With Me.reinsurers
    For X = 0 To .ListCount - 1
      If .Selected(X) = True Then
        reins = reins & .List(X) & vbCrLf
      End If
    Next X
  End With
  Set newrow = tblRISK.ListRows.Add
  With newrow
    .Range(2) = Me.reference.Value
    .Range(3) = Me.insured.Value
    .Range(4) = CDate(Me.startdate.Value)
    .Range(5) = CDate(Me.enddate.Value)
    .Range(6) = Me.share.Value / 100
    .Range(7) = reins
    .Range(8) = Me.brokerage.Value
    .Range(9) = Me.excess.Value
    .Range(10) = Me.cedent.Value
  End With
  Unload Me
  UserForm1.Show
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Status
Not open for further replies.

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top