我是 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