Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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 查找列标题、复制数据并将值粘贴到其他工作簿中_Excel_Vba - Fatal编程技术网

Excel 查找列标题、复制数据并将值粘贴到其他工作簿中

Excel 查找列标题、复制数据并将值粘贴到其他工作簿中,excel,vba,Excel,Vba,我试图将一系列数据从一个工作表“a”复制到另一个工作表“B”。我的代码是复制单元格,从“a”复制一些数据并将其粘贴到“a”。。。。我不确定问题是什么 Sub findazuredataandcopyit() Dim WBB As Excel.Workbook Dim WBA As Excel.Workbook Dim Ed As Excel.Worksheet Set WBB = Workbooks("Source.xlsx") Set WBA = Workbooks("MODEL

我试图将一系列数据从一个工作表“a”复制到另一个工作表“B”。我的代码是复制单元格,从“a”复制一些数据并将其粘贴到“a”。。。。我不确定问题是什么

    Sub findazuredataandcopyit()

Dim WBB As Excel.Workbook
Dim WBA As Excel.Workbook
Dim Ed As Excel.Worksheet


Set WBB = Workbooks("Source.xlsx")
Set WBA = Workbooks("MODEL.xlsb")


Dim Col As Long, LastRow As Long
Dim Rngm As Range
Dim RngSku As Range
Dim RngPO As Range
If Application.CountIf(WBB.Sheets("B").Rows(1), "plan_tamaward*") > 0 Then
    Col = Application.Match("plan_tamaward*", WBB.Sheets("B").Rows(1), 0)
    LastRow = WBB.Sheets("B").Cells.Find(what:="*", searchdirection:=xlPrevious, searchorder:=xlByRows).Row
    Set Rngm = Range(Cells(2, Col), Cells(LastRow, Col))
Else
    MsgBox "The column named like plan_tamaward* was not found in Row1.", vbExclamation, "Column Not Found!"
    Exit Sub
End If

'set range for sku

If Application.CountIf(WBB.Sheets("B").Rows(1), "plan_sku_*") > 0 Then
    Col = Application.Match("plan_sku_*", WBB.Sheets("B").Rows(1), 0)
    LastRow = WBB.Sheets("B").Cells.Find(what:="*", searchdirection:=xlPrevious, searchorder:=xlByRows).Row
    Set RngSku = Range(Cells(2, Col), Cells(LastRow, Col))

Else
    MsgBox "The column named like plan_sku* was not found in Row1.", vbExclamation, "Column Not Found!"
    Exit Sub
End If

' set range for PO

If Application.CountIf(WBB.Sheets("B").Rows(1), "plan_sku_*") > 0 Then
    Col = Application.Match("Rack PO #*", WBB.Sheets("B").Rows(1), 0)
    LastRow = WBB.Sheets("B").Cells.Find(what:="*", searchdirection:=xlPrevious, searchorder:=xlByRows).Row
    Set RngPO = Range(Cells(2, Col), Cells(LastRow, Col))
    'do whatever you want to do with this range here
Else
    MsgBox "The column named like Rack PO #* was not found in Row1.", vbExclamation, "Column Not Found!"
    Exit Sub
End If

     MsgBox "the range is" & Rngm.Address
      MsgBox "the range is" & RngSku.Address
       MsgBox "the range is" & RngPO.Address

    WBA.Sheets("Sourcesheet").Range("F4").Resize(Rngm.Rows.Count).Value = Rngm.Value
    WBA.Sheets("Sourcesheet").Range("E4").Resize(RngSku.Rows.Count, 1).Value = RngSku.Value
    WBA.Sheets("Sourcesheet").Range("C4").Resize(RngPO.Rows.Count).Value = RngPO.Value

MsgBox "the range is" & Rngm.Address
      MsgBox "the range is" & RngSku.Address
       MsgBox "the range is" & RngPO.Address
End Sub
代码似乎运行得很好(找到正确的列、分配变量并显示正确的范围)。问题似乎在于这三行:

WBA.Sheets(“Sourcesheet”).Range(“C4”).Resize(RngPO.Rows.Count)。Value=RngPO.Value


感谢您的帮助。

您没有限定您在以下陈述中所指的工作表:

Set Rngm = Range(Cells(2, Col), Cells(LastRow, Col))
因此,这些语句会将范围设置为ActiveSheet上的某个内容

您应该完全限定您的
范围
单元格

Set Rngm = WBB.Sheets("B").Range(WBB.Sheets("B").Cells(2, Col), WBB.Sheets("B").Cells(LastRow, Col))

重写后的代码可能最终看起来像:

Sub findazuredataandcopyit()

    Dim WBB As Excel.Workbook
    Dim WBA As Excel.Workbook
    Dim Ed As Excel.Worksheet

    Set WBB = Workbooks("Source.xlsx")
    Set WBA = Workbooks("MODEL.xlsb")

    Dim Col As Long, LastRow As Long
    Dim Rngm As Range
    Dim RngSku As Range
    Dim RngPO As Range

    'Use a With block to save typing 'WBB.Worksheets("B").' over and over
    With WBB.Worksheets("B")
        'Set LastRow once - no need to do it each time a range needs to be set
        LastRow = .Cells.Find(What:="*", _
                              SearchDirection:=xlPrevious, _
                              SearchOrder:=xlByRows).Row

        If Application.CountIf(.Rows(1), "plan_tamaward*") > 0 Then
            Col = Application.Match("plan_tamaward*", .Rows(1), 0)
            'Fully qualify `Range` and `Cell` (etc) objects
            Set Rngm = .Range(.Cells(2, Col), .Cells(LastRow, Col))
        Else
            MsgBox "The column named like plan_tamaward* was not found in Row1.", vbExclamation, "Column Not Found!"
            Exit Sub
        End If

        'set range for sku
        If Application.CountIf(.Rows(1), "plan_sku_*") > 0 Then
            Col = Application.Match("plan_sku_*", .Rows(1), 0)
            Set RngSku = .Range(.Cells(2, Col), .Cells(LastRow, Col))
        Else
            MsgBox "The column named like plan_sku* was not found in Row1.", vbExclamation, "Column Not Found!"
            Exit Sub
        End If

        ' set range for PO
        'If Application.CountIf(.Rows(1), "plan_sku_*") > 0 Then ' <-- this seems wrong
        If Application.CountIf(.Rows(1), "Rack PO #*") > 0 Then  ' <-- maybe this?
            Col = Application.Match("Rack PO #*", .Rows(1), 0)
            Set RngPO = .Range(.Cells(2, Col), .Cells(LastRow, Col))
        Else
            MsgBox "The column named like Rack PO #* was not found in Row1.", vbExclamation, "Column Not Found!"
            Exit Sub
        End If
    End With

    MsgBox "the range is" & Rngm.Address
    MsgBox "the range is" & RngSku.Address
    MsgBox "the range is" & RngPO.Address

    'Use a With block to save typing 'WBA.Worksheets("Sourcesheet").' 3 times
    With WBA.Worksheets("Sourcesheet")
        .Range("F4").Resize(Rngm.Rows.Count, 1).Value = Rngm.Value
        .Range("E4").Resize(RngSku.Rows.Count, 1).Value = RngSku.Value
        .Range("C4").Resize(RngPO.Rows.Count, 1).Value = RngPO.Value
    End With

    MsgBox "the range is" & Rngm.Address
    MsgBox "the range is" & RngSku.Address
    MsgBox "the range is" & RngPO.Address
End Sub
子findazuredataandcopyit()
将WBB设置为Excel.工作簿
将WBA设置为Excel.工作簿
编辑为Excel.Worksheet
设置WBB=工作簿(“Source.xlsx”)
设置WBA=工作簿(“MODEL.xlsb”)
暗柱和长柱一样长,最后一排和长柱一样长
变暗Rngm As范围
暗RngSku As范围
Dim RngPO As范围
'使用With块反复键入'WBB.工作表(“B”)。'
带WBB.工作表(“B”)
'设置LastRow一次-无需每次都设置范围
LastRow=.Cells.Find(内容:=“*”_
搜索方向:=xlPrevious_
SearchOrder:=xlByRows).行
如果Application.CountIf(.Rows(1),“plan_tamaward*”)大于0,则
Col=Application.Match(“plan_tamaward*”,.Rows(1),0)
'完全限定'Range'和'Cell'(等)对象
设置Rngm=.Range(.Cells(2,Col),.Cells(LastRow,Col))
其他的
MsgBox“在第1行中找不到名为like plan_TAMARDAward*的列”。VBEQUOTE,“找不到列!”
出口接头
如果结束
'设置sku的范围
如果Application.CountIf(.Rows(1),“plan\u sku\u*”)大于0,则
Col=Application.Match(“plan\u sku\u*”,.Rows(1),0)
设置RngSku=.Range(.Cells(2,Col),.Cells(LastRow,Col))
其他的
MsgBox“在第1行中找不到名为like plan_sku*的列”,VBEQUOTE,“找不到列!”
出口接头
如果结束
'设置采购订单的范围

'If Application.CountIf(.Rows(1),“plan\u sku\u*”)>0那么'0 Then'可能是一个
With
子句在这里是合适的;)@A.S.H-LOL-是的,但有时人们认为带
是问题的实际解决方案,而没有意识到完全限定范围(通过带
使其更容易)是解决方案,我不得不重写几行代码(因为带
应该放在这一行之前的几行左右)而我正要出去周六早上购物……所以我决定懒散了,把它放在外面。(但现在我又回到家了,我可能会编辑答案以包含更广泛的代码版本。)哈哈,你说得对。我只想通知OP这个工具,如果他觉得在这类声明中限定所有内容的任务有点乏味的话(通常是这样).aaah…这是有道理的。我从来都不明白为什么人们会使用带有
子句的
。这就清楚了很多!感谢你花时间帮助我了解这个概念,而不是给我解决方案。慢慢地,我在学习。@VBArookie-小心-如果你学得太多,你将需要一个新的用户ID!:D