Create SQL Query In VBA And Display Result As Table In Worksheet
I need the VBA script in excel that will do a SQL Query, for this case I need to select a value where there are 2-3 tags and between certain period then display the result as a table in a worksheet.
Example I have a database with 3 types of tags "Tag A", "Tag B", and "Tag C", and each tag have a value with different timestamp. the database looks like this:
No. Timestamp Tags Value
1 1-Jan-2010 Tag A 18
2 1-Jan-2010 Tag C 20
3 2-Jan-2010 Tag A 20
4 3-Jan-2010 Tag B 17
5 3-Jan-2010 Tag C 19
6 4-Jan-2010 Tag B 18
7 4-Jan-2010 Tag A 20
8 5-Jan-2010 Tag A 22
9 5-Jan-2010 Tag B 18
10 5-JAn-2010 Tag C 20
View Complete Thread with Replies
Related Forum Messages:
Lookup Query - Occurrences: Create Another Worksheet With The Months Of The Years Listed Down Column
I have a table that lists the months of the year down from cell A2:A13, and days of the week along row from cell B1:H1. The data in between (cell B2:H13) is pulled through from elsewhere in the workbook and is in number format.
What I basically want to do is create another worksheet with the months of the years listed down column A, and in column B, for each month, I want the first day of the week where the value in the original table is more than zero, and in column C the second day of the week where the value is more than zero. I really hope that makes sense, was quite difficult to explain!
Create An Conection With MS QUERY To A Csv And Query Data
I have a csv file on another drive on the network that i need to query. I believe that ms query would be the best way. I know that a DSN needs to be setup but this macro will be used by various users who wont know how to do that. thus I would like to create one via VBA every time the task needs to be run.
I haven't a clue how to do this and i need it to be explained to me in general terms with words of one syllable!
Fill In The 3 Criteria Fields, Calculate The Result, Copy The Result, And Place It Into The Correct Place On The Table
I have a macro that imputs data from an external database and puts it into a temporary worksheet. This data has 3 columns (ID, Date, Amount). I am then making another sheet which has X number of tables (one for each ID), with the years being the column headings, and months being the row headings. ie.
| ID X
| | 1999 | 2000 | 2001 |
| Jan | $100 | $250 | $300 |
| Feb | $200 | $300 | $200 |
| Mar | $300 | $250 | $100 |
| Sum | $600 | $800 | $600 |
| ID Y
| | 1999 | 2000 | 2001 |
| Jan | $100 | $250 | $300 |
| Feb | $200 | $300 | $200 |
| Mar | $300 | $250 | $100 |
| Sum | $600 | $800 | $600 |
Currently I have a few hidden fields for the DSUM Criteria. I start making the tables. And then filling in table based off of the month and year. Doing so I need 3 criteria:
>= First Day of the Month
<= Last Day of the Month
= ID #
The problem is it takes Excel too long to fill in the 3 criteria fields, calculate the result, copy the result, and place it into the correct place on the table. Is there another way to get this data into the correct tables faster? Instead of using DSUM?
If Number =1 Display The Result
I have a count for each site for certain cloumn headers.
But i want to collate these so that if there is a 1 in the column the it will output it with the column header. But there are 10 column headers and I would like to get a result that has all the columns with 1 in.
a b c d e f g h i j k l m n o p q (Organic Suites) (Inorganic Suites)
sitea 1 0 1 0 1 1 0 1 0 1 1 1 0 0 0 0 (O1 O3 O5 O6 O8 OS) (I1 I2)
Display Formula In A1, Result In A2
I have a formula in A1 that I would like to execute from A2. I want the A1 to display the formula (so no "="), but I would like cell A2 to execute the formula in A1.
If A1 contains: "SUM(1+2)", how can I get A2 to display "3" without reproducing the formula?
I suppose I'm looking for something like this:
But, of course, that doesn't work.
If I do this it gets close:
But that returns "=SUM(1+2)" instead of executing the formula.
If / Vlookup Display Result Not Formula
Hopefully you will be able to help again. Is it possible to do a vlookup that references data on other tabs within the worksheet so that the result of the formula is in the cell not the formula.
So if my vlookup was =vlookup(a2,$a$1:$b$12,3,0) and the result was john smith i want just john smith in the cell. I know about copy and paste values but i was looking for a more automatic way. One that doesn't need intervention.
Display Result Of Formula In Cell
Dim numwords As Integer
numwords = WorksheetFunction. CountA("H5:H64")
Selection.FormulaR1C1 = numwords
I have a quick question regarding this simple code thats been drviing me nuts. There x distinct pieces of Data in the range speciifed, but when i run this code, the reply posted in cell H1 is 1, instead of x.
Eventually the code will be built to check if the result is odd or even and then add an entry into the first blank cell if the result is odd.
Display Many Cell Result In Message Box
i have a routine which loops through a range looking for past dates and when it finds one display the result in a message box in my testbook i only use 25 rows but it gets annoying having to click ok for every find....is it possible to collect all results and display them in the message box at one time?
Private Sub Workbook_Open()
Set Rng = Sheets("Sheet1").Range("B1:B25")
For Each Mycell In Rng
If Mycell.Value < Date Then
MsgBox Mycell.Offset(0, -1).Value & " Is Overdue By " & Date - Mycell.Value & " Days, Take Action Now!", vbOKOnly, "Tasks Overdue"
Create Query Of Mutiple Workbooks
I have a workbook that query's our case tracking database and pulls data into a csv. I am trying to write something that would do the following:
1) Prompt the user to enter in a case number
2) Open up the following file:
-"C:Documents and SettingsDetails.csv"
3) Look through A:A for a match of the user entered number
4) If a match is found then pull the data from cell B in that Row
5) Output the data (from row b)into a text box on the user form along with the original entry made by the user.
Display Result And Hide Macro Formula
I have been struggling for an hour how to hide a number to text macro in a way that only macro result is displayed in a cell.
(I have one 2-3 years old xls, where I have managed with task, but now can't figure out how and how to unhide the macro
Can it be password protected somehow?
Display Result Of Date Function In Label
I am trying to display the result of a custom VBA function as a label on a userform. The function is called and takes arguments from three text boxes on the userform. I attached it to the exit parameter. (This may be part of the problem; I'm not sure).
There seem to be two problems, first all the text boxes don't always contain data, but because the arguments are not optional, I get a type mismatch error when the code runs (I tried switching some of them to optional, but it didn't help as you can see from my 'commented code).
The other problem is that even when all the arguments seem to be met (i.e. the text boxes all have data), I still get a compile error that the argument is not optional. This only happens if I try and pass the result to some aspect of the userform. As you can see from the last coded line in the custom function, if I assign it to a range on the worksheet, it functions fine.
Private Sub txtStartDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim StartDate As Date
Dim EndDate As Date
If IsDate(Me.txtStartDate.Value) Then
StartDate = Me.txtStartDate.Value
Me.txtStartDate.Value = Format(StartDate, "m/d/yyyy")
Else: MsgBox "Please enter a date"
Display Result Based On Time Difference
I want to make a formula based on 2 times ex: 10:07:00 and 10:09:00. This formula should display "intime" if the diference between both is under 20 minutes, "outime" if the diference is above 20 minutes and it should display "error" if the time is under 0 minutes (this will only happen when someone makes a mistake typing in the time. For example 10:37:00 and 10:36:00)
Macro To Create A Statistics Table From Another Data Table (containing Merged Cells)
I would like to have a macro to automatically generate a statistics table (on the "statistics" tab) with the 5 following fields:
Fragment names / # samples / # of failed samples / % of success / # of variations in the fragment (SNP). At the bottom of this table, I would like to have a cell with the average % of success for all fragments. The data to generate these statistics are on the "gene name" tab (please note that this name will change every time I will work on a new gene). To make things easier, I think the macro should be run from this tab.
1. The Fragment names are displayed in row #5. I use one column per variation per fragment. If one fragment has 3 variations, there will be three columns and I will merge together the fragment name cells. The fact that some cells are merged can be a problem when copy-paste to the stats table (as I would like to get rid of the merging).
2. # of samples corresponds to the number of cells in blue in column A. The number of samples can change from one report to another but is always constant in the same report.
3. # of failed sequences. In the table, I type "Failed Sequence" (if the analysis has failed) and "Missing Sequence" (if the analysis has not been done). When a sample is failed or missing, it is for the who fragment, no matter how many variation there is in the fragment, so I usually merge the cells of all variations for this failed sample.
4. % of success: this is quite easy #sample/#of failed+missing sequence for this fragment
5. # of variation is equal to the number of variations for this fragment (can be 0, 1, 2, etc.). When there is no variation in a fragment, I put '-- in all cells of the corresponding fragment on the "gene name" tab. Fragment 3 on my file is an example of 0 variation.
Create Database Table From Multi-Column Summary Table
I want to accomplish something like this but slightly different:
I have the same issue but a bit more complex. In my case I have an additional amount of columns (let's say dimensions). So for the example given (sales per month) I would add two columns for 4 different regions and 5 machine types (just examples).
The output (that would serve as pivot tabel input) would then have to be a 4 x 5 x 12 x 4 = 960 row database table.
I have tried to accomplish it by customizing the given code but that provedto be beyond my powers ;o)
and some 3rd party software [url]and [url] but non of them is doing what I'm looking for.
The code I'm looking for ideally would count the number of columns and unique records per column and construct the database table from that. So I am looking for flexibility in the number of dimensions also...
Display The Path Result From A Floyd Algorithm Matrix Output
I want to display the path result from a Floyd Algorithm matrix output..
You can download the excel file here : http://ifile.it/lw4tgic
It consists of 1044 nodes, and we need to find the path between 2 nodes for ALL pairs..
Now, to find the path between 2 nodes, we need to :
1. Find the resulting cell of corresponding 2 nodes (y to x = z)
2. Update the path with that value (y z x)
3. For all the direct pairs in the current path (yz, zx), find the resulting cell
4. If destination (x) not the same with result (z) then repeat step 1 for that pairs.
5. repeat until x = z for all pairs.
Maybe it's better if i use an example....
we'll use T01 to E78 as an example
T01-E78 = if you look in the table,
y (vertical) = T01,
x (horizontal) = E78
z (result) = E77, so
T01-E78 = E77, so path = T01-E77-E78
check if result of T01-E77 = E77 (x = z?)...........................
Create Hyperlink To Result Of Lookup
I have three columns. Column A=" Name", Column B=" Date" and Columnc="Amount". I need to find and go to the correct amount based on a certain name and date as criteria.
I was going to see if it was possible to use this =INDEX(C1:C1000,MATCH(1,(A1:A1000=D1)*(B1:B1000=E1),0)) with a hyperlink, but I can't even get it to work without a hyperlink. I also tried this to find the match= SUMPRODUCT((A1:A1000=E5)*(B1:B1000=EE6)*(C1:C1000)). I found it on a website(I'm not sure how it works.) My next shot was going to be with multiple criteria vlookup, but I can't get anything more than a plain vlookup to work. I'm using excel 2007 non-commercial use.
Query From 2 DSN's Into A Single Table
Our company's accounting system is physically split into 2 company databases, CompanyA and CompanyB. I'm trying to write a query which will pull the customer number and the accounts receivable balances from both companies into a single Excel table, for any customer with a total balance 0.
I have created the following query, which doesn't generate any errors, but is only successfully importing the data from the first company.
The CompanyA DSN is specified in the query ...
VBA - Refresh Pivot Table & Query
I've used this code prior and it worked fine in Excel 2007, now I'm having issues with it. Originally every Pivottable & Query pointed to a different CSV file. Now I have two pivottables built off a Excel worksheet that is built from a Query. This allows me to do some complex lookups that I couldn't do with just the Pivottable and MS Query alone.
Here's the code that's failing out: Any problems with it in Excel 2007?
Private Sub Update_All_Data_Click()
Dim pvt As PivotTable
Dim ws As Worksheet
Dim qt As QueryTable
mytitle = "This will refresh all data for validation, are you sure?"
Msg = "The Refresh process takes about 5 minutes, are you sure you want to continue?"
Response = MsgBox(Msg, vbExclamation + vbYesNo, mytitle)
Select Case Response
Case Is = vbYes
' Do Nothing, continue with program
Case Is = vbNo
For Each ws In ThisWorkbook.Worksheets
For Each qt In ws.QueryTables
qt.BackgroundQuery = True
For Each ws In ActiveWorkbook.Worksheets
For Each pvt In ws.PivotTables
mytitle = "Confirmation of data refresh"
Msg = "The data has been refreshed"
Response = MsgBox(Msg, vbExclamation, mytitle)
Web Query - Select Table Not Being Captured
I'm trying to get the data from this site
When going through the Web Query in Excel and pulling data with the above address. I get the option to pull the data from the products table..the table with the code/name/product group/sub group..etc etc...
But when I launch the query, it doesn't pull the table data..I get everything but the product list.
Pivot Table Count Only 1 Query
the attachment is a sample spreadsheet of what i am trying to do. I would like to create a pivot table so that for each site I can list all of the organic suites individually. Then show a count of only 1 for each site, even when there are several counts for each site. And then for this to be related to a cost of each of the suites so that I can get a total cost for each site.
organic suites suite cost
Copy Data From Web Query To A Table
I use web query to get some range of data. This data is refresing and changing on some time (i set query refresh on 1 minute). So, i'd like to fetch this data and make the table with all data (new data on eg. every 1 minute). How to do this?
I use Excel 2003.
HLOOKUP- Query To Populate A Table In My Spreadsheet
I run a web query to populate a table in my spreadsheet. The data is dynamic - and on some days, I will get a couple of extra rows in the table (its weather data, so e.g. when its raining, they will forecast an extra row of how many mm, etc...)
To give a simple example, right now I use Hlookup to locate the value of F3 (the date) in my dynamic range of B26:G26 and step down 3 rows, where my needed temperature values are for that corresponding day. This works great like this already: =HLOOKUP(F3,B26:G29,3,FALSE)
The big issue is...what happens if my target values are not always 3 rows down? If the query refreshes with any extra rows, this reference is shifted. (Well, my starting "Anchor" is shifted)
Make A Pivot Table In 2003 Based On This Query
I have an SQL Server that has a query (a view stored on the server) which takes roughly 10-20 seconds to execute. This execution time is the same in Excel 2007 if I import data to worksheet/pivottable. It is also the same in Excel 2003 while importing data into a worksheet. It returns about 350 rows.
However, if I try to make a pivot table in 2003 based on this query, it is much slower. I can set it up fine, but refreshing this pivot table takes at least 10-20x as long to complete, in the order of 5-20 minutes, to sometimes taking so long that I give up and kill it by hitting escape a bunch.
It's important to note that I am forced to used Microsoft Query to set up a pivot table in Excel 2003. There is no other way, I believe.
While it is running, Excel just sits there saying "Waiting for data to be returned from Microsoft Query".
Here is the weirdest part. Once the refresh starts, Microsoft Query loads in the background, you can see it on the taskbar. I believe that is expected behavior, but if I click on MS Query and view its datatable, I can see all the data is sitting there.
So what is the hold up? MS Query has the data. Excel is waiting for it to be returned. What is going on? And remember from above, if I do a simple import data to worksheet, MS Query is not used and everything is fast.
So my question is does anybody have any tips/advice on this issue? I need a way to not use MS Query for a pivottable's data source or I need a way to make MS Query not freeze up. I don't get why MS Query needs to be involved in the first place since I am not doing any parameters or so forth.
Avoid Query Table Continue Message On Refresh
I've used the code below and it's worked to stop Update Links message, but i still need one tip to avoid continue message that shows up after Update Links massage:
Private Sub Workbook_Open()
Application.AskToUpdateLinks = False
Create A Formula Where Using Different Texts To Display A Value
I would like to create a formula that allows the destination cell display a number depending on a word that is typed in the source cell. The source cell and the destinating cell will remain the same two cells.
If i type WORD1 in A1 (source cell) then A2 (destination cell) will display 1.5, but how do i make this formula work for a multiple of different words.
example:A2=IF(A1="WORD1",1.5,0 IF(A1="WORD2",3.0,0 IF(A1="WORD3",2.5,0)))
this formula doesn't work, but is basically what i am looking for the formula to do - WORD1 displays 1.5, WORD2 displays 3.0, WORD3 displays 2.5 etc where the source cell and the destinating cell remain the same two cells, it is just the word and its value that change.
The Result Of A Formula To Move To The Left And Top Of The Table
This formula tells me the highest number: =LARGE((G36,O36,W36,AE36),1)
The result is AE36. The answer is correct. I get the highest number. I want the formula now shows Y3 instead of AE36.
In my situation Y3 is the name of 1 of my tables. In my case I get from this table, the highest number.
These are 6 cells to the left side. These are 33 cells to the upper part. In the future I will work with more tables. So the answer must always move
I've been busy this few days to investigate. That is why I know it works with offset. Anyway, I still do not know how it goes.
Create Formula To Display Text With Date
I have this formula =TEXT(inventory!I2,"mmm-yyyy") which takes the date from another spreadsheet, but anytime that the date is missing, it susbstitutes for Jan-1900. Is there any way to revise the formula to leave the missing dates blank in the cell? For example, if the date is missing on the other worksheet, in this case, the inventory worksheet, is there any way for a blank to appear, instead of the Jan-1900? Also, anyway for this formula to spell out the month, and not just the abbreviation?
Populate Cells/Table Depending On INDEX Result
I am trying to populate a table (a7:c27) in the attached sheet with data.
First, I must enter a number 1-16, which uses index function to return either "series" or "base"
If it is base, i want the number in the corresponding table filtered down - i.e. if 1 originally picked then h7 is (40) is entered into each month as base = constant.
If the result is series, then the data in i7:z7 is entered into the the 18 month table.
Automatically Run Macro On Formula Result Obtained From Pivot Table
I currently have an excel file set up which is refreshing imported data every minute from a server. This data is located on " sheet 1".
This data is the result of another system constantly monitoring (pulling data from) the process at my factory. It consists of temperatures, speeds and other settings.
When a temperature, speed or setting changes then it will be captured by the monitoring system and therefore my excel sheet will load it into the imported data within the next minute.
I would like to be able to capture the change in temperature, for example, if it goes into alarm and automatically send an email reporting this alarm. This alarm is captured in my imported data in a specific column with a 1. If it is out of alarm it is 0. There will not be a report of an item, at any one time, with both a 0 or 1... so there is only one case of any one monitored process.
I have a pivot table set up to filter all the uneeded data out and I currently refresh it manually to show the current status of all alarms. If an alarm occurs on the process, I can refresh my pivot table and it will be indicated with a 1 until it is refreshed again (and the alarm is gone).
So the question:
1. How can I refresh the pivot table automatically every x seconds?
2. How can I automatically send an email on the value change from 0 to 1 in case of an alarm?
Compare Dates In Table & Adjust Adjacent Value Based On Result
I have built a macro that tests to see if a date in a range is before the Period Start Date, and if it is, then the labor rate in that same row (3 Columns Over) should be escalated by the appropriate AWI. My code below works, but I am wondering if there is a better way than using offset(0,3) to prevent a code breakdown if columns are inserted between the Date and Rate columns. The date range is names "DATES" and the rate column in names "RATES"
Dim cell As Excel.Range
Dim i As Integer
For Each cell In Sheet2.Range("DATES")
If cell.Value < Sheet2.Range("POPS") Then
i = i + 1
MsgBox ("You have " & i & " Rates that will be escalated b4 the POP Begins")
yesno = MsgBox("Would you like to Escalate the Dates and Rates?", vbYesNo)
If yesno = vbNo Then
MsgBox ("Not Done")
Function To Call Another Worksheet Query
I have a spreadsheet with 5 worksheets representing the working days of the week. Each worksheet contains data pertaining to every occurance of that day for as long as it has been recorded. Each day is broken up into hourly data.
So for example, for Monday's worksheet, you would have
Col A..............Col B........Col C
We are trying to create a function, on another worksheet called Query. The only way I can describe what we want it to do is to do it in psuedocode.
Display A Table In The Same Sheet
I have a table with headers C1:I124, Columns E-H are hidden.
Col C = Agent (list of names)
Col D = #Hits (number of times the Agent's name appears on a list)
Col I = #Strikes (Number of times an Agent fails an Audit)
What I want to do is produce a Watchlist split into 4 sections:
1. All the Agents with 1 Strike
2. All the Agents with 2 Strikes
3. All the Agents with 3 Strikes
4. All the Agents with 4 or more strikes
So I want to display this in a table in the same sheet which has 4 columns as described about listing all the Agents who match that criteria.
Strike 1 Strike 2 Strike 3 Strike 4 or more
Bob Ian Larry Reggie
Tim Colin Stephen
Pivot Table Text Display
I have a data file in excel with the following columns
I want to build a pivot table from this which shows:-
date along the top, name down the left side and in the data area all the activities..(as text)
Is this possible ?
I can only manage the activities to be displayed in the pivot as zero