我被困在这上面一段时间了,我真的需要一些帮助。我在五个工作表(B,E,L,I,T)中搜索标题为"Account“的列。当找到匹配时,它会执行一个vlookup,将整个列的值带到利差(MasterTab)中的另一个工作表中。效果很好。但是我需要让它成为一个循环,这样它就可以使用550个变量数组(这些是其他列标题)来完成这个过程。
我对循环和基本示例非常陌生,但这个例子似乎很复杂,因为
mf_x_TEXT
变量(匹配函数的字符串)以及每个mf_x
变量(匹配函数本身)进行循环。既然代码本身就是一个循环,那就是三个循环。mf_x
变量依赖于mf_x_TEXT
变量才能工作,因此我不知道如何设置它,以便循环将正确的TEXT
变量正确地放置到正确的mf_x
匹配函数中。这是我的潜艇工作,没有任何尝试得到我说的工作。如果我向你们展示我做循环的尝试,这只会使事情变得更加混乱。mf_Account
和mf_Account_TEXT
是两组550+变量中的一个例子。
Sub GetInfoAltVersion()
'
Dim wsMaster As Worksheet: Set wsMaster = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("MasterTab")
Dim vWSs As Variant: vWSs = Array("B", "E", "L", "I", "T")
'
Dim v As Long
Dim Mrange As Range
Dim Vrange As Range
'
With Workbooks("LBImportMacroTemplate.xlsm")
Set Mrange = Nothing
Set Vrange = Nothing
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
MsgBox lastrow
End With
'
Dim mf_Account_TEXT As String: mf_Account_TEXT = "Account"
'ETC, THERE ARE MANY MORE VARIABLES JUST LIKE THIS, BUT WITH DIFFERENT STRINGS
'
'THIS IS THE PART THAT I NEED TO LOOP FOR EACH VARIABLE
For v = LBound(vWSs) To UBound(vWSs)
If CBool(Application.CountIf(.Sheets(vWSs(v)).Range("A2:ZA2"), mf_Account_TEXT)) Then
Set Mrange = .Sheets(vWSs(v)).Range("A2:ZA2")
Set Vrange = .Sheets(vWSs(v)).Range("A:ZA")
mf_Account = Application.Match(mf_Account_TEXT, Mrange, 0)
'
For i = 2 To lastrow
wsMaster.Cells(i, 2) = Application.VLookup(wsMaster.Cells(i, 1), Vrange, mf_Account, 0)
Next i
Exit For
End If
Next v
Set Mrange = Nothing
Set Vrange = Nothing
'
End With
End Sub
有一件事可以帮助我把application.Match
函数放入vlookup函数中,而不必使它成为一个变量,因为这将消除所需的一个循环。但我无法让vlookup这样工作。我能够做到的唯一方法是将match函数声明为变量,然后将该变量放入vlookup的index_col_num
部分。
我知道程序员不会手动写550次,所以一定有一种超出我目前理解的方法。如果可以的话,请帮帮忙,我快疯了。
发布于 2015-02-16 14:32:27
这将从主表中获取“变量”名称。将它们放在第1行,从第2列开始。假设在其他工作表中查找的值在母版页和其他页中的第一列中。
如果查找值和列位于多个工作表中,则这将用数组中稍后列出的工作表覆盖该值。例如,如果在工作表"B“和工作表"T”中都有查找值"12345“和列名"Account”,那么工作表"T“中的值将显示在数据中。如果需要来自不同工作表的值,则重新排序数组以将最重要的工作表放在最后,否则需要修改代码。
请注意,这可能不是最有效的方法,但它应该能工作。
Sub GetInfoAltVersion()
Dim xlWb As Workbook
Dim wsMaster As Worksheet
Dim vWSs As Variant: vWSs = Array("B", "E", "L", "I", "T")
Dim v As Long
Dim Mrange As Range
Dim Vrange As Range
Dim colName As String
Dim lastCol As Integer
Dim LastRow As Long
Dim AccountCol As Integer
Dim CurrSheet As Worksheet
Set xlWb = Workbooks("LBImportMacroTemplate.xlsm")
Set wsMaster = xlWb.Worksheets("MasterTab")
LastRow = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Row
MsgBox LastRow
lastCol = wsMaster.Cells(1, wsMaster.Columns.Count).End(xlToLeft).Column
MsgBox lastCol
For j = 2 To lastCol
colName = wsMaster.Cells(1, j).Value
For v = LBound(vWSs) To UBound(vWSs)
CurrSheet = xlWb.Sheets(vWSs(v))
If CBool(Application.CountIf(CurrSheet.Range("A2:ZA2"), colName)) Then
Set Mrange = CurrSheet.Range("A2:ZA2")
Set Vrange = CurrSheet.Range("A:ZA")
AccountCol = Application.Match(j, Mrange, 0)
'
For i = 2 To LastRow
wsMaster.Cells(i, j) = Application.VLookup(wsMaster.Cells(i, 1), Vrange, AccountCol, 0)
Next i
End If
Next v
Set Mrange = Nothing
Set Vrange = Nothing
'
Next j
End Sub
希望这能有所帮助。
发布于 2015-02-16 14:35:13
虽然我不能权威地回答特定于VBA的问题,但我可以提供一个通用的编程建议。
如果要迭代的值已经在工作表中,则可以从工作表中收集值。如果没有,则可以创建另一个工作表来存储值列表。
如果由于某些原因无法将数据外部化,则可以简单地创建一个变量(或常量)来将值存储在集合中。根据用例的不同,可以使用列表(顺序)或映射(键值对)类型来存储它们。这将使您能够分别迭代列表或键集。
https://stackoverflow.com/questions/28550456
复制相似问题