Error 13, type mismatch

PGNewbie

New Member
Joined
Feb 6, 2020
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I'm copy certain cell from one sheet to another sheet. My code searches for a value on sheet set as Condition and then searches the Source sheet for the value. Once found it copies specific cells of the row from the Source sheet to a new row on the Target sheet. I'm getting run time error 13, type mismatch. I'm not sure what I've done wrong.
VBA Code:
Sub specific_columns
Dim g As Range
    Dim h As Range
    Dim z As Integer
    Dim Source1 As Worksheet
    Dim Target1 As Worksheet
    Dim Condition1 As Worksheet
    
    
    

    Set Source1 = ActiveWorkbook.Worksheets("Input")
    Set Condition1 = ActiveWorkbook.Worksheets("Top10NoisiestClient")
    
    
       'This will start copying data to Target sheet at row 3
    
        
      For Each g In Condition1.Range("A2:A11") 'specifiy condition
      
      'create worksheet for each value in condition
      
      Set Target1 = Sheets.Add(after:=ActiveSheet)
      
      Target1.Name = "Client-" & g.Value
      
      
        'add code to show client name on line A1
        Target1.Range("A1").Value = g.Offset(, 1).Value
        Target1.Range("A1:K1").Merge
        Target1.Range("A1").Interior.ColorIndex = 45
        
        [A2:K2] = Split("Start_Time Host_Name Client_Name Message Count Probe Source Origin Status End_Time Ack_By")
        
            For Each h In Source1.Range("M" & Rows.Count).End(xlUp)
          
                If h.Value = g.Value Then
          
                    Source1.Cells(h, 5).Copy Target1.Cells(lastrow + 1, 1) '<-- causes error
                    Source1.Cells(h, 11).Copy Target1.Cells(lastrow + 1, 2)
                    Source1.Cells(h, 22).Copy Target1.Cells(lastrow + 1, 3)
                    Source1.Cells(h, 6).Copy Target1.Cells(lastrow + 1, 4)
                    Source1.Cells(h, 8).Copy Target1.Cells(lastrow + 1, 5)
                    Source1.Cells(h, 17).Copy Target1.Cells(lastrow + 1, 6)
                    Source1.Cells(h, 12).Copy Target1.Cells(lastrow + 1, 7)
                    Source1.Cells(h, 13).Copy Target1.Cells(lastrow + 1, 8)
                    Source1.Cells(h, 3).Copy Target1.Cells(lastrow + 1, 9)
                    Source1.Cells(h, 4).Copy Target1.Cells(lastrow + 1, 10)
                    Source1.Cells(h, 21).Copy Target1.Cells(lastrow + 1, 11)
                  
                
               End If
              
            Next h
          
    
    Range("A1").Select
    'Zoom to first cell
    ActiveWindow.ScrollRow = 1
    ActiveWindow.ScrollColumn = 1

        Next g
End Sub
 
I am not sure what you current version of your code looks like, but in the latest version you posted, you were calculating "lastrow" BEFORE your loop.
So, in your loop, if you don't increment the value of lastrow by one at the end of the loop (i.e. lastrow = lastrow + 1), you are just going to keep overwriting the same row over and over.
Thank you so much, this fixed my problem :)
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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