excel 具有2个偏移的循环

lb3vh1jj  于 2022-12-14  发布在  其他
关注(0)|答案(2)|浏览(130)

做了相当多的研究,我不能完全弄清楚这一点。这是一个工作簿,在工作表“报价”和工作表“导出”之间搜索
此脚本应

  • 找到后,在报价表中开始搜索“PartNum”
  • 下移2个单元格
  • 复制新活动单元格中的值
  • 在工作表“export”中搜索该值
  • 找到后,将值向右偏移24个单元格
  • 返回工作表“报价”
  • 查找“交付周期”
  • 向下移动2个单元格并粘贴值

我被卡住的零件,我没有按照我的意愿正确地写循环,我如何才能准确地循环每一次的Partnum和Leadtime下一行?有没有什么方法我可以添加,使它忽略如果一个零件没有找到,而不是错误?

'Find PartNum
Worksheets("Quote").Activate
Cells.Find(What:="PartNum").Offset(2, 0).Select

'Copy/search part Num

   Dim str1 As String
    Dim Cntr As Integer
    Cntr = 0
    Do While Cntr <= 650

      Cntr = Cntr + 1
      str1 = ActiveCell.Value
      Selection.Copy

      Worksheets("Export").Activate
      ActiveCell.Select
      Cells.Find(What:=str1, After:=ActiveCell, LookIn:= _
        xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
      Cells.FindNext(After:=ActiveCell).Activate
      ActiveCell.Offset(0, 24).Range("A1").Select
      Selection.Copy

      Worksheets("Quote").Activate
     
     
'Find PartNum
Cells.Find(What:="Leadtime").Offset(2, 0).Select
      Selection.PasteSpecial Paste:=xlPasteValues
jdg4fx2g

jdg4fx2g1#

有些事情要尝试。
回复:我如何准确地循环一行降低每一次都为Parth编号和Leadtime?

Use ".Offset(3, 0).Select"

回复:未找到部件
将“单元格.查找(What:=“PartNum”).偏移量(2,0).选择”替换为:

Dim PN As Range
Set PN = Cells.Find(What:="PartNum")

If PN Is Nothing Then
    MsgBox ("Part Number is not found")
    'Exit Sub?
End If

PN.Offset(3, 0).Select
a5g8bdjr

a5g8bdjr2#

根据我所能收集到的从您张贴的代码,但需要一些澄清围绕什么你正在做的“出口”表...

Option Explicit

Sub CopyData()

    Dim wsQuote As Worksheet, wsExport As Worksheet, wb As Workbook
    Dim str1 As String, f As Range, f2 As Range
    
    'use an explicit workbook reference
    Set wb = ThisWorkbook 'or ActiveWorkbook for example
    Set wsQuote = wb.Worksheets("Quote")
    Set wsExport = wb.Worksheets("Export")

    Set f = FindWholeValue(wsQuote.Cells, "PartNum")
    If f Is Nothing Then Exit Sub
    
    str1 = f.Offset(2, 0).Value 'two rows down from "PartNum"

    '------------------
    'Can't figure out what you're doing on the Export sheet...
    '
    '  Set f2 = ???
    '------------------
    
    Set f = FindWholeValue(wsQuote.Cells, "Leadtime")
    If f Is Nothing Then Exit Sub
    
    f.Offset(2, 0).Value = f2.Value
    
End Sub

'Search `rng` for `v` and return the first matched cell
'If not found and `WarnIfMissing` is True, warn the user
Function FindWholeValue(rng As Range, v, Optional WarnIfMissing As Boolean = True) As Range
    Set FindWholeValue = rng.Find(what:=v, lookat:=xlWhole, LookIn:=xlFormulas)
    If FindWholeValue Is Nothing And WarnIfMissing Then
        MsgBox "Value '" & v & "' not found on sheet '" & rng.Parent.Name & "'"
    End If
End Function

相关问题