Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在VBA中写入单元格_Excel_Vba - Fatal编程技术网

Excel 在VBA中写入单元格

Excel 在VBA中写入单元格,excel,vba,Excel,Vba,我想遍历工作簿中的所有页面,然后写入所有这些单元格。我想说的是,给定一个单元格,我当前在一个特定的工作表中写入该特定单元格。我如何才能继续这样做,使“this worked”的cell=“foo”写入相同的单元格代码,但在新的工作表中 Sub WorksheetLoop() Dim ws As Worksheets beginning Dim starting_ws As Worksheet Set starting_ws = ActiveSheet

我想遍历工作簿中的所有页面,然后写入所有这些单元格。我想说的是,给定一个单元格,我当前在一个特定的工作表中写入该特定单元格。我如何才能继续这样做,使“this worked”的cell=“foo”写入相同的单元格代码,但在新的工作表中

  Sub WorksheetLoop()

     Dim ws As Worksheets beginning
     Dim starting_ws As Worksheet
     Set starting_ws = ActiveSheet 
     ws_num = ThisWorkbook.Worksheets.Count
     For I = 1 To ws_num
        ThisWorkbook.Worksheets(I).Activate
        For Each Cell In Range("G9:G39").Cells
           If Cell = "foo" Then
           Sheets("mysheet").Cell.Value = "this worked"
           Cell = "this worked"
           Exit For

        Next

     Next


  End Sub
考虑:

Sub WorksheetLoop()
     ws_num = ThisWorkbook.Worksheets.Count
     For I = 1 To ws_num
        ThisWorkbook.Worksheets(I).Activate
        Range("G9:G39").Value = "this worked"
     Next
  End Sub
您不需要在工作表中的单个单元格上循环,也不需要激活

Sub WorksheetLoop2()
     ws_num = ThisWorkbook.Worksheets.Count
     For I = 1 To ws_num
        With ThisWorkbook.Worksheets(I)
            .Range("G9:G39").Value = "this worked"
        End With
     Next
  End Sub
您可能需要:

Sub WorksheetLoop()
    Dim sht As Worksheet
    Dim cell As Range

    For Each sht In ThisWorkbook.Worksheets ' loop through sheets
        If sht.Name <> "mysheet" Then ' avoid considering "mysheet" sheet
            For Each cell In sht.Range("G9:G39") ' loop through current sheet range "G9:G39"
                If cell.Value = "foo" Then Sheets("mysheet").Range(cell.Address).Value = "this worked" ' if current cell value is "foo" then write "This worked" in corresponding cell of "mysheet" sheet
            Next
        End If
    Next
End Sub
子工作表loop()
将sht变暗为工作表
暗淡单元格作为范围
对于此工作簿中的每个sht。工作表在工作表中循环
如果sht.Name为“mysheet”,则“避免考虑”mysheet“表
对于sht.范围(“G9:G39”)中的每个单元格,通过当前工作表范围“G9:G39”循环
如果cell.Value=“foo”,则在“mysheet”工作表的相应单元格中写入“this worked”。范围(cell.Address)。Value=“this worked”’如果当前单元格值为“foo”,则在“mysheet”工作表的相应单元格中写入“this worked”
下一个
如果结束
下一个
端接头

您需要指定范围对象所引用的工作表。对于便利店来说,每个工作范围的左上角范围也是如此

Sub WorksheetLoop()
    Dim i As Long, j As Long, ws_num As Long
    ws_num = ThisWorkbook.Worksheets.Count

    Dim ws As Worksheet, rng As Range
    Dim my_ws As Worksheet, my_rng As Range
    Set my_ws = Sheets("mysheet")
    Set my_rng = my_ws.Range("G9")

    For i = 1 To ws_num
        Set ws = ThisWorkbook.Worksheets(i)
        Set rng = ws.Range("G9")
        ' Go down 30 cells from G9
        For j = 1 To 30
            If rng.Cells(j, 1).Value = "foo" Then
                my_rng.Cells(j, 1).Value = "this worked"
                rng.Cells(j, 1).Value = "this worked"
            End If
        Next j
    Next
End Sub

注意
ws
指向
工作表(“mysheet”)
时会发生什么情况?

不清楚您实际想做什么。每个
单元格
包含特定工作簿中特定工作表中特定单元格的地址。您可以使用相同的
单元格
对象写入两个不同的工作表。@barskyn如果这个答案没有价值,我将删除它。键入:您的意思是
.Range(“G9:G39”).value=“这个有效”
。注意
范围()
之前的
,或者它的作用域是
活动工作表
@ja72,您是对的!我来修理。。。。。。。。。。。。。。。。。。。。。谢谢在这种情况下,这个点有很大的不同。另外,在发布之前尝试测试代码。我完全同意你的看法。