简体   繁体   中英

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. 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. 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.

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. I suspect a Do Until would be easiest since there are no blanks in Table A until the end of the list. 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.

Please follow the below steps,

  • Define the desitination worksheet/workbook by its range (cSheetName)
  • Define the source worksheet/workbook by its range(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.

'Days Open for - Col:S

'Existing Logic ==$T$1-I(n)+1
'$T$1 - end of month date

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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