Dim stockList As Variant
Dim stockPrices As Collection
Dim currentStockIndex As Integer
‘ User Form Code
Private Sub UserForm_Initialize()

' Initialize UserForm and display stock codes in ListBox
Dim stockTable As ListObject
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Set ws = ThisWorkbook.Sheets("STOCK_LIST") ' Replace "Sheet1" with the table name containing the stock code list
Set stockTable = ws.ListObjects("MA_CK")

stockList = stockTable.ListColumns(1).DataBodyRange.Value

Set stockPrices = New Collection

currentStockIndex = 1

LabelStock.Caption = "Enter the price for stock: " & stockList(currentStockIndex, 1)

End Sub
Private Sub CommandButtonNext_Click()
Dim price As String
price = TextBoxPrice.Value

If price <> "" Then
stockPrices.Add price, stockList(currentStockIndex, 1)
TextBoxPrice.Value = ""
currentStockIndex = currentStockIndex + 1

If currentStockIndex <= UBound(stockList, 1) Then
    LabelStock.Caption = "Enter the price" & stockList(currentStockIndex, 1)
Else
    MsgBox "Success!", vbInformation
    Call SaveStockPrice
    Unload Me
End If

Else
MsgBox “Please enter a valid price”, vbExclamation
End If
End Sub

Private Sub SaveStockPrice()
Dim newsheet As Worksheet
Dim i As Integer
Dim stockCode As String
Dim stockPrice As String

Set newsheet = ThisWorkbook.Sheets("STOCK_PRICE")
newsheet.Cells.Clear

newsheet.Cells(1, 1).Value = "Ma_CK"
newsheet.Cells(1, 2).Value = "Gia_CK"

For i = 1 To stockPrices.Count
    stockCode = stockPrices.Keys(i)
    stockPrice = stockPrices(i)
    newsheet.Cells(i + 1, 1).Value = stockCode
    newsheet.Cells(i + 1, 2).Value = stockPrices
    Next i

End Sub

Hệ thống ôn thi của Ôn thi Công chức Toàn quốc

Facebook Zalo