在VBA中,我知道可以像这样循环工作表:
for i = 1 to 5
msgbox worksheets(i).cells(1,1)
next
从工作簿中拉出前5张(按从左到右的顺序)。我如何参考表格的代号呢?正如在工作簿中的第一个工作表可能是Sheet10,第二个Sheet6等...所以,如果我试图使用工作表#s(代号)循环,那可能吗?
答案 0 :(得分:3)
您可以创建自己的工作表集合,您可以使用代码名称对其进行索引。这个功能可以做到:
class DeviceFilterController extends Controller
{
public function filter(Feature $feature){
$marks = isset($_POST["mark"]) ? $_POST["mark"] : null;
$feature = $feature->newQuery();
if(isset($marks ))
{
foreach ($marks as $value)
{
$feature->where('device_mark', $value);
}
}
return $feature->get();
}
}
然后您可以将它用于索引,如下所示:
Function SheetsByCodeName() As Collection
Dim sh As Worksheet
Set SheetsByCodeName = New Collection
For Each sh In ThisWorkbook.Worksheets
SheetsByCodeName.Add sh, sh.CodeName
Next
End Function
答案 1 :(得分:1)
CodeName上没有索引器,因此您必须自己遍历表单。 一个非常基本的例子是这样的:
Public Sub FindSheets()
Dim i As Integer
Dim objSheet As Worksheet
For i = 1 To 5
Set objSheet = FindSheetByName("Sheet" & i)
If objSheet Is Nothing Then
MsgBox "No sheet with codename Sheet" & i
Else
MsgBox objSheet.Name & " has codename " & objSheet.CodeName
End If
Next
End Sub
Function FindSheetByName(ByVal v_strCodeName As String) As Worksheet
Dim objSheet As Worksheet
Set FindSheetByName = Nothing
For Each objSheet In ActiveWorkbook.Sheets
If objSheet.CodeName = v_strCodeName Then
Set FindSheetByName = objSheet
Exit Function
End If
Next
End Function
答案 2 :(得分:1)
不可以,不能按照CodeName索引表 您可以循环所有工作表并检查其CodeName,也可以使用单独的方法:
Sub processSheet(ws As Worksheet)
Debug.Print ws.Cells(1)
End Sub
Sub process()
processSheet(Sheet10)
processSheet(Sheet11)
processSheet(Sheet12)
End Sub
另一种选择可能是(未经测试):
For Each ws In Array(Sheet10, Sheet11, Sheet12)
Debug.Print ws.Cells(1)
Next
答案 3 :(得分:-2)
如果纸张名为Sht1,Sht2,,, Sht5 然后
for i = 1 to 5
msgbox worksheets("Sht" & i).cells(1,1)
next