Excel表格:使用单元格值引用其他工作表

rvpgvaaj  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(122)

所以我想在一个不同的工作表(假设是tasksheet 2)中获取单元格的值,而不需要输入“tasksheet 2”。它已经是我的主工作表(tasksheet 1)上的字符串值。我想在几个不同的工作表中这样做,因为我有一整行的工作表名称。
下面是我想做的

Dim SheetNameString as String
Dim SheetWork as Worksheet
Dim i as Integer

for i = 3 to 15
SheetNameString = cells(3,i)
SheetWork = Sheets(SheetNameString)
   If SheetWork.cells(i+4, 8) = blah Then
   Blah blah
   End If

Next

字符串
所以当前工作表,工作表1,第3行是我工作簿中工作表名称的一行。我想遍历这一行,逐个单元格(工作表名称按工作表名称),然后访问该工作表,使用该工作表中的值执行If语句(例如Sheets(“工作表2”).cells(1,1))。
我试图避免做的是键入每个工作表的名称,因为我有~45。
我不知道如何将单元格值(字符串)输入到Sheets(“”)中。以下两种方法都不起作用:

Dim SheetNameString as String
Dim SheetWork as Worksheet

SheetNameString = cells(3,3)
SheetWork = Sheets(SheetNameString)


Dim SheetNameString as String
Dim SheetWork as Worksheet

SheetNameString = cells(3,3)
SheetWork = Sheets("SheetNameString")

pengsaosao

pengsaosao1#

遍历一行昵称


的数据

Sub LoopThroughRow()

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sws As Worksheet: Set sws = wb.Sheets("Master")
    
    Dim srg As Range, scCount As Long
    
    With sws.Range("C3")
        scCount = sws.Cells(.Row, sws.Columns.Count).End(xlToLeft).Column _
            - .Column + 1
        If scCount < 1 Then
            MsgBox "No worksheet names found!", vbExclamation
            Exit Sub
        End If
        Set srg = .Resize(, scCount)
    End With
    
    Dim dws As Worksheet, scell As Range, dwsName As String
    
    For Each scell In srg.Cells
        dwsName = CStr(scell.Value)
        On Error Resume Next
            Set dws = wb.Sheets(dwsName)
        On Error GoTo 0
        If dws Is Nothing Then
            MsgBox "Worksheet """ & dwsName & """ not found!", vbExclamation
        Else
            ' Do your 'Blah' code here e.g.:
            MsgBox "Processing worksheet """ & dwsName & """.", vbInformation
            
            Set dws = Nothing ' reset for the next iteration (worksheet)
        End If
    Next scell
    
End Sub

字符串

相关问题