簡體   English   中英

如何使用 Excel VBA 中的循環更新一系列單元格中的值

[英]How to update the value in a range of cells using a Loop in Excel VBA

我目前正在開發一個 excel 應用程序,其中有一個宏,通過單擊按鈕操作,它可以重置表格中某些單元格內的數值。

在這個表中有 3 列; “安裝數量 (n)”、“所需數量 (m)”和“鎖配置”。

我需要發生的是,當單擊按鈕時,“擬合數量(n)”列中每行的數值被重置以匹配同一行的“所需數量(m)”列中顯示的值.

但是,如果該行“鎖定配置”列中的單元格值設置為“鎖定”,我希望單擊按鈕后“擬合數量(n)”值保持不變。

我希望這是有道理的! 這是我目前對此編碼的嘗試:

Public Sub Reset_Quantity_Fitted()
'Macro to make quantity fitted equal to quantity required

    Dim rng As Range
    Dim cell As Range

    Set rng = Worksheets(ActiveSheet.Name).ListObjects("Table_" & ActiveSheet.Name).ListColumns("Quantity Fitted (n)").DataBodyRange

For Each cell In rng.Cells

    If rng.Offset(, 5) = "Locked" Then
        cell = Worksheets(ActiveSheet.Name).ListObjects("Table_" & ActiveSheet.Name).ListColumns("Quantity Fitted (n)").DataBodyRange
    Else
        cell = Worksheets(ActiveSheet.Name).ListObjects("Table_" & ActiveSheet.Name).ListColumns("Quantity Required (m)").DataBodyRange
    End If

Next cell

End Sub

此方法是此站點上的另一個用戶推薦的,但是在運行此代碼時,我收到以下錯誤:

運行時錯誤“13”:類型不匹配

誰能幫我找出這段代碼有什么問題?

這段代碼應該做你要問的:

Sub Test()

    Dim x As Long

    'Set reference to your table.  Have hard-coded the sheet name and table name in.
    Dim MyTable As ListObject
    Set MyTable = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table_Sheet1")

    'These will be the numerical index numbers of the columns in your table.
    'This assumes your "Locked" column is in the table and has the header "Locked".
    Dim Required As Long, Fitted As Long, Locked As Long
    Required = MyTable.ListColumns("Quantity Required (m)").Index
    Fitted = MyTable.ListColumns("Quantity Fitted (n)").Index
    Locked = MyTable.ListColumns("Locked").Index

    'Look at each row in the table.  Am using `x` rather than `Each Cell`
    'as the row number of the cell may differ from the row location in the table
    'e.g. If your table starts on row 2, then first row after the header is row 3 - row 3 as Cell, but row 1 in table.
    For x = 1 To MyTable.Range.Rows.Count
        'If Locked column doesn't say "Locked" then copy value from
        'Fitted to Required columns, otherwise do nothing.
        If MyTable.DataBodyRange.Cells(x, Locked) <> "Locked" Then
            MyTable.DataBodyRange.Cells(x, Fitted) = MyTable.DataBodyRange.Cells(x, Required)
        Else
            'Do Nothing.
            'The ELSE and comment aren't really needed - just here to show nothing happens.
        End If
    Next x

End Sub  

回答您的Set問題 - Set用於為對象分配引用。
發現this other question可以更好地回答。 考慮下面的代碼:

Sub Test2()

    Dim MyCellValue As String
    Dim MyCellReference As Range

    'Will only contain the string value held in A3 as
    'the default property of a cell is the value.
    MyCellValue = Sheet1.Range("A3")
    MyCellValue = 3 'This changes the value of MyCellValue.
    'MyCellValue.Font.Bold = True 'Doesn't compile as "Invalid Qualifier"

    'Holds a reference to A3.
    Set MyCellReference = Sheet1.Range("A3")
    MyCellReference = 1 'This changes the value held in cell A3.
    MyCellReference.Font.Bold = True

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM