如何编写在另一个工作表中查找单元格中列标题的VBA代码,然后返回相应的列?[关闭]


0

因此,我有一个完整的客户电子表格,其中客户作为列,产品作为行,交叉单元格指示每个客户购买了多少。在另一个工作表中,我希望能够运行一个宏,该宏将提取所有产品并列出仅为一个客户购买的数量。

我还有另一个宏,用于删除空白行,仅保留客户购买的产品。


1
欢迎来到超级用户。不幸的是,我们不是代码编写服务。与其简单地要求执行特定任务的代码,不如向我们展示您到目前为止已经尝试过的内容(包括您当前拥有的任何代码)以及遇到的困难,以便我们可以帮助您解决特定的问题。仅要求代码的问题范围太广,很可能被搁置或关闭
DavidPostill

看起来像乔布斯!
Raystafarian

Answers:


0

使用以下数据:

在此处输入图片说明

运行此宏:

Sub prodList()
Dim cust As String, rLook As Range, msg As String
Dim ct As Variant, r As Range
cust = Application.InputBox(Prompt:="Enter Customer Name", Type:=2)
Set rLook = Range("1:1").Find(What:=cust, After:=Range("A1")).EntireColumn

msg = ""
For Each r In rLook.Cells
    pr = Cells(r.Row, 1).Value
    If pr = "" Then Exit For
    ct = CStr(r.Value)
    If ct <> "" Then
        msg = msg & vbCrLf & pr & vbTab & ct
    End If
Next r

MsgBox msg
End Sub

将显示:在此处输入图片说明

编辑#1:

要将输出存储在Sheet2上,请使用以下宏:

Sub prodList2()
    Dim cust As String, rLook As Range, K As Long
    Dim ct As Variant, r As Range
    cust = Application.InputBox(Prompt:="Enter Customer Name", Type:=2)
    Set rLook = Range("1:1").Find(What:=cust, After:=Range("A1")).EntireColumn

    K = 1
    For Each r In rLook.Cells
        pr = Cells(r.Row, 1).Value
        If pr = "" Then Exit For
        ct = CStr(r.Value)
        If ct <> "" Then
            Sheets("Sheet2").Cells(K, 1) = pr
            Sheets("Sheet2").Cells(K, 2) = ct
            K = K + 1
        End If
    Next r
End Sub

谢谢,这是我想要的,但是有没有办法在excel的另一张纸上获得该输出呢?这只是我实际要处理的数据的精简版。我计划获得引用产品和编号以及进行其他计算的信息。
格雷格

@Greg见我的EDIT#1
加里的学生
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.