0

I am seeking assistance with copying and pasting specific cells (in columns 1, 5, 21, 27, 29, 231) from one sheet if a condition ('lockout' word is used in column 29) is met and paste them in the second sheet.

Here is how I started from watching other videos on Youtube - I'm totally lost!

Private Sub CommandButton1_Click()
a = Worksheets("Circuit Data").Cells(Rows.Count, 1).End(xlUp).Row

For i = 8 To a

    If Worksheets("Circuit Data").Cells(i, 29).Text = "Lockout" Then

        Worksheets("Circuit Data").Cells(i, 1, 5, 21, 27, 29, 231).Copy
        Worksheets("Lockout-Est. Cost of Care").Activate
        b = Worksheets("Lockout-Est. Cost of Care").Cells(Rows.Count, 1).End(xlUp).Row
        Worksheets("lockout-est. Cost of Care").Cells(b + 1, 1).Select
        ActiveSheet.Paste
        Worksheets("Circuit Data").Activate

End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Circuit Data").Cells(1, 1).Select

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
  • I do not recommend trying to use Youtube to learn how to code for many reasons. – braX Jan 09 '20 at 00:26
  • When you paste them you expect them to be in the same column numbers? – Ricardo Diaz Jan 09 '20 at 00:35
  • I would encourage you to read a decent VBA tutorial to understand basic concepts like loops, variables and conditionals. It will be hard to explain to you how to do this without this type of knowledge. If it has to be Youtube, try WiseOwl. – SnowGroomer Jan 09 '20 at 00:46
  • Thanks for the tips. I'm an attorney in the social work field, not trying to become a professional coder - I am trying to track children who are abandoned by their families and the factors surrounding the circumstances so I can advocate for funding to implement more programs to help these families. I am trying to put together some excel tracking with my basic excel knowledge but when I have the time I will try to learn more about VBA :) – Jo Villarroel Jan 09 '20 at 02:18

1 Answers1

0

This is my approach to solve it.

Please read the code's comments and adjust it to fit your needs.

Public Sub CopyData()

    ' Define the object variables
    Dim sourceWorksheet As Worksheet
    Dim targetWorksheet As Worksheet

    ' Define other variables
    Dim searchString As String

    Dim lastSourceRow As Long
    Dim startSourceRow As Long
    Dim lastTargetRow As Long
    Dim sourceRowCounter As Long
    Dim columnToEval As Long
    Dim columnCounter As Long

    Dim columnsToCopy As Variant

    ' Adjust the worksheets names
    Set sourceWorksheet = ThisWorkbook.Worksheets("Circuit Data")
    Set targetWorksheet = ThisWorkbook.Worksheets("Lockout-Est. Cost of Care")

    ' Define the number of columns to copy from one sheet to the other
    columnsToCopy = Array(1, 5, 21, 27, 29, 231)

    ' Set the string you're going to evaluate
    searchString = "Lockout"

    ' Adjust the initial row where data is going to be evaluated
    startSourceRow = 8

    ' Adjust the column where you evaluate if condition is met
    columnToEval = 29

    ' Find the number of the last row in source sheet (notice that this search in column A = 1)
    lastSourceRow = sourceWorksheet.Cells(sourceWorksheet.Rows.Count, 1).End(xlUp).Row

    For sourceRowCounter = startSourceRow To lastSourceRow

        ' Evaluate if criteria is met in column 29
        If sourceWorksheet.Cells(sourceRowCounter, columnToEval).Value = searchString Then

            ' Get last row on target sheet (notice that this search in column A = 1)
            lastTargetRow = targetWorksheet.Cells(targetWorksheet.Rows.Count, 1).End(xlUp).Row

            For columnCounter = 0 To UBound(columnsToCopy)

                ' You don't need to use copy and paste if values is all that you're passing
                targetWorksheet.Cells(lastTargetRow, columnsToCopy(columnCounter)).Offset(1, 0).Value = sourceWorksheet.Cells(sourceRowCounter, columnsToCopy(columnCounter)).Value

            Next columnCounter

        End If

    Next sourceRowCounter

    ' If this is necessary...
    sourceWorksheet.Activate

End Sub

EDIT:

Added:

  1. Search for multiple strings
  2. Define different columns in targetWorksheet than sourceWorksheet

New code (with comments):

Option Explicit

Public Sub CopyData()

    ' Define the object variables
    Dim sourceWorksheet As Worksheet
    Dim targetWorksheet As Worksheet

    ' Define other variables
    Dim searchStrings() As String ' -> Updated to hold multiple values

    Dim lastSourceRow As Long
    Dim startSourceRow As Long
    Dim lastTargetRow As Long
    Dim sourceRowCounter As Long
    Dim columnToEval As Long
    Dim columnCounter As Long
    Dim searchCounter As Long ' -> New

    Dim columnsToCopy As Variant
    Dim columnsDestination As Variant

    ' Adjust the worksheets names
    Set sourceWorksheet = ThisWorkbook.Worksheets("Circuit Data")
    Set targetWorksheet = ThisWorkbook.Worksheets("Lockout-Est. Cost of Care")

    ' Define the number of columns to copy from one sheet to the other
    columnsToCopy = Array(1, 5, 21, 27, 29, 231)
    columnsDestination = Array(1, 2, 3, 4, 5, 6) ' -> This must have the same items' quantity as columnsToCopy

    ' Set the string you're going to evaluate
    searchStrings = Split("Lockout,DJJ lockout", ",") ' -> Here the values are separated by commas in one single string (be careful of spaces between commas)

    ' Adjust the initial row where data is going to be evaluated
    startSourceRow = 8

    ' Adjust the column where you evaluate if condition is met
    columnToEval = 29

    ' Find the number of the last row in source sheet (notice that this search in column A = 1)
    lastSourceRow = sourceWorksheet.Cells(sourceWorksheet.Rows.Count, 1).End(xlUp).Row

    For sourceRowCounter = startSourceRow To lastSourceRow

        ' New -> Where need to iterate through each of the values in the search string array
        For searchCounter = 0 To UBound(searchStrings)

            ' Evaluate if criteria is met in column 29
            If sourceWorksheet.Cells(sourceRowCounter, columnToEval).Value = searchStrings(searchCounter) Then

                ' Get last row on target sheet (notice that this search in column A = 1)
                lastTargetRow = targetWorksheet.Cells(targetWorksheet.Rows.Count, 1).End(xlUp).Row

                For columnCounter = 0 To UBound(columnsToCopy)

                    ' You don't need to use copy and paste if values is all that you're passing
                    ' -> New See that I replaces the first columnsToCopy for columnsDestination
                    targetWorksheet.Cells(lastTargetRow, columnsDestination(columnCounter)).Offset(1, 0).Value = sourceWorksheet.Cells(sourceRowCounter, columnsToCopy(columnCounter)).Value

                Next columnCounter

            End If

        Next searchCounter

    Next sourceRowCounter

    ' If this is necessary...
    sourceWorksheet.Activate

End Sub
Ricardo Diaz
  • 5,658
  • 2
  • 19
  • 30
  • Do I copy the beginning section as is minus the grayed out comments referring to "DIM"? – Jo Villarroel Jan 09 '20 at 02:55
  • I think I may have this part mis-calculated - where you point out "notice this search in column A=1" my source worksheet data starts on row 8 and runs through row 186. So I would like for the formula to evaluate each row and if it contains the word "lockout", then copy those specified cells from the that row and (the columns as indicated) to the other sheet - Lockout-Est. Cost of care sheet. – Jo Villarroel Jan 09 '20 at 03:01
  • PS I really am grateful for your help! This will save me a ton of time in the long run! – Jo Villarroel Jan 09 '20 at 03:02
  • I was refering for that it searches in column A. The 1 is the column number – Ricardo Diaz Jan 09 '20 at 03:21
  • _"Do I copy the beginning section"_ : Depending on where you're going to run the macro, if it's from a button, just copy the complete code, from the words `Public Sub` to `End Sub` and then, assign the macro to that button. See this [article](https://support.office.com/en-us/article/assign-a-macro-to-a-form-or-a-control-button-d58edd7d-cb04-4964-bead-9c72c843a283) – Ricardo Diaz Jan 09 '20 at 10:50
  • _So I would like for the formula to evaluate each row and if it contains the word "lockout"_ : The macro does exactly that in column 29 (columnToEval variable). When I say that it _searchs in column A_ is that it is looking in that column to find the last non empty cell. If that works, just leave it that way, if not, change the _1_ to the column number you want to find the last non empty row. – Ricardo Diaz Jan 09 '20 at 10:55
  • I read the article and was able to add the button. The vba code is amazing! Last two questions. (1) so I am going to distinguish between "MH Lockout" and "DJJ lockout" - how do I add the additional search string to distinguish the two lockouts? (2) how to make the data paste to the target sheet (Lockout -Est. Cost of Care) columns 1, 2, 3, 4, 5? (vs. pasting to columns 1,5, 21, 27, 29 on the target worksheet)? I am grateful for your time and assistance! – Jo Villarroel Jan 13 '20 at 05:12
  • Hi Jo, (1) You could add another variable and an OR statement in the IF or (see my edit) store values in an Array and evaluate them (2) This one requires another variable to store the column numbers (see columnsDestination in my edit). Please re-read the code's comments where I explain what I did. – Ricardo Diaz Jan 13 '20 at 18:42
  • Hey Ricardo, this is absolutely fantastic! I am so excited and looking forward to efficient data tracking! I am truly very grateful for all of your time and assistance (and patience)! Thank you so much and wishing you all the very best in the new year! – Jo Villarroel Jan 14 '20 at 06:07
  • Remember you can always press F8 and step through the code if you need to adjust it, and turn on the macro recorder to see how other things are done. Cheers! – Ricardo Diaz Jan 14 '20 at 07:14