简体   繁体   English

Excel VBA:将表中的单元格值复制到有条件的其他工作表中的表

[英]Excel VBA: Copy cell value in a table to a table on a different sheet with condition

I'll apologize in advance for the large scope of this question and any vagueness I might create. 对于这个问题的范围之广以及可能造成的任何模糊性,我会事先表示歉意。 I'd like to create a macro that goes down a table and copy a cell value if a particular condition is met in a different column in that row to a table in another sheet. 我想创建一个宏,将宏放在表中,如果在该行的不同列中满足特定条件,则将单元格值复制到另一张表中的表中。

Basically, if Condition equals X in column C in Table A, then copy Number from column B in Table A to column B in Table B. There are 2k+ rows. 基本上,如果条件等于表A中C列中的X,则将Number从表A中的B列复制到表B中的B列。有2k +行。 Column C in Table A is always empty or equals X, and column B in Table A is never empty until the end of the list. 表A中的C列始终为空或等于X,并且表A中的B列始终为空,直到列表末尾。 Column B in Table B is blank and the number of rows will be determined by the number of rows copied to it from Table A. 表B中的B列为空白,行数将由从表A复制到该行的行数确定。

I'm sure this will require a loop, but being new to VBA, I'm not sure which loop type I need and how the loop logic should look. 我确定这将需要一个循环,但是对于VBA来说是新手,所以我不确定我需要哪种循环类型以及循环逻辑的外观。 I suspect a Do Until would be easiest since there are no blanks in Table A until the end of the list. 我怀疑“直到”是最简单的方法,因为表A中直到列表末尾都没有空白。 My best guess so far: 到目前为止,我最好的猜测是:

Dim wsPAPS As Range
Dim wsPAVA As Range
Dim wsVNPN As Range
Dim wkATTR As Workbook

Set wkATTR = Workbooks("PARCEL_ATTR_MACRO-TEST.xlsm")
Set wsPAPS = Sheets("PARCEL_ATTR_BASE").Range("PARCELSTAT")
Set wsPAVA = Sheets("PARCEL_ATTR_BASE").Range("APO_VA_Properties_Vacant_Abandoned")
Set wsVNPN = Sheets("VA_NAME").Range("L1_PARCEL_NBR")

Do
    If wsPAVA = "Vacant/Abandoned" Then
        wsVNPN = wsPAPS
    End IF
Loop Until wsPAPS = ""

Note this code doesn't actually work; 注意,此代码实际上不起作用; I'm told I have a Loop without a Do, not sure why. 有人告诉我我有一个“不做”的循环,不知道为什么。

I would be incredibly grateful for any help offered on this. 对于在此方面提供的任何帮助,我将万分感谢。 Thanks, all! 谢谢大家!

UPDATE The intent with the Range variables is to attempt to make referencing specific columns easy, but I get an application-defined or object-defined error for them. 更新 Range变量的目的是试图简化对特定列的引用,但是我得到了针对它们的应用程序定义或对象定义的错误。

Please follow the below steps, 请按照以下步骤操作,

  • Define the desitination worksheet/workbook by its range (cSheetName) 通过范围(cSheetName)定义目标工作表/工作簿
  • Define the source worksheet/workbook by its range(cFileLocWS) 通过范围(cFileLocWS)定义源工作表/工作簿

  • Take row count for the operation to be performed can be upto 1 million rows. 要执行的操作的行数最多可以达到一百万行。

  • Add the loop not necessarily do while a simple "For" would do. 添加循环不一定是简单的“ For”就能做到的。

'Days Open for - Col:S '开放日-Col:S

'Existing Logic ==$T$1-I(n)+1 '现有逻辑== $ T $ 1-I(n)+1
'$T$1 - end of month date '$ T $ 1-月底日期

lRowCount = Sheets(cSheetName).Cells(Rows.Count, "I").End(xlUp).Row
For rowIndex = 2 To lRowCount
    mDate = Sheets(cFileLocWS).Range(cMonthEndDate).Value
    strtDate = Sheets(cSheetName).Cells(rowIndex, "I").Value
    lResult = (mDate - strtDate) + 1
    Sheets(cSheetName).Cells(rowIndex, "S").Value = lResult
Next rowIndex

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

Try this: 尝试这个:

Assuming the sheet names are: "TableA" and "TableB" and Column B always has some data. 假设工作表名称为:“ TableA”和“ TableB”,并且列B始终具有一些数据。

Sub Copy()
    Dim lr As Long, r As Long
    Set Sh1 = ThisWorkbook.Worksheets("TableA")
    Set Sh2 = ThisWorkbook.Worksheets("TableB")        

    lr = Sh1.Cells(Rows.Count, "B").End(xlUp).row
    x = 2
    For r = 2 To lr
        If Range("C" & r).Value = "X" Then 'Evaluate the condition.
            Sh2.Range("B" & x).Value = Sh1.Range("B" & r).Value 'ColumnB
            x = x + 1
        End If
    Next r
    Sh2.Select
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM