简体   繁体   English

VBA用于检查行并将返回条件的单元格返回到另一个工作表

[英]VBA for checking rows and returning cells that match criteria to another sheet

I'm struggling trying to develop an automated solution for the following challenge (ie apart from a button press, no user intervention): I have a master 'sheet that contains Accrual figures in monthly columns and an associated Receipt Number for this in a column immediately to the right. 我正在努力开发针对以下挑战的自动化解决方案(例如,除了按按钮之外,无需用户干预):我有一个主工作表,该工作表的每月列中包含应计数字,并在列中包含与此相关的收据编号立即向右。 The Receipt columns have the month in the form: 'Feb', 'Jun', 'Dec' at the top. 收据栏的月份格式为:“ Feb”,“ Jun”,“ Dec”在顶部。

I need to return a variety of cell data to another summary 'sheet in the workbook from each row that matches the following criteria: 我需要从符合以下条件的每一行中,将各种单元格数据返回到工作簿中的另一个“摘要”工作表:

  1. Identify the Receipt column based on a match of the month with an entered month in cell 'x' in the summary 'sheet, eg if I type 'Mar', find the column headed 'Mar' in the master 'sheet. 根据月份与“摘要”表中单元格“ x”中输入的月份的月份匹配来确定“收据”列,例如,如果我键入“ Mar”,请在主“表”中找到标题为“ Mar”的列。
  2. Ignoring blank rows (ie there are data in Column A), if the cell in the first row of the Receipt column is blank, identify the row, select 6 different cells and return the contents to 6 specific cells in the first blank row (from a given row number) in the summary 'sheet – then move onto the next row in the column and continue this process until the end (or a row limit). 忽略空白行(即列A中有数据),如果“收据”列第一行中的单元格为空白,则标识该行,选择6个不同的单元格,然后将内容返回到第一行空白行中的6个特定单元格(从摘要表中的给定行号),然后移至该列的下一行,并继续此过程,直到结束(或行数限制)为止。 NB: the 'blank' cell will have a fill colour (conditionally formatted). 注意:“空白”单元格将具有填充颜色(有条件格式化)。

If the cell contains data, move to the next row down and continue the process. 如果单元格包含数据,请移至下一行并继续该过程。 A VBA routine I can attach to an onscreen button to update the results each time would do the job nicely. 我可以将VBA例程附加到屏幕上的按钮上,以便每次更新结果都可以很好地完成工作。 Although I've picked up bits of really useful code here, eg for checking each row for given conditions, I can't get my head around a working solution to include returning the relevant cells to the summary 'sheet... 尽管我在这里获得了一些非常有用的代码,例如用于检查给定条件下的每一行,但是我仍然无法解决一个可行的解决方案,包括将相关单元格返回到摘要表中。

Any and all feedback greatly appreciated. 任何和所有反馈非常感谢。

(Sorry - can't post images / screenshots yet...) (抱歉-尚无法发布图片/屏幕截图...)


Update Thanks for the quick response, and apols I couldn't append/paste-in a sample of the spreadsheet - apparently I need 10 points... :) 更新感谢您的快速响应,Apols无法追加/粘贴电子表格示例-显然我需要10分... :)

Slightly Different summary sheet (Sheet1). 汇总表(Sheet1)略有不同。 The columns are: Month (Col A), then 4 cells (TBD) to be returned to cols BE, then Receipt No (Col F). 列为:月(Col A),然后是4个单元格(TBD)要返回到cols BE,然后是收据编号(Col F)。 The data come from each identified row in the master sheet (Sheet4) and are placed in the next available blank row in Sheet1. 数据来自主表(Sheet4)中每个已标识的行,并放置在Sheet1中的下一个可用空白行中。 I can mod any examples given to match the actual positions. 我可以修改任何示例以匹配实际位置。

I have the criteria for selecting the row (this is from a loop I've got working to identify the row, but using a fixed column reference [26 - Col Z] for a specific month): 我有选择行的标准(这是从我已经确定行的循环中进行的,但是在特定月份使用固定的列引用[26-Col Z]):

If Not Cells(TheRow, 3).Value = "" And Cells(TheRow, 26).Value = "" And Cells(TheRow, 6).Value < Cells(TheRow, 25).Value Then... (do the other bit I'm stuck on) 如果不是,则Cells(TheRow,3).Value =“” And Cells(TheRow,26).Value =“” And Cells(TheRow,6).Value <Cells(TheRow,25).Value然后...(执行我被卡住了)

Hope this is a bit clearer. 希望这一点更加清楚。 Appreciate your help. 感谢您的帮助。


Can't add a comment, or chat (!) so further update: 无法添加评论或聊天(!),因此需要进一步更新:

Thanks Tony The month column in the summary is to confirm the month reported on, as this will change when the user overwrites the source cell for the month they wish to parse the master sheet with. 谢谢Tony摘要中的month列用于确认报告的月份,因为当用户覆盖要与之一起解析母版表的月份的源单元格时,此列将更改。 The person this data goes to will get these 7 columns and nothing else. 此数据转到的人将获得这7列,仅此而已。 The mechanism has to find the Receipt col (month), then identify that row entry is blank AND col 3 on that row is not blank AND that the Accrual value [Rng.Column - 1] is greater than the PO value (Col 4) on that row. 该机制必须找到收据列(月),然后确定行条目为空,并且该行的列3不为空,并且应计值[Rng.Column-1]大于PO值(列4)在那排。 Once these criteria are established, the Receipt, Accrual, PO and 3 other data are selected, copied and pasted into the summary sheet on the next available blank row. 一旦建立了这些标准,就可以选择收据,应计,采购订单和其他3个数据,将其复制并粘贴到摘要表中的下一个可用空白行上。

The master sheet has Accrual and Receipt for each month, so there is only one column to identify. 母版表每个月都有应计和收据,因此只有一栏可以标识。

Hope this helps... 希望这可以帮助...

I am struggling to visualise you worksheets. 我正在努力使您的工作表可视化。 I see the master sheet as something like: 我将主表视为:

 ... |Accrual|  Jan|Accrual|  Feb|Accrual|  Mar|Accrual|  Apr| ...
 ... |   1.23|A12  |       |     |  23.67|A14  |       |     |
 ... |       |     |  56.78|C34  | 178.12|C56  |       |     |
 ... |   2.34|B678 |       |     |       |     | 123.82|B821 |
 ... |       |     |       |     |  96.52|D56  |       |     |

Somewhere within the Summary sheet there is a list of months of interest. 在“摘要”表中的某处,有一个感兴趣的月份列表。

I need a variable: 我需要一个变量:

Dim MonOfInt As String

and a loop within which a month of interest will be loaded from the worksheet into this variable: 和一个循环,其中一个月的时间将从工作表加载到此变量中:

With Sheets("Summary")
  MonOfInt = .Cells(R, C).Value
End With

The following is a possible skeleton for the code to move values for the month of interest: 以下是代码移动感兴趣月份的值的可能框架:

Dim ColCrnt As Long
Dim Rng As Range
Dim RowCrnt As Long
Dim RowLast As Long
  :

With Sheets("Master")
  ' Look for month of interest in row 1
  Set Rng = .Rows(1).Find(MonOfInt)
End With

If Rng Is Nothing Then
  ' No columns for this month in Master

Else
  ' Accrual and Receipt columns for this month found
  ColCrnt = Rng.Column - 1        ' Accrual column for month
  ' Find last used row in Accrual column
  RowLast = .Cells(Rows.Count, ColCrnt).End(xlUp).Row
  For RowCrnt = 2 To RowLast

    ' Code to extract values from Master and move to Summary here

  Next

  End If

End With

If you can add information to your question to confirm my visualisation and to give more information about (1) how to detect is a row is blank (other than testing the whole row which is an option) and (2) the source and destination of the six cells then I will try to pad out this answer. 如果您可以在问题中添加信息以确认我的可视化并提供有关以下方面的更多信息:(1)如何检测某行是否为空白(除了测试整个行(可选)之外,以及(2)该行的来源和目标六个单元格,然后我将尝试填补这个答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA将符合条件的单元格复制到另一张工作表 - VBA copy cells that meet criteria to another sheet VBA将满足条件的行复制到另一个工作表 - VBA copy rows that meet criteria to another sheet Excel VBA用于隐藏一个工作表中的单元格(如果它们匹配另一个工作表中的单元格) - Excel VBA for hiding cells in one sheet if they match cells in another sheet 遍历填充的行,根据条件匹配,使用 VBA 将值添加到 Excel 中另一个工作表中的特定单元格 - Iterate through populated rows, Match based on criteria, add value to specific cell in another sheet in Excel using VBA 如果列中的行范围与另一工作表中的列的范围相匹配,则 VBA 会清除行中单元格的内容 - VBA clear contents from cells in row if range of rows in column match range from column in another sheet VBA-根据多个条件从另一张纸复制单元格 - VBA - copy cells from another sheet based on multiple criteria VBA附加-根据条件将单元格从一张纸复制到另一张纸 - VBA Append - copy cells froma sheet to another based to a criteria VBA使用3条条件将行从一张纸复制到另一张纸 - VBA to copy rows from one sheet to another using 3 criteria VBA将符合条件的行复制到仅粘贴值的另一张工作表 - VBA copy rows that meet criteria to another sheet pasting only values VBA将满足条件的多行复制到另一张工作表 - VBA copy mutlipe rows that meet criteria to another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM