lundi 30 janvier 2017

Populate records in a new worksheet based on values in another worksheet VBA

What I am trying to do is populate a new worksheet only with records based on certain values in a particular column on another sheet. As of now I have been able to populate the entire sheet without the condition to populate only the records that I want.

There is a column in the worksheet "Full View" named 'Update Status' (Column C) that has the values No Change, Updated, New, Closed. I need to only select those records in my new worksheet that I am populating below with only those records that have values such as No Change, Updated, New in the 'Update Status' Column. However, when I run this code, it gives me a blank workboook even though there are values in the full view workbook that has other values than closed in column C. Can someone please help? Thanks for your help in advance!

 Sub Scatterplot()

 Dim headers() As Variant
 Dim ws As Worksheet
 Set ws = Worksheets("Scatterplot Excel Template")

 'Clean Contents
 ws.Cells.ClearContents
 ws.Cells.Interior.ColorIndex = 0

 Sheets("New Risk Template").Range("B3:B4").ClearContents

 'Assign headers
  headers = Array("Record ID", "ID", "Title", "Summary", "Primary Risk     Type", "Secondary Risk Type", _
 "Primary FLU/CF Impacted", "Severity Score", "Likelihood Score", "Structural Risk Factors")

 With ws    
 For I = LBound(headers()) To UBound(headers())
  .Cells(1, 1 + I).Value = headers(I)
  Next I

  Dim book1 As Worksheet
  Dim lookFor As Range
  Set book1 = Worksheets("Full View")
  Set lookFor = book1.Range("B2:X1000")
  Dim row_count As Integer
  Dim col_count As Integer

  'Find the last row and column number
  Dim col_name As String
  Dim record_id As String
  Dim col_index As Integer
  row_count = book1.Range("C" & Rows.Count).End(xlUp).Row

  If book1.Cells(row_count, "C") = "Updated" And book1.Cells(row_count, "C") = "No Change" And book1.Cells(row_count, "C") = "New" Then

  'Loop for input values
  For I = 2 To row_count

  ws.Cells(I, 1).Value = book1.Cells(I + 1, 2).Value
  ws.Cells(I, 2).Value = Right(ws.Cells(I, 1).Value, 4)

  For j = 3 To 10
  On Error Resume Next
  col_name = ws.Cells(1, j)
  record_id = ws.Cells(I, 1)

  col_index = Sheets("Full View").Cells(2, 1).EntireRow.Find  (What:=col_name, _
  LookIn:=xlValues, LookAt:=xlWhole, _
  SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
  MatchCase:=False).Column

  ws.Cells(I, j).Value = Sheets("Full View").Cells(I + 1, col_index).Value
  Next
  Next
  End if

Aucun commentaire:

Enregistrer un commentaire