Update Tables from Multi page data VBA

karolina1406

Board Regular
Joined
Apr 18, 2016
Messages
110
Office Version
  1. 365
Platform
  1. Windows
I am using MultiPage in my Userform to update 2 separate Excel tables. I have below code to update first table called "ReservCards " from page 1 called "Cards" and it workds perfectly. How can i improve the code to update my second table called "ReservCables", with the same data but from page 2 called "cables" and my textBoxes are called "cablestype" and "cablequantity"
VBA Code:
Dim oNewRow As ListRow
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Available_Stock").Range("ReservCards")
rng.Select
Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)
With ws
    oNewRow.Range.Cells(1, 2).Value = Date
    oNewRow.Range.Cells(1, 3).Value = Me.cardtype.Value
    oNewRow.Range.Cells(1, 4).Value = Me.cardquantity.Value
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Just repeat the code.
It doesn't matter which page the textboxes are on, they are unique.

VBA Code:
Private Sub CommandButton1_Click()
  Dim oNewRow As ListRow
  Dim rng As Range
  
  Set rng = ThisWorkbook.Worksheets("Available_Stock").Range("ReservCards")
  Set oNewRow = rng.ListObject.ListRows.Add(AlwaysInsert:=True)
  oNewRow.Range.Cells(1, 2).Value = Date
  oNewRow.Range.Cells(1, 3).Value = Me.cardtype.Value
  oNewRow.Range.Cells(1, 4).Value = Me.cardquantity.Value

  Set rng = ThisWorkbook.Worksheets("Available_Stock").Range("ReservCables")
  Set oNewRow = rng.ListObject.ListRows.Add(AlwaysInsert:=True)
  oNewRow.Range.Cells(1, 2).Value = Date
  oNewRow.Range.Cells(1, 3).Value = Me.cablestype.Value
  oNewRow.Range.Cells(1, 4).Value = Me.cablequantity.Value
End Sub
 
Upvote 0
In fact if it's in the same book, it could be like this:

VBA Code:
Private Sub CommandButton1_Click()
  With Range("ReservCards").ListObject.ListRows.Add(AlwaysInsert:=True).Range
    .Cells(1, 2).Value = Date
    .Cells(1, 3).Value = Me.cardtype.Value
    .Cells(1, 4).Value = Me.cardquantity.Value
  End With
  
  With Range("ReservCables").ListObject.ListRows.Add(AlwaysInsert:=True).Range
    .Cells(1, 2).Value = Date
    .Cells(1, 3).Value = Me.cablestype.Value
    .Cells(1, 4).Value = Me.cablequantity.Value
  End With
End Sub
 
Upvote 0
Just repeat the code.
It doesn't matter which page the textboxes are on, they are unique.

VBA Code:
Private Sub CommandButton1_Click()
  Dim oNewRow As ListRow
  Dim rng As Range
 
  Set rng = ThisWorkbook.Worksheets("Available_Stock").Range("ReservCards")
  Set oNewRow = rng.ListObject.ListRows.Add(AlwaysInsert:=True)
  oNewRow.Range.Cells(1, 2).Value = Date
  oNewRow.Range.Cells(1, 3).Value = Me.cardtype.Value
  oNewRow.Range.Cells(1, 4).Value = Me.cardquantity.Value

  Set rng = ThisWorkbook.Worksheets("Available_Stock").Range("ReservCables")
  Set oNewRow = rng.ListObject.ListRows.Add(AlwaysInsert:=True)
  oNewRow.Range.Cells(1, 2).Value = Date
  oNewRow.Range.Cells(1, 3).Value = Me.cablestype.Value
  oNewRow.Range.Cells(1, 4).Value = Me.cablequantity.Value
End Sub
hi,
I just tried to apply this code to a different task and the difference is, that a CommanButton 1 is in a Tab called "Requests" and i have an error at line Set rng = ThisWorkbook.Worksheets("Available_Stock").Range("ReservCards")

any idea why? ;)
 
Upvote 0
What does the error message say?
You must change the name of the sheet to " Requests " and also in that sheet you must have a named range, you must update the named range in the code line.

ThisWorkbook.Worksheets("Requests").Range("?????")
 
Upvote 0
What does the error message say?
You must change the name of the sheet to " Requests " and also in that sheet you must have a named range, you must update the named range in the code line.

ThisWorkbook.Worksheets("Requests").Range("?????")
ok, got it but the next problem occurred :-)
I use now below code to update 2 different tables in 2 different tabs. It works, however, want to improve it to reflect that if CBCode2 box is empty - then ReservedOptics table will not be populated while ReservedCards table will be (because CbCode will not be empty)... is it doable? ;)
VBA Code:
Dim oNewRow As ListRow
Dim rng As Range
  
Set rng = ThisWorkbook.Worksheets("cards").Range("ReservedCards")
Set oNewRow = rng.ListObject.ListRows.Add(AlwaysInsert:=True)
oNewRow.Range.Cells(1, 2).Value = "Reserved"
    oNewRow.Range.Cells(1, 3).Value = Date
    oNewRow.Range.Cells(1, 4).Value = Me.Requestor.Value
    oNewRow.Range.Cells(1, 5).Value = Me.email.Value
    oNewRow.Range.Cells(1, 6).Value = Me.ProjectID.Value
    oNewRow.Range.Cells(1, 7).Value = Me.CostCentre.Value
    oNewRow.Range.Cells(1, 8).Value = Me.PO.Value
    oNewRow.Range.Cells(1, 9).Value = Me.DTPicker1.Value
    oNewRow.Range.Cells(1, 10).Value = Me.CBCode.Value
    oNewRow.Range.Cells(1, 11).Value = Me.RequestedQuantity.Value

Set rng = ThisWorkbook.Worksheets("optics").Range("Reservedoptics")
Set oNewRow = rng.ListObject.ListRows.Add(AlwaysInsert:=True)
oNewRow.Range.Cells(1, 3).Value = Date
oNewRow.Range.Cells(1, 4).Value = Me.Requestor.Value
oNewRow.Range.Cells(1, 5).Value = Me.email.Value
oNewRow.Range.Cells(1, 6).Value = Me.ProjectID.Value
oNewRow.Range.Cells(1, 7).Value = Me.CostCentre.Value
oNewRow.Range.Cells(1, 8).Value = Me.PO.Value
oNewRow.Range.Cells(1, 9).Value = Me.DTPicker1.Value
oNewRow.Range.Cells(1, 10).Value = Me.CBCode2.Value
oNewRow.Range.Cells(1, 11).Value = Me.RequestedQuantity2.Value
 
Upvote 0

Forum statistics

Threads
1,224,844
Messages
6,181,294
Members
453,030
Latest member
PG626

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