【问题标题】:InVBA, need a program to search through a row for a specific value(X), then in specific columns that share same row as X need to print those values(Y)在VBA中,需要一个程序在一行中搜索特定值(X),然后在与X共享同一行的特定列中打印这些值(Y)
【发布时间】:2021-07-30 20:00:22
【问题描述】:

VBA 新手。我正在尝试创建一个宏,它将在 worksheet1 中搜索 A 列中的特定值(即“EM - Easy Money”、“P - Profit”)。随着新报告的上传,这些特定值的行位置可能会发生变化。找到此值后,我需要查看在特定列标题(即“加拿大”、“美国”、“墨西哥”)中与“EM - Easy Money”共享同一行的值 数据集包含额外的列标题,但不是每个列标题都需要,只需这些选择标题。但是,在使用新数据集时,标题的位置可以位于不同的列位置

在此之后,我需要在不同的工作表 (worksheet2) 中的单元格位置打印来自加拿大、美国、墨西哥的值的结果。

这是我迄今为止尝试过的:

Sub dataReport()

'for loop for vertical range (EM & P)

Dim ws1 As Worksheet, ws2 As Worksheet
Dim SrchRng1 As Range, cel1 As Range
Dim emRow As Long, pRow As Long

Set ws1 = Sheets("worksheet1")
Set ws2 = Sheets("workseet2")

Set SrchRng1 = ws1.Range("A15:A100")

For Each cel1 In SrchRng1

    If InStr(1, cel1.Value, "EM - Easy Money") > 0 Then
        emRow = ws1.Range("cel1").Row

    ElseIf InStr(1, cel1.Value, "P - Profit") > 0 Then
        pRow = ws1.Range("cel1").Row

    End If
Next cel1

'for loop for horizontal range followed by if statements for different world segments
Dim SrchRng2 As Range, cel2 As Range

Set SrchRng2 = ws1.Range("B13:BA14")

Dim canadaCol As Long, canadaEM As Range, canadaP As Range

For Each cel2 In SrchRng2

    'Canada

    If InStr(1, cel2.Value, "C_Canada") > 0 Then
        canadaCol = ws1.Range("cel2").Column

        canadaEM = Range(Cells(emRow, canadaCol))
        canadaP = Range(Cells(pRow, canadaCol))

        ws1.Range(canadaEM).Copy
        ws2.Range("F8").PasteSpecial

        ws1.Range(canadaP).Copy
        ws2.Range("F27").PasteSpecial
 
    End If
Next cel2

End Sub

任何帮助将不胜感激

【问题讨论】:

  • 似乎 INDEX/MATCH/MATCH 可以满足您的需求。
  • 你能有一个例子来说明如何做到这一点吗? @ScottCraner
  • =INDEX(worksheet1!$A:$ZZ,MATCH("EM - Easy Money",worksheet1!$A$15:$A$100,0),MATCH("C_Canada",worksheet1!$A$13:$ZZ$13,0))
  • 抱歉,我遇到了一个我似乎无法理解的小问题。 MATCH("C_Canada",worksheet1!$A$13:$ZZ$13,0)) - 由于某种原因,这部分在 excel 中无法解决,即使该值包含在数组中。有什么建议么? @ScottCraner
  • 那么C_Canada中的一个是不同的。

标签: excel vba


【解决方案1】:

嗯,亲爱的王牌, 我宁愿可以识别你向上或向下计数的 var

对于 SrchRng1 中的每个 cel1

If InStr(1, cel1.Value, "EM - Easy Money") > 0 Then
    emRow = ws1.Range("cel1").Row

ElseIf InStr(1, cel1.Value, "P - Profit") > 0 Then
    pRow = ws1.Range("cel1").Row

End If

下一个单元格1

因为它不起作用,并且 Excel 对行和列有一个最大值,如果你的循环超出这些限制,你应该插入你的 VBA 应该做什么。

【讨论】:

    【解决方案2】:

    您应该使用 THISWORKBOOK 完整地设置您的表格

    PRIVATE CONST MaxRow=1048576
    PRIVATE CONST MaxCol=16384
    
    Dim ws1Obj AS Worksheet
    Dim ws2Obj AS Worksheet
    
    Dim RowObj AS Row
    Dim ColObj AS Col
    Dim r, c AS Int
    Dim RowObj AS Range
    Dim ColumnObj AS Range
    Dim CellObj AS Range
    Dim EM AS STRING
    
    SET ws1Obj = Thisworkbook.sheets("ws1")
    EM="em"
    c=1 'This initialation is necesary because of the use of the FOR EACH-Loop
    r=1 'Your rownumber
    SET RowObj = ws1Obj.Range(r,c)
    
    FOR EACH c IN RowObj
    IF c > MaxCol Then EXIT FOR
    IF ws1Obj.Cells(r,c) = EM Then EXIT FOR 'fullfills your Instr-condition
    NEXT
    
    r=1 'This initialation is necesary because of the use of the FOR EACH-Loop 
    IF (c < MaxCol) Then 'found your Instr
    SET ColObj = ws1Obj.cells(r, c)
    FOR EACH r IN ColObj
    IF r > MaxRow
    IF ws1Obj.Cells(r, c) = EM Then EXIT FOR 'Your Instr-condition
    NEXT
    END IF
    
    IF r < MaxRows
    SET ws2Obj = Thisworkbook.sheets("ws2") 'Replace ws2 with the part of cells string
    PRINT ws2Obj
    END IF
    

    【讨论】:

      【解决方案3】:

      我建议像这样打印出你的“ws2”

      PrintOut ws2Obj Copies=1 'This line prints the entire table
      
      'If you want a certain Range in ws2 SET this Range in another Obj
      Dim ws2PrintObj 'Insert this line at the top of the code 
                      'where the other variable   are defined
      
      SET ws2PrintObj=ws2Obj.Range(A1,Q51)
      PrintOut ws2PrintObj
      

      Dontt hesitade to ask more Id 很乐意为您提供更多帮助。

      【讨论】:

        猜你喜欢
        • 2018-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多