Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

Why is my data not pasted into worksheets when I created the worksheets beforehand using VBA?

$
0
0

In relation to the thread that I have asked earlier, How to solve the "program not responding error" when i extract data with same name in a column and put them into different worksheets? , the earlier thread is about distinguishing the data and paste them into worksheets namely index 1, index 2 and index 3 but that is the case where I created the worksheets in excel.

So to add on to the earlier thread, I want to program them beforehand using VBA instead of creating them through excelsheets. So the additional stuff is the inclusion of sheets.add for index 1,2 and 3. However, the extracted data did not paste into those indexes worksheet even when I created them in VBA already.

  Sub UpdateVal()
    Static count As Long
    Dim iRow As Long
    Dim aRow As Long
    Dim a As Long
    Dim b As Long
    Dim selectRange As Range
    Dim lastline As Integer
    Dim sheetname As String
    Dim indexrowcount As Integer
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = wb.Worksheets("Result")
    Set site_ai = Sheets.Add(after:=Sheets(Worksheets.count))
    site_ai.Name = "Index1"
    Set site_bi = Sheets.Add(after:=Sheets(Worksheets.count))
    site_bi.Name = "Index2"
    Set site_ci = Sheets.Add(after:=Sheets(Worksheets.count))
    site_ci.Name = "Index3"




    j = 2
    iRow = 1
    lastline = ws.UsedRange.Rows.count
    While iRow < lastline + 1
        a = iRow + 1
        b = iRow + 17 ' Max Group Size with Same name in F to H column
        count = 1
        If ws.Cells(iRow, "F").Value = "Martin1" Then
            sheetname = "Index1"
        ElseIf ws.Cells(iRow, "F").Value = "John1" Then
            sheetname = "Index2"
        Else
            sheetname = "Index3"
        End If
        For aRow = a To b
            If ws.Cells(iRow, "F") = ws.Cells(aRow, "F") And ws.Cells(iRow, "G") = ws.Cells(aRow, "G") And ws.Cells(iRow, "H") = ws.Cells(aRow, "H") Then
                count = count + 1
            Else
                Set selectRange = Range("A"& iRow & ":J"& aRow - 1)
                selectRange.Copy
                indexrowcount = Sheets(sheetname).UsedRange.Rows.count
                Sheets(sheetname).Range("A"& indexrowcount).PasteSpecial xlPasteAll
                iRow = iRow + count
                Exit For
           End If
        Next aRow
    Wend
End Sub

Viewing all articles
Browse latest Browse all 88868


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>