Need Help with VBA Code

Megha1484

New Member
Joined
Nov 10, 2018
Messages
17
Hi,
I have been struggling with this VBA code for quite some time now and would appreciate any help with it. I have attached a sample spreadsheet I am working with. First sheet shows the raw data that comes from the SharePoint and second sheet shows the report I am trying to build form the raw data. Goal is to search for each question (starts at col C) and look for any “No – Correction Needed” and “No – Correction Not Needed” words and display the corresponding “ID”, “Name”, “Notes” associated with the question and “correction made”. I don’t want any “yes” or NA in the report only “Nos*”.
I have filled question “TND” as an example. So, when I run the code it should go through the entire col look for Nos and fill out rest of fields that comes from raw data. Here is the code I am trying to work with.
There are couple of issues with it. First, every time I will run the code it will start from scratch instead of picking up the row where it left off last. This could be an issue overtime when data starts building up.
Second, I am going to have to write the same code 32 times one for each question, which is OK as long as I can make this code work.
Third, I think its not recognizing the new report location where I am trying to build the report.


Sorry, I could not find a way to attach to Excel workbook so copied the sheets at the bottom of the email. First table is Rawdata and second table is Report. Thanks in advance!



Sub Report ()
'builds the report line by line based on iterative check of criteria

'clear old report data
Sheets ("Report").Select
Range("3:3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp

'set up for report build
Range("Report[Question ID]").Select


'iteratively search for selected criteria for Q1

Dim Q1Column As Range
Dim q1answer As Range
Dim q1current As Range


Set Q1Column = Sheets("Rawdata").Range("Table_Rawdata[Memo]")
For Each q1answer In Q1Column.Cells
If q1answer.Value Like "No*" Then
Set q1current = Range(Cells(ActiveCell))
Range("Report[@[Question ID]]").Select
Set ActiveCell.Value = "Q.1"
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = "TD"
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = "Deatiled Question Description"
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = "Memo"
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = Cells(q1current.Row, Range("Table_Rawdata[@[Name]]").Column)
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = q1answer.Value
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = Cells(q1current.Row, Range("Table_Rawdata[@[CorrectionsMade]]").Column)
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = Cells(q1current.Row, Range("Table_Rawdata[@[MemoNotes]]").Column)
ActiveCell.Offset(0, 1).Select
Set ActiveCell.Value = Cells(q1current.Row, Range("Table_Rawdata[@[ID]]").Column)

Else
End If
Next q1answer

'Next datum - copy code for the next question...
End Sub


RawData(Sheet1)

IDNameMemoTNDSTWDecAccConProCoDBegDatDurationCPContributionFIMFMBPRpayOffAccReAccReErrAmtErrAmtICDmetStatusResAccuteBenaccuPETComAAuCplanDMWUSUIOSalPrePdatOUPYAmanClInterWrkstBupWkstLwaiversReviewCCCompTimExtRDMDReview DateCorrectionsMadeMemoNOtesTNDNotesSTWNotesDecAccNotesConProNotesCoDNotesBegDatNotesDurationNotesCPNotesContributionNotesFIMFNotesMBPNotesRpayNotesOffAccNotesReAccNotesReErrNotesAmtNotesErrAmtNotesICNotesDmetNotesStatusNotesResAccuteNotesBenaccuNotesPETNotesComANotesAuNotesCplanNotesDMWUNotesSUIONotesSalPreNotesPdatNotesItem
4567678MHYesNo - Correction NeededN/ANo - Correction NeededNo - Correction NeededNo - Correction Not NeededYesYesN/AYesYesN/AN/ANo - Correction Needed1000000200000YesYesNo - Correction NeededN/AYesYesYesN/AYesN/ANo - Correction Not NeededYesYesN/ANo - Correction NeededNo - Correction Not NeededNo - Correction Not Needed1/30/2019YesdgddItem
67890SGYesNo - Correction Not NeededYesNo - Correction Not NeededNo - Correction Not NeededNo - Correction NeededN/AN/AYesYesYesYesN/ANo - Correction Needed45666750000010000YesN/AYesN/AYesNo - Correction NeededYesYesN/AYesYesNo - Correction Not NeededYesYesYesYesYesN/ANo - Correction Not NeededNo - Correction NeededNo - Correction Not Needed1/16/2019Nofgfgdgdgd​bzdbxb​fbxb​xb​​eytsghsfth​fgfbxbfbxbb​bbxb​xfbxb​​fxgxfxhfbxxfb​Item
45678CLYesNo - Correction NeededNo - Correction NeededNo - Correction Not NeededN/ANo - Correction NeededYesYesYesYesYesN/AYesNo - Correction Needed1500000200000YesYesN/ANo - Correction NeededNo - Correction Not NeededYesYesYesYesN/ANo - Correction NeededYesYesYesNo - Correction NeededNo - Correction Not NeededNo - Correction Not Needed2/13/2019YescvddfgItem
45678DGYesYesYesNo - Correction Not NeededNo - Correction Not NeededNo - Correction NeededN/AN/AYesYesYesYesN/AYes70000050000YesYesNo - Correction Not NeededNo - Correction Not NeededNo - Correction NeededYesYesYesYesYesNo - Correction Not NeededYesYesN/AYesYesYes8/14/2019NocxvxcvxcItem
45678DLYes
No - Correction Not NeededN/ANo - Correction Not NeededNo - Correction Not NeededNo - Correction NeededYesYesYesYesYesYesN/ANo - Correction Not Needed75000010000N/AYesNo - Correction NeededNo - Correction NeededNo - Correction Not NeededYesYesYesYesYesNo - Correction NeededYesYesYesNo - Correction NeededYesYes4/16/2020Yes gdfhhItem
45678GSYesNo - Correction NeededN/ANo - Correction Not NeededNo - Correction NeededNo - Correction NeededYesYesYesYesYesYesYesNo - Correction Needed3000002000N/AYesNo - Correction Not NeededNo - Correction Not NeededNo - Correction NeededYesYesN/AYesYesN/AYesYesYesNo - Correction NeededYesYes9/24/2020NogdfggItem
56789MBYesNo - Correction NeededN/ANo - Correction Not NeededN/ANo - Correction NeededYesN/AYesNo - Correction NeededYesNo - Correction NeededYes60000020000No - Correction Not NeededNo - Correction Not NeededNo - Correction Not NeededNo - Correction NeededYesYesYesNo - Correction NeededNo - Correction NeededNo - Correction NeededNo - Correction Not NeededYesNo - Correction Not NeededN/AYesNo - Correction NeededNo - Correction Not Needed2/4/2021NosdgdsgsdItem
234567CLYesYesNo - Correction Not NeededYesNo - Correction NeededN/AN/ANo - Correction NeededNo - Correction NeededNo - Correction Not NeededYesNo - Correction NeededNo - Correction Not NeededYes10000005000060000YesYesNo - Correction Not NeededNo - Correction Not NeededNo - Correction NeededYesN/AYesYesNo - Correction NeededNo - Correction NeededYesNo - Correction Not NeededN/AYesNo - Correction Not NeededYesNo - Correction NeededNo - Correction Not Needed1/15/2019Nosdgsdg​comments​Comment​Comment
​Comments​Comments​comments​comments​comments​COmment​comments​comments​Comments​CommentsItem
692877NBYesNo - Correction Not NeededNo - Correction NeededN/AYesNo - Correction NeededYesYesYesN/AYesN/AN/A957.19N/AN/AN/AYesN/AYesYesN/AN/AN/AN/AN/AN/AN/AN/AN/AN/A1/21/2019Nosdgsdgs​date corrected to reflect earlier onseti:0#.w|sic\kspierinncm consult neededItem
00325698TG1/15/2019NoerteryItem
693008NBN/AN/AN/AYesN/ANo - Correction NeededN/AN/AN/AN/ANo - Correction NeededN/AN/A1000N/ANo - Correction NeededN/AN/AN/AN/AN/AN/AYesN/ANo - Correction NeededYesYesN/ANo - Correction NeededN/ANo - Correction Not Needed1/23/2019Noeryery​disability date should be 10.1Item
00323333TG
1/8/2019NoeryeryItem
<colgroup><col width="65" style="width: 49pt; mso-width-source: userset; mso-width-alt: 2304;"> <col width="106" style="width: 80pt; mso-width-source: userset; mso-width-alt: 3783;"> <col width="101" style="width: 76pt; mso-width-source: userset; mso-width-alt: 3584;"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;"> <col width="149" style="width: 112pt; mso-width-source: userset; mso-width-alt: 5290;"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;" span="3"> <col width="91" style="width: 68pt; mso-width-source: userset; mso-width-alt: 3242;" span="2"> <col width="149" style="width: 112pt; mso-width-source: userset; mso-width-alt: 5290;" span="2"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;"> <col width="149" style="width: 112pt; mso-width-source: userset; mso-width-alt: 5290;" span="2"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;" span="2"> <col width="83" style="width: 62pt; mso-width-source: userset; mso-width-alt: 2958;"> <col width="73" style="width: 55pt; mso-width-source: userset; mso-width-alt: 2588;"> <col width="63" style="width: 47pt; mso-width-source: userset; mso-width-alt: 2247;"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;" span="6"> <col width="98" style="width: 74pt; mso-width-source: userset; mso-width-alt: 3498;"> <col width="62" style="width: 46pt; mso-width-source: userset; mso-width-alt: 2190;"> <col width="149" style="width: 112pt; mso-width-source: userset; mso-width-alt: 5290;" span="3"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;"> <col width="58" style="width: 44pt; mso-width-source: userset; mso-width-alt: 2076;"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;"> <col width="72" style="width: 54pt; mso-width-source: userset; mso-width-alt: 2560;"> <col width="90" style="width: 67pt; mso-width-source: userset; mso-width-alt: 3185;"> <col width="121" style="width: 91pt; mso-width-source: userset; mso-width-alt: 4295;"> <col width="110" style="width: 82pt; mso-width-source: userset; mso-width-alt: 3896;"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;"> <col width="72" style="width: 54pt; mso-width-source: userset; mso-width-alt: 2560;"> <col width="114" style="width: 85pt; mso-width-source: userset; mso-width-alt: 4039;"> <col width="175" style="width: 131pt; mso-width-source: userset; mso-width-alt: 6229;" span="3"> <col width="95" style="width: 71pt; mso-width-source: userset; mso-width-alt: 3384;"> <col width="125" style="width: 94pt; mso-width-source: userset; mso-width-alt: 4437;"> <col width="98" style="width: 73pt; mso-width-source: userset; mso-width-alt: 3470;"> <col width="128" style="width: 96pt; mso-width-source: userset; mso-width-alt: 4551;" span="2"> <col width="101" style="width: 76pt; mso-width-source: userset; mso-width-alt: 3584;"> <col width="134" style="width: 101pt; mso-width-source: userset; mso-width-alt: 4778;"> <col width="150" style="width: 112pt; mso-width-source: userset; mso-width-alt: 5319;"> <col width="109" style="width: 82pt; mso-width-source: userset; mso-width-alt: 3868;"> <col width="155" style="width: 116pt; mso-width-source: userset; mso-width-alt: 5518;"> <col width="232" style="width: 174pt; mso-width-source: userset; mso-width-alt: 8248;"> <col width="146" style="width: 109pt; mso-width-source: userset; mso-width-alt: 5176;"> <col width="133" style="width: 100pt; mso-width-source: userset; mso-width-alt: 4721;"> <col width="91" style="width: 68pt; mso-width-source: userset; mso-width-alt: 3242;"> <col width="133" style="width: 100pt; mso-width-source: userset; mso-width-alt: 4721;"> <col width="143" style="width: 107pt; mso-width-source: userset; mso-width-alt: 5091;"> <col width="137" style="width: 103pt; mso-width-source: userset; mso-width-alt: 4864;"> <col width="134" style="width: 100pt; mso-width-source: userset; mso-width-alt: 4750;"> <col width="164" style="width: 123pt; mso-width-source: userset; mso-width-alt: 5831;"> <col width="138" style="width: 104pt; mso-width-source: userset; mso-width-alt: 4920;"> <col width="173" style="width: 130pt; mso-width-source: userset; mso-width-alt: 6144;"> <col width="169" style="width: 127pt; mso-width-source: userset; mso-width-alt: 6001;"> <col width="94" style="width: 71pt; mso-width-source: userset; mso-width-alt: 3356;"> <col width="118" style="width: 89pt; mso-width-source: userset; mso-width-alt: 4209;"> <col width="143" style="width: 107pt; mso-width-source: userset; mso-width-alt: 5091;"> <col width="152" style="width: 114pt; mso-width-source: userset; mso-width-alt: 5404;"> <col width="102" style="width: 76pt; mso-width-source: userset; mso-width-alt: 3612;"> <col width="162" style="width: 121pt; mso-width-source: userset; mso-width-alt: 5745;"> <col width="90" style="width: 68pt; mso-width-source: userset; mso-width-alt: 3214;"> <col width="134" style="width: 101pt; mso-width-source: userset; mso-width-alt: 4778;"> <col width="126" style="width: 94pt; mso-width-source: userset; mso-width-alt: 4465;"> <col width="145" style="width: 109pt; mso-width-source: userset; mso-width-alt: 5148;"> <col width="132" style="width: 99pt; mso-width-source: userset; mso-width-alt: 4693;"> <col width="66" style="width: 49pt; mso-width-source: userset; mso-width-alt: 2332;"> <tbody> </tbody>



Report (sheet#2)

PREFILLED FIELDPREFILLED FIELDPREFILLED FIELDPREFILLED FIELDComes from Raw DataComes from Raw DataComes from Raw DataComes from Raw DataComes from Raw Data
Question IDCategoryQUESTIONSlist fieldNameCorrectable/Not CorrectableIDNotesCorrectionMade
Q.2TDDetailed Question DescriptionTNDMHNo - Correction Needed4567678dgddYes
Q.2TDDetailed Question DescriptionTNDSGNo - Correction Not Needed67890fgfgdgdgdNo
Q.2TDDetailed Question DescriptionTNDCLNo - Correction Needed45678cvddfgYes
Q.2TDDetailed Question DescriptionTNDDLNo - Correction Not Needed45678 gdfhhYes
Q.2TDDetailed Question DescriptionTNDGSNo - Correction Needed45678gdfggNo
Q.2TDDetailed Question DescriptionTNDMBNo - Correction Needed56789sdgdsgsdNo
Q.2TDDetailed Question DescriptionTNDNBNo - Correction Not Needed692877sdgsdgsNo
Q.3TDDetailed Question DescriptionSTW
Q.3TDDetailed Question DescriptionSTW
Q.3TDDetailed Question DescriptionSTW
Q.3TDDetailed Question DescriptionSTW
Q.3TDDetailed Question DescriptionSTW
Q.3TDDetailed Question DescriptionSTW
Q.3TDDetailed Question DescriptionSTW
Q.3TDDetailed Question DescriptionSTW
Q.5DADetailed Question DescriptionDecAcc
DADetailed Question DescriptionDecAcc
DADetailed Question DescriptionDecAcc
Q.6DADetailed Question DescriptionConPro
Q.7DADetailed Question DescriptionCoD
Q.8PFADetailed Question DescriptionBegDat
Q.9PFADetailed Question DescriptionDuration
PFADetailed Question DescriptionDuration
Q.10PFADetailed Question DescriptionCP
PFADetailed Question DescriptionCP
Q.12PFADetailed Question DescriptionFIMF
PFADetailed Question DescriptionFIMF
Q.13PFADetailed Question DescriptionMBP
PFADetailed Question DescriptionRpay
Q.14PFADetailed Question DescriptionOffAcc
PFADetailed Question DescriptionOffAcc
Q.15PFADetailed Question DescriptionReAcc
<colgroup><col width="166" style="width: 125pt;" span="2"> <col width="166" style="width: 125pt;" span="7"> <tbody> </tbody>
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Dante,
sorry to bother you again. One small issue. The "Notes" column on Report sheet is returning only TND notes. I tried to change the range instead of referring to a "TNDNotes".column,
but it is not accepting it. Right now, the report gives me pretty much everything I want except for Notes column. I would like TNDNotes displayed next to TND list field, STW notes displayed next to
STW list field etc...
Thanks!!
 
Upvote 0
Change name TNDNotes by the new name, or put the number of column e.g.:


ws2.Cells(j, "H").Value = ws1.Cells(answer.Row, 8)


Code:
Sub Fill_Report()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim answer As Range
    Dim q As Long, j As Long, initial As Long
    
    Application.ScreenUpdating = False
    
    Set ws1 = Sheets("Rawdata")
    Set ws2 = Sheets("Report")
    
    'clear old report data
    ws2.Rows("3:" & Rows.Count).ClearContents
    'ws2.ListObjects("Report").Resize Range("$A$2:$I$3")
    
    q = 1   'Initial question number
    j = 3   'Initial row target
    
    'Column C initial question To column AR end question
    initial = ws1.Range("Table1[#All]").Cells(1, 1).Row
    For i = Columns("C").Column To Columns("AR").Column
        For Each answer In ws1.Range("Table1[" & ws1.Cells(initial, i).Value & "]").Cells
            If answer.Value Like "No*" Then
                ws2.Cells(j, "A").Value = "Q." & q                                                      'question ID
                ws2.Cells(j, "B").Value = "Category"                                                    'category
                ws2.Cells(j, "C").Value = "Deatiled Question Description"                               'question
                ws2.Cells(j, "D").Value = ws1.Cells(1, i).Value                                         'list field
                ws2.Cells(j, "E").Value = ws1.Cells(answer.Row, Range("Table1[[Name]]").Column)  'name
                ws2.Cells(j, "F").Value = answer.Value                                                  'Correctable/Not Correctable
                ws2.Cells(j, "G").Value = ws1.Cells(answer.Row, 1)    'ID
                ws2.Cells(j, "H").Value = ws1.Cells(answer.Row, Range("Table1[[[COLOR=#ff0000]TNDNotes[/COLOR]]]").Column)  'Notes
                ws2.Cells(j, "I").Value = ws1.Cells(answer.Row, Range("Table1[[CorrectionsMade]]").Column)   'CorrectionMade
                j = j + 1
            End If
        Next
        q = q + 1
    Next
    
    Application.ScreenUpdating = True
    
    MsgBox "End"
End Sub[CODE]
[/CODE]
 
Upvote 0
Once again, thanks for the quick reply. I think my last message was little confusing. I did not provide a good explanation of what I was trying to accomplish.
Sorry about that.
So, the code that I had posted originally was meant to run 32 times for each question separately and the line below would have worked perfectly in that case because we knew what question we wanted to pull the information from. But thanks to your modified version I don't have to do that anymore. It saved me a lot of time and effort. Its now looking for wild card "no*" and fills out all the fields from Table1 and it is basically looking for all 32 questions at the same time rather than individually.

All the columns from "Question ID" till "ID" and "Corrections Made" is working perfectly on the Report, except for "Notes" column.
Other columns are easier because they are all fixed values but Notes column is not fixed. Here I would like to look the for the "No*" and fill out the corresponding NOTE in the notes column.

Issue is that "NO*" could appear in any question randomly, which impacts ONLY the "Notes" col in the Report. The code below will pull only notes from TND but if for some reason
the first "No*" appears in some other question then this line won't work. or replacing with a different name or number won't help because we don't know which notes to pull from.

ws2.Cells(j, "H").Value = ws1.Cells(answer.Row, Range("Table1[[TNDNotes]]").Column

so instead of pulling information from this specific column from Table1,it should be a range(only for "Notes"), I think.

For instance, if the first "No*" was found in "DecAcc" (Table1, row 2, Col F), then it would search for all NOTES from col AU to Col BY and pull "DecAccNotes"(Table1, row2, col AX), but if it was found in "ConPro" (Table1, row 3, col G) then
the it will do a search again and pull information from "ConProNotes" (table1, row3, col AY).

I apologize if this is confusing. Please let me know if this helps you understand the issue or if you need more clarification.

Thanks!
https://app.box.com/s/k331pem8w3jypbw3tux2xz65rty7zkwi
 
Upvote 0
Please discard the last post. I figured it out. The solution is a combination of what you had suggested earlier and changing the way row was displayed. Thanks!!


Modified code line - ws2.Cells(j, "H").Value = ws1.Cells(answer.Row, i + 7).Value
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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