0

In the code below, my LastRow variable is not storing the right row number on the 27th loop (i = 27) causing the code to malfunction

I have used the F8 step through multiple times and noticed that the issue is on the 27th loop. The LastRow variable is meant to be +1204 rows from the previous LastRow value on each iteration of the loop, so I was expecting LastRow = 32509 instead of LastRow = 31316. For reference, on the 26th loop, LastRow = 31305. I'm not sure why the it is finding the wrong LastRow when the code has worked for the first 26 loops.

I am trying to get from my Source Table to my Desired Table:

Source Table enter image description here

to

Desired Table

enter image description here

Also , the final error that shows is:

Run-Time error '1004': Application -defined or object- defined error

Sub Populate_entity()        
Dim i As Integer    
i = 1    
Dim LastRow  As Long
Dim SearchText As String   

Do While i < 122 ' go across entity (columns wise)    
    If i = 1 Then   
        Range("E1").Select    
        Selection.Copy                                 
        SearchText = ActiveCell.Value
        ActiveCell.End(xlToLeft).Select                 'snap to left (cell A1)
        ActiveCell.Offset(0, 2).Select                  'move to cell C1
        ActiveCell.Offset(1, 0).Select                   ' move to cell C2
    Else            
        ActiveCell.Offset(0, i + 1).Select    
        Selection.Copy
        SearchText = ActiveCell.Value
        ActiveCell.End(xlToLeft).Select
        ActiveCell.Offset(0, 2).Select
        ActiveCell.End(xlDown).Select
        ActiveCell.Offset(1, 0).Select            
    End If                
    ActiveSheet.Paste
    ActiveCell.Offset(1203, 0).Select        
    ActiveSheet.Paste
    ActiveCell.End(xlUp).Select
    ' ======== Error here ========
    LastRow = Cells.Find(What:=SearchText, After:=ActiveCell, LookIn:=xlValues, SearchOrder:=xlByRows).Row
    Range("C" & ActiveCell.Row & ":C" & LastRow).FillDown
    ActiveCell.End(xlUp).Select       
    i = i + 1    
Loop    
End Sub
omega
  • 45
  • 9
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/143021/discussion-on-question-by-david-cheong-integer-not-storing-correct-lastrow-value). – Bhargav Rao Apr 30 '17 at 12:26

2 Answers2

0

In my opinion you don't need any search because your code always places the SearchString in row 1205. Since you know that it is there you don't need to look for it. This thought brings me to the code below.

Sub Populate_Entity()

    Dim C As Long                                   ' Column
    Dim Target As Range
    Dim FirstRow As Long
    Dim LastRow  As Long

    FirstRow = 2
    LastRow = 7     '1205
    C = 3
    Range("C2").value = Range("E1").value
    ' Cells(2, C).Value = Cells(1, 5).Value

    Do
        Set Target = Range(Cells(FirstRow, C), Cells(LastRow, C))
        Target.FillDown

        C = C + 1
        Cells(2, C).value = "Can't figure"

    Loop While C < 3    ' 122

End Sub

I have cut the loop short to only 7 rows (instead of 1205) and 3 columns (instead of 122). I just couldn't figure out where the text in the FirstRow should come from. For column C it comes from E1, but where does it come from in the subsequent columns? You can fill this in using the method I showed you above, like, Cells(2, C).Value = Cells(1, 5).Value. I believe that the 5 can be replaced by a value derived from the current C, perhaps C + 2.

Note the Cells(2, C).Value doesn't refer to the value in cell C2. Instead if refers to the cell in Row 2, Column C.

Variatus
  • 14,293
  • 2
  • 14
  • 30
0

A summary of what you want, as you described in the comments:

Copy the values from cells E1:DU1, paste each cell 1204 times in column C.

  • 1st loop it will paste cell E1 in C2:C1205
  • 2nd loop it will paste cell F1 to C1206:C2409
  • etc.

This code achieves that:

Sub Populate_entity()

    ' Declare 2 range variables (top row to copy from and paste destination)
    Dim RowRange As Range
    Dim PasteCells As Range
    ' Use the With block to specify the sheet. If you want the destination
    ' to be another sheet, then you can specify that instead: 
    ' ThisWorkbook.Sheets("SheetName").Range("...") 
    With ThisWorkbook.ActiveSheet
        Set RowRange = .Range("E1:DU1")     ' Set range to copy from
        Set PasteCells = .Range("C2:C1205") ' Set paste cells, blocks of 1204 cells in column C
    End With
    ' Loop through RowRange, copy each cell's value into PasteCells
    ' Then offset the PasteCells range by 1024 rows, so next RowRange cell
    ' is inserted underneath previously copied cells.
    Dim ofst As Long
    For ofst = 1 To RowRange.Cells.Count
        ' Use .Value to avoid the (comparably slow) copy/paste operation 
        PasteCells.Offset((ofst - 1) * 1204, 0).Value = RowRange.Cells(ofst).Value
    Next ofst

End Sub
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • Thanks, it worked like a Charm; code is so concise. I feel like an idiot having spent 4-5 hours making and troubleshooting my code. My first time on VBA, maybe thats why! Although I am still itching to find out what was causing the LastRow issue in my code! – omega Apr 27 '17 at 15:21
  • @David, R.e. what caused your error... You are using `.Find` without specifying `LookAt:=xlPart` or `LookAt:=xlWhole`. It may be defaulting to `xlPart`, in which case it would be doing a *partial* match on a previously pasted value. This would give you an incorrect row. Try adding `LookAt:=xlWhole` to the `.Find` statement. [Docs here](https://msdn.microsoft.com/en-us/library/office/ff839746.aspx) – Wolfie Apr 27 '17 at 15:29
  • 1
    you are correct, i tried xlWhole and it works now. Except that my code runs in 15 seconds while yours took 1-2 secs. thanks again!!! appreciate the help – omega Apr 27 '17 at 15:37
  • @David, that time disadvantage probably comes from 2 things. 1) all of the selecting/using ActiveCell instead of defining a range. 2) Using `.Value` is *much* quicker than using the clipboard (copy/paste). You can also use `.Formula` in future if you want to copy the formula not the value. Happy VBA-ing – Wolfie Apr 27 '17 at 15:45
  • definitely, will learn to use .Value more. Part of the reason why my code is structured like that was because i used the record macro function for 2 iterations to get a feel for what VBA code is required, and than I generalised the recorded macro. – omega Apr 27 '17 at 16:13
  • @David, nothing wrong with using the Macro Recorder to see what functions are available, it uses `Select` a lot though, which [should be avoided](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) and there are some [best practices](http://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices/11274/always-define-and-set-references-to-all-workbooks-and-sheets#t=201704271618503351679) to go by too. – Wolfie Apr 27 '17 at 16:19