My team and I have created a consolidator tool that consolidates data from worksheets uploaded using a button. However, there's an added enhancement that our leads would like to have.
I don't know if it's possible in VBA but what they wanted is a button that checks and highlights "garbage data" (for example: if First Name column contains a blank or if it contains ajsajdj or something similar), it will prompt the user and ask them if they want to delete it.
We already have the code for the consolidator tool (and it's working perfectly) however, this feature is headache inducing as I don't know if it's possible. I would really like to ask suggestions regarding this as I'm really new to VBA and programming.
Maybe, I would be enlightened on what next step I should take.
Let's see how a human would validate that
He would look at the name ajsajdj and think "I have never seen this name in my life before so it must be nonsense data". But he might fail because the fact that he never came accross this name doesn't mean it's not an existing name (parents can be inventive sometimes).
So what a human actually does is comparing the ajsajdj with a list of names (he has in mind because of his experience in life).
Now a program can do the same
You can write a code that compares ajsajdj with a list of valid names. But here we are at the same point where the human can fail too. The list will never be complete because tomorrow parents invent a new name (that you don't have in that list).
Conclusion
This task cannot be coded unless you define a rule for either valid or invalid data. Some programs look like they could do magic, but actually it is only working because of the rules you give them.
I do automated auditing of this type prolifically so I would approach it like this;
Your 'audit macro' is basically an iterator with many quality checks - is 'name' = "", etc. You can run this auto macro either.
On a single line of data each step of the consolidation
After the consolidation has completed.
The first is easiest to use, and works like this:
After your consolidation step run 'auditing macro' on line of data just brought in.
If an issue is found, write the line of data to a separate Tab leaving column A blank; not to your consolidation. At the end of your consolidation give user a warning message if there have been any issue lines found at the end of the consolidation
User skim reads data on separate tab, puts 1s for 'keep' in Column A.
User clicks a button to run a macro which adds the rows with a '1' against them back to your full data set (e.g. on the end if order doesn't matter).
Equally you could approach this by running the audit when the consolidation is totally complete; in this case you'd need to delete or otherwise track rows which may be removed if a user chooses not to keep them.
I like this approach because it is non-blocking; user can leave your consolidator to run without supervising and then deal with exceptions at their convenience. Also you can write/edit as many tests as you want without fundamentally changing your consolidator at all; you can then also start for example counting the number of each issue per import and putting this into a report for continuous improvement... there are options to extend.
In terms of pseudocode its an iterator full of if-else blocks, with a single 'there is an issue' flag, which if it's 1 causes the row to be treated as an issue;
For rowCount = startRow to endRow
' startRow and endRow correspond to lines of data you just imported
'Test 1
if (Some condition e.g. cells(rowCount ,2).value = "") then
issueFlag = 1
End if
'Test n...
if (Some condition e.g. cells(startRow,2).value = "") then
issueFlag = 1
End if
next rowCount
if issueFlag = 1 then
'CODE TO PASTE DATA
'Set some flag/variable which then triggers a Error Message at the end of the whole consolidation or audit
End if
You can put a Exit For at the end of the IF block so if the issueFlag is triggered you immediately exit and skip all further tests.
Related
I'm extremely new to VB. I've managed to put together a form that gives users the option to pick from 10 different checkboxes (all or any combination) that will run a query against our database and bring back the results into an excel worksheet (a separate worksheet for each option). Basically in my submit button code, the code says If ckboxA = True Then Call query A End If, If ckboxB = True Then Call query B End If, etc...(Each of my queries is a separate Sub in 1 Module - not sure that's important, but just in case).
Some of the queries are really quick while others take some time, I want to provide a progress bar so that the user knows the process is running and gets an idea of progress being made. I've watched video after video, but they all involve a loop where the same thing is being done over and over so the progress is pretty easily measured. How do I do this in my situation, can I add something in my "If" statement for each of the options?
I have the below "Vlookup" , When a record gets submitted in list "Template" I want that to reflect in the list "Aug2021"(Created from an excel sheet) in the "Submitted" column as a "Yes" or "No".
Lookup with between "Project ID" in Template, and "Title" in Aug2021.
IT works fine for the first record submission into "Template", The submitted column shows a "Yes" nicely within list "Aug2021" .
However when I submit a 2nd record, the vlookup resets and that first record will no go to a No. Why is this?
This is what I mean - the flow re runs based on the new record.
When Power Automate does not execute the branch I "think" it "should" execute, that always is because the condition is not met.
The only way to troubleshoot that is to look into what is actually being fed into the Condition. At least while you're still developing.
Initialise two variables at the top, one for the Title and one for the Project ID. Write the Project ID into the variable as soon as you have it. Write the Title into the variable before the Condition step.
Now you can see in the flow run what the values are and you can see the reason for the condition branch being executed.
Also note that the For Each loop will run on all records that are returned bye the Get Items step. This looks like it may write a lot of records that don't match the title.
Edit after comment: You say that the list Aug2021 has 2000 items. Are you attempting to loop through all of them in the flow? That won't work, because the Get Items command only returns a maximum of 100 items.
The flow run screenshot shows that it processed exactly 100 items in the Apply to Each loop. Your screenshot only shows the first one.
Unfortunately, you did not follow my recommendation for troubleshooting, so you still don't know which values are being fed into the condition for each instance of the Apply to Each loop.
If you don't follow advice, I have no idea what else to write.
I'm working on a project for our fabrication warehouse. Currently, we have a system where the workers will type in what order they are working on their tablets and then the part numbers, dimensions, etc will all autofill based on a query of all currently open manufacturing orders...
Whenever we have a prototype or expedited part it won't show up in the query. I was wondering if anyone knew some VBA code (or simpler solutions) that would allow us to keep the current data validation but also give operators an option to override.
Example series of events:
1- operator receives and types in order number
2- msgbox pops up saying "this doesn't exist --- is this a prototype or expedited part?" (answer Y/N)
3- Override message pops up "are you sure xxxx is the correct part number" (operator selects yes)
4- data gets inputed as usual but requires manual entries for dimensions, parts used, etc.
psuedo code:
steps 1-3 : a series of if statements and message boxes guiding the decisions is not too difficult
step 4: where im getting tripped up... how would i unlock that row so they could have custom input for that order and then lock it again once they are finished.
Just a suggestion.
What if you add a non-protected column to the file (assuming that each row is a new entry and entry details are stored in columns).
So, one column with data validation and a warning with the text you mentioned.
And another column to enter the order number manually.
I appear to have a significant data integrity issue using Dictionaries in Excel VBA which I use extensively in my code. I have a routine where I use a Dictionary to capture the best results from an optimization routine. Once captured, I then update an existing data table, using the results in calculations where a zero value will cause errors. This routine has been used extensively in the past with no errors. Therefore, I was surprised when errors started occurring. I tracked the error to what appears to be a Dictionary's data being corrupted outside of the actual code.
The Dictionary with the error is loaded by another Dictionary that continually looks for updates and only keeps the values that are better than previous ones, thus the optimization. Once these values have been captured, the Dictionary (DataItems) that causes the error loads the new values into a data table a variable at a time. Here is the code I used to trap the error:
If KeyCount <> DataItems.Count Then: Stop
NewValue = DataItems(NewData)
If KeyCount <> DataItems.Count Then: Stop
I captured the count of Items in DataItems prior to reading the data into the NewValue variable. Prior to reading the data the Dictionary had a count of 1, but after reading the data, the count went to 2. The original key became the key of a new record with an item value of zero and the original item obtained a new key that appears to be a value in the original bulk data being scanned. This appears to occur almost always with only one record in the DataItems dictionary.
I can use the same routine with other calling applications with absolutely no problems, so the problem appears to be tied to the calling application which makes no sense since the dictionaries being used originated in the routine where the error occurs. I have tried eliminating potential timing issues by stepping through the code and other debugging actions, but I remain extremely puzzled. It really makes me question the potential data integrity of using Dictionaries in Excel for application critical data. Any insights would be appreciated.
I'm guessing that you're using the Watch window and/or the Immediate pane while debugging, and that's what's causing the problem (or at least making it worse)... If you add specific expressions such as DataItems("keyHere") to the Watch list then when code is paused that watch will actually add that key if it doesn't exist.
Similarly, if when paused you do something like ? DataItems("thisKeyDoesNotExist") in the Immediate window, you will not see any output, but that key will silently get created.
To illustrate:
Sub DictWatch()
Dim d As Object, k
Set d = CreateObject("scripting.dictionary")
d.Add "one", 1
Stop 'add a watch on the expression d("one") before proceeding
d.RemoveAll
Stop 'enter "? d("test")" in Immediate pane and hit enter before proceeding
For Each k In d.keys
Debug.Print k 'output is "one" and "test"
Next k
End Sub
I am writing a piece of code to copy data from a web table to an excel sheet. I don't understand why I am getting row count and column count 1. Is there something else that I need to add?
Here is my code:
Dim XL
Set XL=createobject("Excel.Application")
XL.Workbooks.Open "D:\QTP\RailwaysforSurat.xlsx"
Set nsheet = XL.Sheets.Item(1)
row=Browser("title:=.*").Page("title:=.*").WebTable("html tag:=TABLE", "index:=0").GetROProperty("rows")
msgbox row
cols=Browser("title:=.*").Page("title:=.*").WebTable("html tag:=TABLE", "index:=1").GetROProperty("cols")
msgbox cols
This not complete code. I have trouble getting rows and columns count. Please help!
P.S. I am using this website for the testing "http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi"
I noticed that you are using Descriptive Programming techniques to identify the WebTable, but you're using extremely generic descriptions for everything. Perhaps that's just scrubbed for the sake of posting publicly, but the important thing that I notice is that your code to read Rows is trying to find a table with Index of 0 and your code to read Cols is trying to find a table with index of 1...
This means you are accessing two different tables. Are you sure that your descriptions are finding the table that you think you want?
My suggestion is to:
1) bring up the page in your browser so that you can see the table.
2) Bring up QTP and open the GuiSPY
3) Click the pointing hand over a cube button to begin spying
4) Click on something inside the table you are trying to work with.
GuiSPY will snap back and show a hierarchy of objects that it found. Next, you want to positively identify what level in that hierarchy is the table at... I would...
5) starting at the top (Browser), select the top row and click "Highlight in Application" and watch what gets lit up.
6) go down the hierarchy list clicking the next item in the hierarchy and clicking "Highlight in Application" until you see it flash on the exact table you're trying to target.
7) Once you have isolated the table, click the button for "Copy the identification properties to the clipboard" button, then close GuiSPY.
8) Open a notepad, or just use QTP's editor window itself and paste in what GuiSPY copied to clipboard.
Ok, you now have a complete list of everything QTP was able to see about the specific table you want to detect. From here, you want to look through the list of properties and find one (or two) that would positively identify that table every time.. For example, on this very page, the table that holds your question could be identified as:
WebTable("text:=I am writing a piece of code to copy data")... (*note I've shaved it down because it's automatically a Regex string... With some cleanup, it could be:
Browser("StackOverFlow.Com").Page("Question 36663629").WebTable("text:=I am writing a piece of code to copy data"))
Now replace your .WebTable("html tag:=TABLE", "index:=0") with that data you've selected, and try it again. Hopefully you can lock in on the exact table you're expecting and get the info you need.
The indianrail page did not open so cannot see the table you are testing. But QTP provides default methods for rows and columns, try using that and see if it works.
NumRows = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").RowCount
NumColumns = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").ColumnCount(1)
ColumnCount takes row number as parameter
Not sure what tables you are looking at, but if it is Services and information then the rows/cols value for those table is 1.The whole web page is consisted of concatenated web tables. So its going to be little tricky to capture that information. Try using a child object method and count the number of links within the web table, its going to look something like this -
Set oWebEdit=Description.Create
oWebLink("micclass").value="Link"
Set olink = Browser("title:=.*").Page("title:=.*").WebTable("").childobjects(oWebLink)
olink.count
msgbox olink.count
'Then initiate a for loop
For i = 0 to olink.count-1
' Get the link name
olink. count(i).GetRoproperty("name")
'Initiate an array and save the link names
If get link name does not work then you can use childitem method.
Also make sure you are using the correct index for the tables or define some other properties as well.
P.S. If you are going to use DP in future and haven't already then read about Childitems/childobjects methods.They come in real handy while using DP.