Select Data for excel Graph sees zeroes when I load the data using sql - excel

I have a problem with Excel (2016)/ VBA macros. I am happy to provide more detail or copies of files if required.
In summary this is what is happening:
I have a workbook with five worksheets. Three contain data representing both axes of a matrix (1&2), the cells of the matrix (3), one sheet on which data from sheets 1 to 3 are brought together (4) and - finally - a graph worksheet (5 - based on a range in 4).
The reason I do it like this is that I want to graph parts of the data at a time otherwise it is all too confusing. A simple form is used to choose which column of the data-set to display and to also allow stepping through the list of items to display so just part of the range is in view at a time. Variables are set in cells on sheet 4 (outside the graphed range) when the buttons are clicked on the form. The formulae that populate the graphed range on that sheet use these variables to choose the required data from sheets 1 to 3.
Initially I populated the data in sheets 1 to 3 using macros to read from text files. These files had to be populated from a database before opening the Excel workbook. I decided to "Simplify" this process by changing the macros to query the database directly.
It took a while to get this going and all looked fine. I can see the data in the first four worksheets is populated correctly (including being able to click on the form buttons and change the contents of the graphing range) but the graph sees values of zero in every cell. When I right click on the graph and select "Select Data" and then choose an item in the "Legend Entries (Series)" list in the left-hand box, and the click "Edit", I see the cell ranges of both the Series name and the Series Values are correctly displayed, the preview of "Name" has the correct value but the preview of "Series" shows a string of comma-separated zeroes.
I can switch worksheets to view the "GraphData" on sheet 4 while still keeping the dialogue box open and see the actual cells that are being referred to and they are not 0, 0, 0... they are 78, 69, 44...
Where are the zeroes coming from?
I have even saved the workbook - keeping changes - so the contents of sheets 1 to 3 are kept, put a stop in the "Workbook_Open" macro so it doesn't run, and still I get zeroes when I re-open it.

Sorry to bother folk. I worked out the problem. As usual, there were interesting lessons along the way.
I started out with a query that populated a text file with data from my database. Because I wanted the file to be ingested by Excel as a csv file, the output for each row of the selected data was a single string column containing the keys and the data values with commas between them, the numbers being converted to VARCHAR within the sql so I could concatenate them into the string.
Them when I decided to try using the query directly from Excel rather than having the file step in between, I removed the concatenation operators and the commas and put a comma between them in the sql syntax but didn't remove the conversion to VARCHAR of the numbers.
With QueryTable, when you import a text file, one of the optional properties is an array of codes representing the data types of the incoming data. I assumed that with a direct query, Excel would assign a data type based on content as it does for typed values (unless you specify another). Clearly Excel and the source database exchange information, in addition to the data set itself, that tells Excel the data type of the columns - as sent.
The graphing function was seeing my data as strings so assigned values of zero. I began by putting a Value() around my lookup functions on the GraphData worksheet. That worked but then I went further back and actually changed the Sql so it sent numeric data without conversion to VARCHAR and then it worked without the Value() change.
It never occurred to me that Excel wouldn't just see a number and treat it as such - not that Sql Server would have told Excel it was a string.

Related

Create Connection to one raw data file for multiple excel files

I currently have one excel file with four worksheets with data (Name: target value 2022.xlsx). This data is used in multiple excel files to make calculations and to show the values using VLookUp. Until now I copy-paste the values from this one file with four worksheets into all the other files with those four worksheets (and more) when one value changes throughout the year. It also seems to be problematic when a new year begins and a new "target value 2023.xlsx" is required. I tested a lot of ways to make a connection, but nothing seems to be the perfect way:
copy-paste each table via VBA (current way, but I don't want to open every file just because one value changed and click the "Refresh" button)
external reference Cell A1: =[target value 2022.xlsx]Table1!E3 (if one column is deleted, the connection shows #REF!)
Data > New Query > From File > From Workbook (if one column is deleted, the power query doesn't work anymore)
Data > From Text (only works, if all four worksheets are in four seperate csv-files, not optimal)
Data > From Access (seems to be the best way to get the data from four different tables within the database???)
What's the best way to do this, if multiple people use it? The values in "target value 2022.xlsx" change multiple times a year and many users need different files where the data is required. Thank you!

Limit with creating a drop-down list dependent on a selection in excel

I have an excel file with two sheets. The second sheet (Report) contains data validation cells based on the first sheet (Data). From the second sheet, the drop-down list that displays in the Select XXX depends on the selection in the Generate Report. When the Generate Report is set to anything beyond the first five in its list, the "Select XXX" displays year as a default list (no problem with this) via the code ...INDIRECT("Year").... The problem is that excel does not allow for addition of more code (seems I hit the limit). The question is - how can I manipulate this code to accommodate every option in the Generate Report? or perhaps, is there another method to implement?
The data validation source code for the drop-down list is =IF($B$4=Data!$Q$5,INDIRECT("Client"), IF($B$4=Data!$Q$6,INDIRECT("Month"), IF($B$4=Data!$Q$7,INDIRECT("Product_Service"), IF($B$4=Data!$Q$8,INDIRECT("Sector"), IF($B$4=Data!$Q$9,INDIRECT("Trans_Type"),INDIRECT("Year"))))))
Please, see the sample file at https://drive.google.com/file/d/1VKkGHjlJzLQqx4J9kyd_bCKG4r0Q7HkG/view?usp=sharing
What you could do is put the range names in column R, and VLOOKUP them:
=IFERROR(INDIRECT(VLOOKUP($B$4,Data!$Q$5:$R$9,2,FALSE)),INDIRECT("Year"))
You could then have as many item lists as you wish.

Is it possible to base data validation on a string in a single cell in excel 2010

I am building a multilevel dependent dropdown structure in Excel based on a parent-child structure. Building this with vba is not a problem but the problem is reopening the file. It removes the validation because they have too many characters (more than 255?)
The generated validation strings are too long for Excel to store so they are removed after reopening.
A solution could be to write the validation string in a cell and base the validation on this cell value. Only problem is when i link the data validation to this cell its only one option.
the value in the cell is something like A,B,C,D (already tried with or without "")
Does anyone have a suggestion for an excel formula to use in the data validation to generate multiple options from a string like A,B,C,D located in one single cell.
TNX for the support.
The solution was a bit more difficult.
I was able to make it work but only by combining VBA and Excel Formulas. Also i added a value to the dataSource with both the id and optionid in it.
Excel:
For The data validation i use an fuction who utilizes an offset which is dynamic and based on 2 values. This way i dont get the error that a validation is more than 255 characters.
data validation formula:
=OFFSET(List!$A$2;MATCH(CONCATENATE(V18;"|";E18);List!$F:$F;0)-2;1;COUNTIF(List!$F:$F;CONCATENATE(V18;"|";E18)))
One is for the id and one is the optionid.
VBA:
I now use the on change event to produce the values needed for the dropdowns(data validation) but not letting vba insert the values for the data validation. It gets these values via a pre-loaded array which is started with a workbook_open event. (if this isnt loaded it will load it again).
When i select an item it writes the id and optionid to a different cell. I then use these to produce the next dropdowns etc with the offset function. (one dropdown can result in multiple rows of new dropdowns)
Also added some functionality for delete and change (in the middle) events.
Also the onChange event now triggers the next dropdown and write its value if there is only one option. This will of course trigger the next.

dynamic linking of word and excel

This is my first question and it would have been nice to include an image but it seems that I can't. I have seen some answers to my problem but they always seen to be in one direction. Excel->Word. However I want Word->Excel->Word as described below.
I have an excel workbook that draws a graph. Some of the input data is pre-calculated and one or two parameters are entered manually and entered in a table in the spreadsheet.
I want to do the following.
The graph and the data entry table are to be displayed in word. The table in Word is also part of the spreadsheet but must be displayed separately. I want to be able to enter the variables in the table in the word document. These values are linked to the excel sheet, the graph will be changed and the actualised version displayed in the word document. I have attached two images, the data entry able. The entry fields are those in green and an image of the resulting graph. Any help would be appreciated.
I can't see your pictures, so the following is theoretical.
1) Open Word
2) Insert Chart - this opens a instance of Excel which is directly linked to the Chart, which I'll refer to as ExcelChartData to avoid confusion.
3) Edit the range in ExcelChartData to match what you have in your Excel data workbook.
4) Link the ranges in ExcelChartData to the required ranges in your Excel data workbook.
Changing the values in your Excel data workbook will then change the chart.

SSIS Excel Data Source - Is it possible to override column data types?

When an excel data source is used in SSIS, the data types of each individual column are derived from the data in the columns. Is it possible to override this behaviour?
Ideally we would like every column delivered from the excel source to be string data type, so that data validation can be performed on the data received from the source in a later step in the data flow.
Currently, the Error Output tab can be used to ignore conversion failures - the data in question is then null, and the package will continue to execute. However, we want to know what the original data was so that an appropriate error message can be generated for that row.
According to this blog post, the problem is that the SSIS Excel driver determines the data type for each column based on reading values of the first 8 rows:
If the top 8 records contain equal number of numeric and character types – then the priority is numeric
If the majority of top 8 records are numeric then it assigns the data type as numeric and all character values are read as NULLs
If the majority of top 8 records are of character type then it assigns the data type as string and all numeric values are read as
NULLs
The post outlines two things you can do to fix this:
First, add IMEX=1 to the end of your Excel driver connection string. This will allow Excel to read the values as Unicode. However, this is not sufficient if the data in the first 8 rows are numeric.
In the registry, change the value for HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Nod\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows to 0. This will ensure that the driver looks at all the rows to determine the data type for the column.
Yes, you can. Just go into the output column list on the Excel source and set the type for each of the columns.
To get to the input columns list right click on the Excel source, select 'Show Advanced Editor', click the tab labeled 'Input and Output Properties'.
A potentially better solution is to use the derived column component where you can actually build "new" columns for each column in Excel. This has the benefits of
You have more control over what you convert to.
You can put in rules that control the change (i.e. if null give me an empty string, but if there is data then give me the data as a string)
Your data source is not tied directly to the rest of the process (i.e. you can change the source and the only place you will need to do work is in the derived column)
If your Excel file contains a number in the column in question in the first row of data, it seems that the SSIS engine will reset the type to a numeric type. It kept resetting mine. I went into my Excel file and changed the numbers to "Numbers stored as text" by placing a single quote in front of them. They are now read as text.
I also noticed that SSIS uses the first row to IGNORE what the programmer has indicated is the actual type of the data (I even told Excel to format the entire column as TEXT, but SSIS still used the data, which was a bunch of digits), and reset it. Once I fixed that by putting a single-quote in my Excel file in front of the number in the first row of data, I thought it would get it right, but no, there is additional work.
In fact, even though the SSIS External DataSource Column now has the type DT_WSTR, it will still read 43567192 as 4.35671E+007. So you have to go back into your Excel file and put single quotes in front of all the numbers.
Pretty LAME, Microsoft! But there's your solution. I have no idea what to do if the Excel file is not under your control.
I was looking for a solution for the similar issue, but didn't find anything on the internet. Although most of the found solutions work at design time, they don't work when you want to automate your SSIS package.
I resolved the issue and made it work by changing the properties of "Excel Source". By default the AccessMode property is set to OpenRowSet. If you change it to SQL Command, you can write your own SQL to convert any column as you wish.
For me SSIS was treating the NDCCode column as float, but I needed it as a string and so I used following SQL:
Select [Site], Cstr([NDCCode]) as NDCCode From [Sheet1$]
Excel source is SSIS behaves crazy. SSIS determines the type of data in a particualr column by reading first 10 rows.. hence the issue. If you have a text column with null values in first 10 roes, SSIS takes the data type as Int. With a bit of struggle, here is a workaround
Insert a dummy row (preferrably first row) in the worksheet. I prefer doing this thru a Script task, you may consider using some service to preprocess the file before SSIS connects to it
With the duummy row, you are sure that the datatypes will be set as you need
Read the data using Excel source and filter out the dummy row before you take it for further processing.
I know it is a bit shabby, but it works :)
I could fix this issue. while creating the SSIS package, I manually changed the specific column to text (Open the excel file select the column, right click on column, select format cells, in number tab select Text and save the excel).
Now create the SSIS package and test it. It works. Now try to use the excel file where this column was not set as text.
It worked for me and I could execute the package successfully.
This should be resolved simply, just untick the box "Frist row as column names" and all data will be collected as text data type. Only downside of this choice is that you have to manage the columns names from the auto names (column 1, 2 etc) and handle the first row which contains the column names.
I had trouble implementing the solution here - I could follow the instructions, but it only gave new errors.
I solved my conversion issues by using a Data Conversion entity. This can be found on the SSIS Toolbox under Data Flow Transformations. I placed the Data Conversion between my Excel Source and OLE DB Destination, linked Excel to Data C, Data C to OLE DB, double clicked Data C to bring up a list of the data columns. Gave the problem column a new Alias, and changed the Data Type column.
Lastly, in the Mappings of the OLE DB Destination, use the Alias column name, rather than the original Excel column name. Job done.
You can use a Data Conversion component to convert to the desired data types.

Resources