1

我是 vba 新手,也是我第一次使用“Select Case”。简而言之,我试图通过查找“cust_num”列标题并遍历每一行来循环(行数将在一张纸之间变化),如果 cust_num 与特定行的标准匹配,则“Barco”将被放入“公司名称”列下的同一行。

当我编译时,“Barco”只放在第一行下面,所以似乎没有循环遍历每一行,下面的示例。

XX278        Barco
XX004   
XX004   
XX278   
XX004   
XX004   
XX278   
XX278   





Dim Nu As Range
    Dim cmpny As Range
    Dim v As Integer
    Dim y As Integer

    v = ActiveSheet.Rows(1).Find("customer_name", LookAt:=xlPart).End(xlDown).Count - 1 
'count number of rows

    Set Nu = ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart) 'set Nu = cust_num column header
    Set cmpny = ActiveSheet.Rows(1).Find("company name", LookAt:=xlPart) 'set cmpny = company name column

    For y = 0 To v 'loop through each row
            Select Case Nu.Offset(1 + y, 0).Value 'row 1 + y of "cust_num"
            Case "XX004", "XX278", "XX318" 'if "cust_num" row = these #'s
                 cmpny.Offset(1 + y, 0).Value = "Barco" 'Then corresponding row under "company name" column = "Varco"
            End Select
    Next
4

1 回答 1

0

在@TimWilliams 的帮助下,我的代码现在可以工作了。这使用 Select Case 函数循环指定的行并确定该单元格是否满足条件,如果满足,则在条件单元格右侧的同一行中插入名称(在本例中为 cust_num)。

Dim Nu As Range
Dim cmpny As Range
Dim v As Integer
Dim y As Integer

v = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row 'count number of rows 
'count number of rows

Set Nu = ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart) 'set Nu = cust_num column header
Set cmpny = ActiveSheet.Rows(1).Find("company name", LookAt:=xlPart) 'set cmpny = company name column

For y = 0 To v 'loop through each row
        Select Case Nu.Offset(1 + y, 0).Value 'row 1 + y of "cust_num"
        Case "XX004", "XX278", "XX318" 'if "cust_num" row = these #'s
             cmpny.Offset(1 + y, 0).Value = "Barco" 'Then corresponding row under "company name" column = "Varco"
        End Select
Next
于 2013-06-25T13:06:45.547 回答