使用Excel中的宏编辑另一个表中的单元格值?

发布于 2024-11-07 21:39:51 字数 232 浏览 5 评论 0原文

我有一个 Excel 项目,其中包含 3 个工作表,即“不健康食品”、“健康食品”和“膳食”。

这两个食品表包含食品列表及其脂肪、蛋白质和碳水化合物含量。利用这些数据,还可以计算出每 100 克的卡路里含量。这些食品分为 3 个小标题:食品、零食和饮料。我想创建一个宏,该宏将使用每个桌子上每个食物旁边的“添加到餐食”按钮启动,该按钮会将行的内容复制到“餐食”类中的第一个可用行。

这有可能做到吗?如果是这样,怎么办?

I have an Excel project which contains 3 worksheets, namely "Unhealthy Foods" "Healthy Foods" and "Meal"

The two food tables contain a list of foods along with their their fat, protein and carbohydrate content. Using that data, their calories per 100 grams are also calculated. These foods are divided into 3 sub-headings: Foods, snacks and drinks. I want to create a macro which will be launched using an "Add to Meal" button next to each food item on each table which will copy the row's content to the first available row in the "Meal" class.

Is it possible for this to be done? If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-11-14 21:39:51

(在我看来)使用按钮引用工作表上的单元格的问题是按钮是工作表上的对象,这使得获取它们在列/行方面的位置非常棘手。

如果您想使用按钮,这里是强力方法。假设在 Healthy Foods 中您有以下内容:

     A         B      C          D
1    Item      Fat    Protein    Sugar
2    Apple     0      50         5       |Add To Menu|  <-- this is a commandbutton called **Apple**
3    Snickers  100    0          100     |Add to Menu|  <-- this is a commandbutton called **snickers**

以下代码会将详细信息复制到 Meal 中的下一个可用行(注意 - 我假设 Meal 与上面所示的表格格式相同)

Private Sub Apple_Click()
    AddMealToMenu Range("A2") //For each button you must specify range where item is in table
End Sub

Sub AddMealToMenu(ref As Range)
    Dim mealItem As Range
    Set mealItem = Range(ref, ref.Offset(0, 3))
    mealItem.Copy Destination:=GetNextFreeRow
End Sub

Function GetNextFreeRow() As Range
    With Worksheets("Meal")
        If .Range("A2") = vbNullString Then
            Set GetNextFreeRow = .Range("A2")
        Else
            Set GetNextFreeRow = .Range("A1").End(xlDown).Offset(1, 0)
        End If
    End With
End Function

对于士力架,您需要为 Apple ie 添加类似的代码,

Private Sub Snickers_Click()
    AddMealToMenu Range("A3")
End Sub

如果这对您有用,很好,但可能需要一些设置,并且是一种蛮力方法。

The issue (in my opinion) with using buttons to refer to cells on a worksheet is that buttons are objects on the worksheet which makes getting their position in terms of column / row quite tricky.

Here is the brute force approach if you want to use buttons. Suppose in Healthy Foods you have the folllowing:

     A         B      C          D
1    Item      Fat    Protein    Sugar
2    Apple     0      50         5       |Add To Menu|  <-- this is a commandbutton called **Apple**
3    Snickers  100    0          100     |Add to Menu|  <-- this is a commandbutton called **snickers**

The following code will copy the details to next available row in Meal (NB - I assume Meal is same table format as shown above)

Private Sub Apple_Click()
    AddMealToMenu Range("A2") //For each button you must specify range where item is in table
End Sub

Sub AddMealToMenu(ref As Range)
    Dim mealItem As Range
    Set mealItem = Range(ref, ref.Offset(0, 3))
    mealItem.Copy Destination:=GetNextFreeRow
End Sub

Function GetNextFreeRow() As Range
    With Worksheets("Meal")
        If .Range("A2") = vbNullString Then
            Set GetNextFreeRow = .Range("A2")
        Else
            Set GetNextFreeRow = .Range("A1").End(xlDown).Offset(1, 0)
        End If
    End With
End Function

For Snickers you would need to add simialr code for Apple i.e.

Private Sub Snickers_Click()
    AddMealToMenu Range("A3")
End Sub

If this works for you, fine, but it may take some setting up and is a brute force approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文