Exception from HRESULT: 0x800A03EC in VB.net writing to excel - vb.net

I'm aware there are other questions similar to this, but I haven't found an answer that works for me. I have a program that writes an excel report for sales by month. It works fine when given a date range less than three years, but when I input a date range larger I get this error. Using breakpoints I discovered the error occurs in this portion of code
For iColl = (iReportCollumns + 2) To MaxY
xlApp.Cells(x, iColl) = "=SUM(" & GetColumnName(iColl) & (x + 1) & ":" & GetColumnName(iColl) & (x + iRowsPerRecord) & ")"
Next iColl
Since this is for work, I'm not able to reveal a lot of the code so I'll try to explain in good enough detail what's going on here. If you need more code, let me know and I'll see what I can release. The report has locations which each have multiple (but the same) sale types. For each sale type the report displays by month the total sales.
So sales are grouped at the highest level into locations, with sub groups for each sale type, these are displayed as separate rows of the spreadsheet. The months are the columns of the spreadsheet. iReportCollumns + 2 represents basic information that is displayed for each location, so that the sales data starts in the columns after this. iColl represents the current column, iRowsPerRecord represents the number of sale types. In my test case there are three rows per record, five years for a total of 60 months, and iReportCollumns = 9, so there are a total of 71 columns per row. MaxY represents the number of columns per row, so 71 in this case. This bit of code creates a totals row for each location. I have two locations in my test case so that there are 6 rows of data and 2 rows of totals. The totals rows come before the data rows for each location.
Stepping through this for loop, I discovered that it stops when iColl = 52 while it is still working on the first totals row. So it is not even getting to the data rows before I get this error. However it does insert the column names for all of the columns, but it breaks once it tries to write the sum formulas. Is there some limit to how many columns I can insert into an excel sheet?
My alternative solution is to just limit it to only show a maximum of 24 months per report, which works for other similar reports. That solution is okay, but I'd rather allow the user to choose a larger date range if they so please.

Related

Sum unordered data across multiple sheets

I think sumproduct sumifs indirect is what I need but I fail to see how to construct it;(
I have a workbook for logging volunteer hours.
I'm summing up the monthly hours (12 sheets/tables) to FY Totals sheet/table with hours by volunteer. So the sheet/table that I want to use the formula in is FY TOTALS
The workbook consists of 14 sheets:
sheet(VOLUNTEERS) has a table(tbl_volunteers) It contains data about the volunteer and the 1st 3 columns are duplicated on all 13 sheets (12 monthly and 1 FY totals)
A5[Status] B5[LastName] C5[Firstname]
The Month sheets have the above fields followed by 5 categories with hours per category for each volunteer
The sheet/table FY Totals is identical to Monthly, but I want the categories to sum all 12 months for each volunteer.
So I need to match criteria of [LastName][FirstName] and sum values in [category]D:I
I can send a copy of the file, but not load images here;(
You could use multiple consolidated ranges.
Notice that I have four different ranges. These could have been in different worksheets and the names aren't necessarily in the same order. A pivot table has been created that sums the hours worked for each person as requested. In this example, they just happen to be the same.
How?
Press Alt+D+P (windows) or cmd+alt+P (mac)
Select multiple consolidated ranges, then Next
Select Create a single page field for me, then Next
Add each of your ranges one by one, then Finish
Update
There's so much more you can do with PivotTables, as per your comments -- you can separate out data by creating your own page fields. Do this instead of step 3 above
Select I will create the page fields, then Next
For each range you select and add, click '1' page field and type the description of that data, for example 2016 Data, or 2017 Data
(Optional) Your fields will automatically be put into the report filter field. You might want to drag it into Row Labels field (below Row) to get the view below
You can then see the split of the different fields you used in your PivotTable - in yellow I have my 2016 data and you can see it's been split out in the PivotTable.

Most efficient way to check if a number is within a large amount of ranges in vba

I am attempting to code what sounds like either a segment or interval tree, but I'm not sure how to do it in VBA. Basically, what I am trying to do is take a list of numbers and seeing if they fall under a list of ranges. In the real world, the numbers refer to customers, and these ranges are assigned to different company sites. So for example, let's say we had 3 numbers (aka customers) to input: 156000 166000 and 256000. Site 1 gets any customers between 145000 and 149000, 152300 to 155000, and 165000 to 165999. Site 2 gets any customers between 156000 and 159000, 162000 and 165000, and 255000 to 275000. Site 3 get any customers between 166000 to 180000. So I would want the function to be able to tell that customer one and three would go to site 2, and customer two would go to site three. So each site has about one to eight nonconsecutive ranges associated with it.
The amount of numbers(customers) being checked against ranges can vary from 1 to 5000, but the amount of ranges is about 3000 spread across 497 different sites, which will slowly increase over time but will be statically updated. Customer numbers will be copy/pasted to column K in my Excel worksheet. The site ranges I will have in another sheet in the same workbook. Initially I tried to just do tons of if/then statements, but it seems like doing a list of 3000 if/then statements cant be the best way. Then I thought about doing a tiered list of if/then statements, by initially just looking at the first two numbers of the customer number stored in column L, like:
If Worksheets("Input").Range("L" & x) >= 14 and _
If Worksheets("Input").Range("L" & x) =< 19 then
If Worksheets("Input").Range("K" & x) >= 145000 and _
If Worksheets("Input").Range("K" & x) >= 146000 then
[code to assign customer to specific site]
End If
If Worksheets("Input").Range("K" & x) >= 245000 and _
If Worksheets("Input").Range("K" & x) >= 246000 then
[code to assign customer to specific site]
End If
End If
The problem with doing it this way is that each site has non consecutive ranges, so I'm not sure if making multi tiered if/then statements would even be able to work.
Anyone have any ideas as to how this could efficiently be done with such a large amount of ranges?
Thanks in advance!
This is very easy to do using VLookup (Range) combined with a smaller conditional test to ensure the empty spaces between ranges don't pass. No VBA needed:

Index and Match Excel

I'm creating a vacation tracker in Excel. The first sheet pulls the data from SharePoint which has Start Date, End Date and a Date Difference calculation.
Sheet1
A2=12/16/2015
B2=12/20/2015
C2=5
The second sheet is the visualization of the data. It starts with cell B1 and goes out for 90 days. Is is a word representation of the date. Here is the formula I'm using =UPPER(TEXT(B2,"DDD"))
Sheet2
B1=WED
C1=THU
D1=FRI
E1=SAT
F1=SUN
The next row always B2 has a formula which is always today's date. From there I add one date to increment the dates out to 90 days. B2=Today() and the other cells =B2+1 and so forth
B2=16
C2=17
D2=18
E2=19
F2=20
The problem I'm running to is that our boss can have 3 calendar entries consisting of different dates. So when I perform a SharePoint pull I have 3 different rows of vacation dates. I'm experimenting with an Index and Match example, however the data match it is placed in a new row. How do I place the data on the same row?
I'm not convinced this has much to do with VBA. Seems enough detail of final design but rather light on the data gathering process. However when I perform a SharePoint pull I have 3 different rows of vacation dates hints at a use for Subtotal. Three rows may be 'converted' into one:
where the unshaded part (some cells with Xs) is assumed to be a representation of the current SharePoint data extract. Subtotal is able to add the rows shown lightly filled, which might then be used to =VLOOKUP("Boss"&" Count",A:F,n,0) where n represent the choice of column label.

Excel Formula to calculate number of user hits in a system everyday

This is the continuation of my previous questions and I have included my global aim with project.
Excel Link:
https://drive.google.com/file/d/0B5W4CgZKsARFSmhqV0ZSM1lBSkE/view?usp=sharing
Task:
The task is to collect the "number hits" of a user in an application "on each day". I can collect a log from the system which will give me the name of the users and their last hit date and total number of hits.(This total number of hits in not on daily basis, but sum of all days). and I need to make a formula to segregate these on daily basis
Screenshot:
To understand in better way I have created few screenshot, which I am looking to implement.
The excel has two tabs raw_data and value_capture. I will use raw_data to paste my raw data collecting from system log and value_capture to generate day by day hit statistics
Raw_data tab:
It has three details, 1. date. 2. name and 3 totals hits(This total hits are overall hits of that user on all days)
value_capture:
This has all user names and all dates to mark each day and a column to display all hits.
Now, considering an example of date 20141120, all users hits are 0.
raw_data shows 0 against all users
so value_capture are has to be updated with same
Now on next day 20141121, there is some hits against some users. (Note : if there is no hit against a user, it will only the previous date ie,date will update only if there is a hit on that day)
value_capture updated with that details
On 20141122, there are some more hits against the users
value_capture updated with the details. since the raw_data will show total number of hits against a user, the way of calculating on a particular will be total hits- previous date hit
I am stuck at a way to develop the excel formula here.Looking for an idea to implement this.
Note :
Users name will be fixed on both tabs, but that may come in different order in raw_data tab.
UPDATE
My raw_data sheet will be updated everyday and I want my formula results be fixed in the value_capture sheet after the values are updated (removed) from sheet raw_data
So as i mentioned in my answer to your previous question, the only way to save the results of a formula after deleting/removing its reference cells, is to use a macro.
First of all
you need a proper formula to pull the data from the raw_data sheet and then calculate the difference between that particular cell where the formula resides and the sum of previous cells in that row.
we need two formulas:one for the column B of your value_capture sheet. and the second one for the rest of your table. why? because in the first column you don't calculate the difference between itself and the previous cells, because there aren't any. So there is a little difference in the formula.
FORMULA ONE:(paste in B3 and drag and fill down)
=SUMIFS(raw_data!$C:$C;raw_data!$B:$B;$A3;raw_data!$A:$A;B$2)
FORMULA TWO:(paste in C3 and drag and fill down and to the right)
=IF(SUMIFS(raw_data!$C:$C;raw_data!$B:$B;$A3;raw_data!$A:$A;C$2)-IF(ISNUMBER(B3);SUM($B3:B3);0)>0;SUMIFS(raw_data!$C:$C;raw_data!$B:$B;$A3;raw_data!$A:$A;C$2)-IF(ISNUMBER(B3);SUM($B3:B3);0);0)
As you see in my screenshot for user in cell A3, we have value of 4 for the date 20141120 and the next day the value is 14 but the cell gets the difference, that is 10, and then then next day, the value increases to 16, so the cell value for D3 becomes 2 which is the different of 16 for that day and the sum of previous ones. This continues until today's value which is 24 so the cell gets 7 because the sum of previous ones is 17.
Now that you have your values captured correctly, with respect to the difference with the sum of previous days, it is time to make them fixed and eternal.
For this you need a macro:
Sub saveMyData()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Range("B3:L18").Copy
ws.Range("B3").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
So obviously you need to update this range "B3:L18" this is what I have in the example sheet only.
Now this macro, copies and pastes all the values from that range onto itself, so you lose your formulas.So each day, you need to go to *line 21** of the same sheet and get the formula from C21 and paste it on somewhere on row 3 from where you want to continue capturing the new day's data. (you won't need the first formula, because it is only used once in the first column, the first day.)
This was the only way to capture the values using formulas and then save the results of the formulas in their place and go and fetch new data. I couldn't think of any other strategy to make it more automatic, except for writing mova VBA and letting excel go and find where you left the day before and paste the formulas in their right place and repeat the task.
all these formulas are array formulas, so press ctrl+shift+enter to calculate the formulas.
Remember to adjust the formulas to your regional settings, i suppose, by replacing the ";" with ",".
here is the example sheet downloadable from this link
Tell me if you have problems anywhere and don't forget to answer my question in the comments about the way you update your dates in row 2 of value_capture.
The formula that you are looking for is SUMIFS
=SUMIFS([SUM RANGE],[RANGE 1],[VALUE 1],[RANGE 2],[VALUE 2])
SUM RANGE is the column of values that you want to SUM so that is your column C. For Range 1 you want to select the range of IDs. The VALUE 1 will be equal to the value in Column A Row X. For RANGE 2 it will be the Date column. For VALUE 2 it will be the value in the column header.
Put this together and you have something akin to:
=SUMIFS(raw_data!$C:$C,raw_data!$B:$B,$A3,raw_data!$A:$A,B$2)
This would be in cell B3 and you could fill the other cells from there.
If total hits for a day is a rolling count of ALL hits ever then at the end of your equation you need to subtract the sum of the prior days.
For example:
=SUM(B3:B4)
This would only need to be added to subsequent columns (not the first).
So the equation in C3 would be
=SUMIFS(raw_data!$C:$C,raw_data!$B:$B,$A3,raw_data!$A:$A,C$2)-SUM(B$3:C3)
Then in D3 it becomes
=SUMIFS(raw_data!$C:$C,raw_data!$B:$B,$A3,raw_data!$A:$A,D$2)-SUM(B$3:D3)
Try Sumifs formula
=SUMIFS(raw_data!$C:$C,raw_data!$B:$B,$A3,raw_data!$A:$A,B$2)-IF(ISNUMBER(A3),A3,0)

VBA Function able to search through cells?

Back Story
At my job we use a desk calendar to keep track of a multitude of random details that occur: Everyone's daily hours, what type and how many reports were sent, etc. To help us in filling out a few forms my boss decided he wanted to create a spreadsheet for the month and keep it as up-to-date as possible day by day. He went on vacation before this month started, so I've been taking over his duties. Something I noticed is that he hardwired the calculations for the totals (everyone's total hours, total reports sent, etc), so that when I changed the dates from January to February we had some left over ones that lead into March. I deleted them and then the formulas all threw #REF! errors because of it.
How the data is set up is simple:
AB 2.50
CD 3.50
EF 8.00
...ETC...
With the AB,CD,EF being in one column, and the values being in the adjacent column.
The Question
I tried to get this to work, with no luck, so I'm thinking it might be impossible but I thought I'd ask a group of people who know more about the subject than I do: in a function I made I have it accept a string parameter to search for, if it finds it, it moves to the adjacent cell and adds that value to the running total. It then returns that number at the end, so a call to this function would resemble
=getTotals("AB") ' this would display whatever the total for AB is
But when I tried this all I got was a #VALUE! error in the cell.
Is there any way I can get this to work the way I want it to, without the use of a full macro?
The Function
I got this function to work, how I wanted to
Function getTotal(rng As Range, search As String) As Double
Dim r As Range
getTotal = 0
For Each r In rng
If r.Value = search Then
getTotal = getTotal + r.Offset(0, 1).Value
End If
Next
End Function
Calling it like so: =getTotal(A1:D2, "AD")
SUMIF should get you what you need:
=SUMIF(A1:A10,"AB",B1:B10)
Assuming your 'AB' values are in column A, rows 1-10 and the number values are in column B, rows 1-10.
The first argument in SUMIF is the range to check, the second argument is the condition that must be met (search criteria), and the 3rd [optional] argument is the corresponding range from which to get the values to be summed.