0

I wrote some pretty simple VBA (excel macros) code to manage my audio licencing excel experience. The code is supposed to look through the excel sheet in column 3, look for any that have "AMC" in their column, and then copy and paste the row to sheet 2 and continue searching through entire excel document. This code is very simple and worked once right before it stopped working right. It only takes the very last AMC value and puts that on sheet 2 but not the other 5 rows that have AMC in their column 3 value.

Please help! I would appreciate it very much :)

-Jeremy

VBA Code:

Sub CommandButton1_Click()

    a = Worksheets("Sheet1").UsedRange.Rows.Count

    b = 0

    For i = 2 To a

        If Worksheets("Sheet1").Cells(i, 3).Value = "AMC" Then

            Worksheets("Sheet1").Rows(i).Copy

            Worksheets("Sheet2").Activate

           ' b = ActiveSheet.UsedRange.Rows.Count

            Worksheets("Sheet2").Cells(b + 1, 1).Select

            ActiveSheet.Paste

            Worksheets("Sheet1").Activate

        End If

    Next

    Application.CutCopyMode = False

    ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub
2
  • Continuing search through the entire document or just column C sheet 3? Commented Jun 27, 2018 at 5:35
  • It seems that you are not incrementing your b variable. You always select b+1 and b is always 0. Commented Jun 27, 2018 at 5:39

2 Answers 2

1

This should solve your problem :

  If Worksheets("Sheet1").Cells(i, 3).Value = "AMC" Then    
        Worksheets("Sheet1").Rows(i).Copy  
        Worksheets("Sheet2").Activate   
        Worksheets("Sheet2").Cells(b + 1, 1).Select    
        b = b + 1    
        ActiveSheet.Paste   
        Worksheets("Sheet1").Activate        
  End If
Sign up to request clarification or add additional context in comments.

3 Comments

Oh my god... I cant believe I wasn't kicking the variable b to the next value.... Thank you SO MUCH! Seriously. I feel kind of like an idiot now haha.
You are welcome, glad i could help. But to be honest take a look at answer from @QHarr. Doing things more efficiently should be your goal :)
@JeremyF Btw you can mark the answer if it solved your problem ;) meta.stackexchange.com/questions/147531/…
1

You could use Instr and Union.

  1. Union is very efficient as you store all the qualifying ranges in memory and then write out only once to the sheet. Much less expensive operation than continually writing out to the sheet.
  2. Instr allows you to use vbBinaryCompare which means you are doing a case sensitive match i.e. only AC not ac will be matched on.
  3. The code belows avoids .Activate, which is again an expensive operation that isn't required.
  4. UsedRange means you may be looping many more rows than required. You only want to loop to the last populated row in column C of sheet 1, as that is the column you are testing. Hence, I use .Cells(.Rows.Count, C").End(xlUp).Row to find that last row.
  5. Use Option Explicit - research why! It will make your VBA life soooooo much better.

Code:

Option Explicit    
Sub CommandButton1_Click()

    Dim lastRow As Long, sSht As Worksheet, tSht As Worksheet, loopRange As Range
    Set sSht = ThisWorkbook.Worksheets("Sheet1")
    Set tSht = ThisWorkbook.Worksheets("Sheet2")

    With sSht
        Set loopRange = .Range("C2:C" & .Cells(.Rows.Count, C").End(xlUp).Row)
    End With

    Dim rng As Range, unionRng As Range
    For Each rng In loopRange
        If InStr(1, rng.Value, "AC", vbBinaryCompare) > 0 Then
            If Not unionRng Is Nothing Then
                Set unionRng = Union(unionRng, rng)
            Else
                Set unionRng = rng
            End If
        End If
    Next rng
    If Not unionRng Is Nothing Then unionRng.EntireRow.Copy tSht.Cells(1, 1)    
End Sub

2 Comments

Thank you so much for your input but the former person had already figured it out. Thank you so much for trying :) Seriously, I appreciate you both very much.
I'm pleased. I would give some serious thought to the points I raise above if you want to write more efficient code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.