VBA to move data if cell is greater than 1

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
824
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Would someone be able to help me modify the below so column Q that starts on Q11 gets transferred (A-Q) for to sheet WsALL if the cell is greater than 1?

VBA Code:
'transfer data over to Compare tab
  vCols = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) '<- Columns of interest in specified order
  With WsSec
    With .Range("A11:Q" & .Range("H" & rows.count).End(xlUp).row)
            vRows = Application.Index(.Cells, Evaluate("row(1:" & .rows.count & ")"), Array(17))
            For ii = 1 To UBound(vRows)
                If Len(vRows(ii, 1)) > 1 Then
                    kk = kk + 1
                    vRows(kk, 1) = ii
        End If
      Next ii
        nrALL = WsALL.Range("A" & rows.count).End(xlUp).row + 1
        WsALL.Range("A" & nrALL).Resize(kk, UBound(vCols) + 1).Value = Application.Index(.Cells.Value, Application.Index(vRows, 0, 1), vCols)
    End With
  End With

it errors here with runtime 1004
VBA Code:
        WsALL.Range("A" & nrALL).Resize(kk, UBound(vCols) + 1).Value = Application.Index(.Cells.Value, Application.Index(vRows, 0, 1), vCols)
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I'm not sure exactly what you're doing with this - seeing some actual data would help - but from what I understand from your description and my limited testing it errors if there are no rows with a value in column Q.

You could fix that by testing for the value of the variable kk, like so:

VBA Code:
    'transfer data over to Compare tab
    vCols = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) '<- Columns of interest in specified order
    With WsSec
        With .Range("A11:Q" & .Range("H" & Rows.Count).End(xlUp).Row)
            vRows = Application.Index(.Cells, Evaluate("row(1:" & .Rows.Count & ")"), Array(17))
            For ii = 1 To UBound(vRows)
                If Len(vRows(ii, 1)) > 1 Then
                    kk = kk + 1
                    vRows(kk, 1) = ii
                End If
            Next ii
            nrALL = WsALL.Range("A" & Rows.Count).End(xlUp).Row + 1
            If kk > 0 Then
                WsALL.Range("A" & nrALL).Resize(kk, UBound(vCols) + 1).Value = Application.Index(.Cells.Value, Application.Index(vRows, 0, 1), vCols)
            End If
        End With
    End With
 
Upvote 0
So I solve for that previous to the code by doing this, therefore skipping if no 1 in column Q:

VBA Code:
Set rng = .Range("Q11:Q" & lr1)
End With

If Application.WorksheetFunction.CountIf(rng, ">1") > 0 Then

Column Q used to be "Yes" but that data now is changing coming to me so I tried to modify accordingly just not sure if I am doing so properly. Q now will have numerical numbers any number greater than 0.

old code with Yes
VBA Code:
If vRows(i, 1) = "Yes" Then

I just want to transfer all data from A-Q if Q has a number greater than 1 in it to WsAll next open row. Hope that helps clarify. The data is large so sharing is difficult but if you need a mockup of like 5 rows i can try to produce one for you and send via XLBB
 
Upvote 0
My code is very large so I am going to just send the snipbit that pertains to what we are trying to do here

WsSec is the source data
WsAll is the destination
WsCus is used to loop through the WsSec program

tax lot cons2.xlsx
ABCDEFGHIJKLMNOPQ
1
2
3
4
5
6
7
8D9AC L T QCBCOCCURUCBUYUQCS
9
10DateIndicatorAccountSKU Open Date Lot # QTYLocal CostBase CostOrig CostCurrencyUnit CostBuyerUnique CodeRounded Unit CostFormulaOccurrence
1122-MayAABCG123456781/1/2020110100100100USD10JohnZ10_G12345678102
1222-MayAABCG123456781/1/2021220200200200USD10Tomx10_G12345678202
1322-MayAABCG123456789/1/1999330999999999USD33.3Sallyc33.3_G12345678301
1422-MayAABCG123456781/6/1989440665665665USD16.625Donnav16.63_G12345678401
1522-MayAABCG1234567810/15/2022550140140140USD2.8Timf2.8_G12345678501
1622-MayAABCG123456786/6/1996660356356356USD5.933333333Abigails5.93_G12345678601
1722-MayAABCG123456787/8/1990770985098509850USD140.7142857Mikes140.71_G12345678701
1822-MayAABCG123456784/9/2009880158415841584USD19.8Chirstinew19.8_G12345678801
1922-MayAABCG1234567811/11/2005990458745874587USD50.96666667Sabrinat50.97_G12345678901
2022-MayAABCG1234567810/1/200810100458745874587USD45.87Johnt45.87_G123456781001
Source
Cell Formulas
RangeFormula
O11:O20O11=ROUND(L11,2)
P11:P20P11="_"&D11&G11


VBA Code:
t = Timer
' Run loop for range, clear, run, copy and paste into its respective sheet
  i = 0
  Do Until WsCus.Range("FILTER").Offset(i, 0) = ""
    FILTER = WsCus.Range("FILTER").Offset(i, 0)

'apply filter to start loop and activate sheet
With WsSec
    .Range("F4") = FILTER
    .Application.Calculation = xlManual
    .Activate
    .Range("A10").CurrentRegion.Delete
End With
Debug.Print "Worksheets(WsSec)Delete... :" & Format(Timer - t, "0.00") & " seconds"

t = Timer
    Call SecurityDistribution2
Debug.Print "Worksheets(WsSec)... :" & Format(Timer - t, "0.00") & " seconds"
 
'after execution copy from source and paste into sheet
With WsSec
lr1 = .Cells(rows.count, "A").End(xlUp).row 'find the maximum row
.Range("O10:Q10") = Array("Rounded 2 digit Unit Cost", "For formula", "Same Occurrence")
        .Application.Calculation = xlAutomatic
        .Range("P11:P" & lr1).NumberFormat = "General"
        .Range("O11:O" & lr1).Formula = "=Round(K11, 2)"
If TERMFLG = "L" Then
        .Range("P11:P" & lr1).Formula = "=""_""&D11&O11"
Else
        .Range("P11:P" & lr1).Formula = "=""_""&D11&E11&O11"
End If
        .Range("P11:P" & lr1).NumberFormat = "@"
        .Range("O11:P" & lr1).Value = .Range("O11:P" & lr1).Value
t = Timer
'countif formula
  Set dic = CreateObject("Scripting.Dictionary")
  a = .Range("P11:P" & lr1)
  ReDim b(1 To UBound(a, 1), 1 To 1)
  For k = 1 To UBound(a, 1)
      dic(a(k, 1)) = dic(a(k, 1)) + 1
  Next
  For k = 1 To UBound(a, 1)
    b(k, 1) = dic(a(k, 1))
  Next
        .Range("Q11").Resize(UBound(b, 1)).Value = b
        .Range("R11:R" & lr1).Formula = "=If(Q11>1,""Yes"",""No"")"
        .Range("R11:R" & lr1).Value = .Range("R11:R" & lr1).Value
Set rng = .Range("R11:R" & lr1)
End With

If Application.WorksheetFunction.CountIf(rng, "Yes") > 0 Then

'transfer data over to Compare tab
  vCols = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) '<- Columns of interest in specified order
  With WsSec
    With .Range("A11:R" & .Range("H" & rows.count).End(xlUp).row)
            vRows = Application.Index(.Cells, Evaluate("row(1:" & .rows.count & ")"), Array(17))
            For ii = 1 To UBound(vRows)
                If vRows(ii, 1) > "1" Then
                    kk = kk + 1
                    vRows(kk, 1) = ii
        End If
      Next ii
        nrALL = WsALL.Range("A" & rows.count).End(xlUp).row + 1
        WsALL.Range("A" & nrALL).Resize(kk, UBound(vCols) + 1).Value = Application.Index(.Cells.Value, Application.Index(vRows, 0, 1), vCols)
    End With
  End With
vRows = vbNullString
vCols = vbNullString
    End If

Debug.Print "CountIf Formula/delete... : " & Format(Timer - t, "0.00") & " seconds"
 
'apply filter to start and activate sheet
With WsSec
    .Application.Calculation = xlManual
    .Activate
    .Range("A10").CurrentRegion.Delete
End With
 
    i = i + 1
Loop
 
Last edited:
Upvote 0
Apologies for the delay. It works fine for me. I used your data and this code subset:
VBA Code:
Sub test()
    Dim vCols, vRows
    Dim WsSec As Worksheet, WsALL As Worksheet
    Dim nrALL As Long, ii As Long, kk As Long
    Set WsSec = Sheets("Sheet1")
    Set WsALL = Sheets("Sheet2")
    'transfer data over to Compare tab
    vCols = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) '<- Columns of interest in specified order
    With WsSec
        With .Range("A11:R" & .Range("H" & Rows.Count).End(xlUp).Row)
            vRows = Application.Index(.Cells, Evaluate("row(1:" & .Rows.Count & ")"), Array(17))
            For ii = 1 To UBound(vRows)
                If vRows(ii, 1) > "1" Then
                    kk = kk + 1
                    vRows(kk, 1) = ii
                End If
            Next ii
            nrALL = WsALL.Range("A" & Rows.Count).End(xlUp).Row + 1
            WsALL.Range("A" & nrALL).Resize(kk, UBound(vCols) + 1).Value = Application.Index(.Cells.Value, Application.Index(vRows, 0, 1), vCols)
        End With
    End With
End Sub

Also works if I change this
VBA Code:
 If vRows(ii, 1) > "1" Then
to this
VBA Code:
 If vRows(ii, 1) > 1 Then
 
Upvote 0
Solution
Thanks for getting back to me I realized my issue was I am looping through that code and I am not clearing the variants after it was complete. That was causing me problems so I just added this at the end of the loop and it works

VBA Code:
kk = 0
ii = 0
rng = 0
 
Upvote 0
@myall_blues do you know if there is a size limit in VBA? I have a big data file and when trying to transfer over I get this error? im not at the excel row limit though so not sure what is going on

1717504903932.png
1717504915271.png


at this spot
VBA Code:
            WsALL.Range("A" & nrALL).Resize(kk, UBound(vCols) + 1).Value = Application.Index(.Cells.Value, Application.Index(vRows, 0, 1), vCols)
 
Upvote 0
I'm going to start a new thread may be separate unrelated to the thread / new issue
 
Upvote 0
Yes that would be best. I don’t know the answer so hopefully one of the big guns will chime in.
 
Upvote 0

Forum statistics

Threads
1,217,371
Messages
6,136,174
Members
449,996
Latest member
duraichandra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top