Excel VBA - other sheets - excel

I'm trying to make a Excel VBA that gets 100 lines per time in a table and send them to a new excel file separately, but I don't know how. Is there a way?
There are 2000 lines in the table, so I want 20 separate files.

Excel has some special cell types, one of which returns the cell showing the maximum extent of data in the worksheet. Research xlCellTypeLastCell in the help file. The returned value does not mean there is data in that particular cell, but that there is no data in rows or columns past that row and column ref.
Eg You can select the last cell in a worksheet with the line below.
ActiveCell.SpecialCells(xlLastCell).Select
Then use the row number of that cell to set the bounds for your loop.

Related

Get value from certain cell in row/column match from another excel file

I have multiple excel files that should "report" to a single one.
Imagine this example:
Excel 1)
Excel 1 is the one that receives the whole information from the other files. In that cell with the green circle, it should get the value which has row="6" and column="JAN" on other excel (Excel 2).
Excel 2)
In this excel there is a cell that has exactly the row="6" and column="JAN". The value does not match the cell number of each excel (in this example, the cell I want to fill in Excel 1 is E21 and in Excel 2 is E20).
Plus, I want this to be dynamic. If I now understand that the KPI I want to show in number 6 is another one I should change the number in Excel 2 and it should be getting that new value in Excel 1.
Was this clear? I'm sorry, English is not my primary language and I understand if it is a little bit confusing.
Thank you.
I was using the index function but that is not perfect for my case because if I change the KPI to another row it won't work anymore.

Select Rows from Excel without knowing the exact Range

I am trying to select some Rows from an Excel file.
Normally, I would use the Column Range (i.e. A2:A20), the problem is, this method needs to work on every Excel file, where the start of the column is always A2, but the end of it is always different.
Some file would have a first Column of 20 rows, some others would have 40 or 60 rows.
For this reason I can't use an exact range of cells.
Is there any way to select all cells in a Column, until last non empty row?
I'm using OleDb to load the data from the Excel files.

Excel drowpdownlist in different cell after running VBA code

I have created a relative large Excel file. There are about 500 rows and 2 columns with a lot of text in each cel. Some cells contain a dropdownlist. Al the data is put in a table.
After I sort the table, somehow the drowpdownlist of one cell shifts to another cell, what shouldn't be possible.
Does anyone have an idea what the problem could be?

cell formatting not applied after formula

I'm using NPOI.
I read in an Excel workbook with two sheets that acts as a "template" for the result workbook I'll be generating. Sheet 0 is empty aside from some header rows and sheet 1 has a number of rows of formulas. The formulas generally refer to the sheet 0 and pull data from it.
I can't write directly into the template workbook because I'd be having to move rows out of the way and it would get ugly fast, so in code I create a new result workbook with two sheets. Based on a preset configuration file, I populate the sheet 0 of this result workbook with data, row by row, cloning the style and formulas of certain rows from the template workbook. Sheet 0 ends up filled with data, and sheet 1 is basically a row of formulas copied and adjusted relatively, typically pulling data from sheet 0 with a small calculation here or there.
All of my data in sheet 0 is text and cannot be interpreted as numeric, dates, etc. So I set my data cell types as String.
In code I then loop through all cells in sheet 1 and EvaluateInCell.
I do this to obviously evaluate the formulas, but also to remove the formulas and leave the copied/calculated results. This is just a requirement of the work I'm doing. We deliver the end results and no formulas.
I save the resulting workbook.
In general things look good, except where I have cell formatting, the formatting seems to not be applied.
The formatting IS there when I get the cell formatting properties in Excel.
For example I might have text data that is a date-time in my data that shows up as:
7/7/2016 9:54:55 AM
this IS what the original data text is, but on my formula sheet I have a custom cell format yyyymmdd.
And yet, the cell still shows: 7/7/2016 9:54:55 AM
In Excel I then do something like manually edit the value, for example delete the last 'M' and retype 'M' and hit enter, that cell changes to the desired format and shows:
20160707
So again, the formatting IS there, it's just not applied.
I don't want to have to manually edit cells, or do anything once the workbook is opened in Excel by my customers. I want the resulting workbook to open up with the values formatted.
I've tried a few things that seemed like a shot in the dark, like:
this.ResultWorkbook.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll();
((XSSFWorkbook)this.ResultWorkbook).SetForceFormulaRecalculation(true);
But this didn't help.
Any thoughts?
can you try either of this apprioach while setting the formula for cell
Approach 1
ICellStyle dateCellStyle = workbook.CreateCellStyle();
dateCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("yyyymmdd"); // Or prefix single quote for data when writing it to excel file like 'yyymmdd ex: '20160707
Approach 2
XSSFCellStyle dateCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
XSSFDataFormat dateDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
dateCellStyle.SetDataFormat(dateDataFormat.GetFormat("yyyymmdd"));
I ended up looking at the template Excel file's cell I'm writing into. It looks like if it's not DateUtil.IsCellDateFormatted and the cellStyle.DataFormat is not 0x31 (text based on BuiltInFormats), then it must be numeric.
Based on that I try to convert my text to DateTime or double as applicable, and call SetCellValue with those converted variables. Otherwise I write the text.
This seems to be working for cases I've encountered.

Getting data to input into a spreadsheet by clicking on a button

I'm struggling to get my Excel spreadsheet (2010) to do what I want, but I'm not sure its possible!
What I have is diagrams with numbers (with the numbers in circles) on sheet two, on sheet one I have VLOOKUPs waiting to input the appropriate data. What I want, is for the user to click on the circle containing whichever number, then for that number to be put in the first column of the sheet, thus causing the VLOOKUP to fill in the rest of the data. Any ideas? I'm trying to play about with macros but I've had no luck so far.
Any help would be greatly appreciated
Jazz
EDIT: I've managed to get a macro to copy and paste the data to the correct column, what I now need is for the macro to put the pasted data into the next blank cell in the column
What if you try a command like the following:
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
After you have unlocked the worksheet and copied the data, have the VB code go to the active sheet, and paste your data in row that follows the last used row in the worksheet. If you can weave something like this in, you will be able to avoid indexing the rows with a number. You will also be able to deal with the case that there are empty rows sprinkled within column A in this case.

Resources