Automating the mathematical tool - vba

I have developed a calculator in "TOOL.xlsx" file. It takes 4 inputs and returns 2 outputs. The calculation is performed on the calculator sheet in the "Tool.xlsx" workbook. 4 inputs correspond to 1 data set. I have another Excel file named "DATA.xlsx" that contains around 20,000 datasets (4 inputs per data set) and it also has an output column that collects the output. I would like to automate the "DATA.slsx" and "Tool.xlsx" interaction so that the inputs for the "TOOL.xlsx" are automatically called from the "data.xlsx" and the output column in the "data.xlsx" are filled with the outputs of "TOOL.xlsx". I would really appreciate any help because I am really stuck up here. PS: I an new to VBA.
screenshot that ilustrates my problem

I would like to see the formula behind the input/output cells
and need to put those formula in second sheet.
to give a try, use
=SheetName!CellAddress in second sheet input/output cells

Related

Vba email generator, subject from excel spreadsheet

I have a vba code that generates an email. I would like the subject to be the data from the first and last cells in my list. The thing is, my list isnt of a set length, sometimes it contains 5 pieces of data sometimes 8 etc. How do i tell vba to pick the first and last cell no matter the length of the list?
thanks
For me, best practice is to just have cells on your sheet that calculate the first and last row (different ways you can do that), then give those cells a range name such as FirstRow and LastRow. In your vba then you refer to these cells to make your code dynamic.
e.g:
firstRow = Range("FirstRow)
lastRow = Range("lastRow")
test = range(cells(firstRow,lastRow))
-- Note I have not written VBA in many many years so am writing the above from memory so it may be not be exact.
Of course you can do it all entirely in VBA using the xlDown method mentioned previously but I prefer the transparency of it being on the main page so that easily spot if something breaks.
Range("A1").End(xlDown).Value
Where the cell is where you want to start and the End part moves all the way to the end

Data Mapping Between Excel Files and Third Part Applications

I'm working on a macro which named "MasterTool". All of macro code will be in this "MasterTool". This tool's main role is taking value of certain string name from excel sheet (input sheet) and write this values another excel sheet (output sheet) for same string name.
Additionally there's 2 different term I would like to describe you from now to understand easily.
Input Sheet
Excel file which stores some specific informations (E.g. Vehicle Height : 3m ) ( One of the cell has my string value: Vehicle Height and adjacent next cell has it's value 3m )
Output Sheet
This is also an excel sheet like report page which contains also specification information which comes from Input Sheet.
I wonder is it common usage data mapping methods in Excel VBA ?
My first attempt is:
Naming cells in Input Sheet like that: strCellVehicleHeight and I'm writing string name: Vehicle Height, also naming cell of this feature's value strCellVehicleHeightValue and I'm writing 3.
On output sheet I named cells same and then mapping via cell names. It's a bit hard way to use I think.
Isn't there more possible ways or other formats like XML JSON etc. I would be glad if you help me about this situation.

Read formatted text file from excel with VBA

I have a huge text file with lots of numbers divided into different sections and I want to extract only certain values. It is something like this
step 1
787268 4.29430E-01
787269 4.05248E-01
787270 3.99742E-01
787271 3.99136E-01
787272 3.98422E-01
787273 3.97019E-01
787274 3.95237E-01
step 2
787268 4.29430E-01
787269 4.05248E-01
787270 3.99742E-01
787271 3.99136E-01
787272 3.98422E-01
787273 3.97019E-01
787274 3.95237E-01
I want to copy into my excel file only the two columns in the step 2 section.
So I need a VBA code that allows me to search for a particular string and after it finds it copy and paste all the raws until the next step.
Any pieces of code?
Thanks
Stefano
You can use this website to help you find what you need, a simple Google search can go a long way.
I would suggest using an if found section to find the spot where you need to copy over.
Ex.
If (Range("A1").Value = "YOUR TEXT HERE") Then
'''' COPY OVER DATA
End If

grab and filter from more than 255 columns from a huge closed workbook

i have a huge workbook (0.6 million rows) and 315 columns whose column names i need to grab into an array. due to the huge size, i don't want to open and close the workbook to copy the 1st row of the range. Also, I want to only grab certain columns from the 1st row that begin with the word "Global ".
can anyone help with short code example on how to go about doing this? please note i have tried ADOX, ADO etc but both show the 255 column limitations. I also dont want to open the workbook, but pull the required "Global " columns from the 315 columns into an array.
any help is most appreciated.
You can copy the first row of your target by opening a new workbook, and in A1 use this formula:
='C:\PATH_TO_TARGET\[TARGET_FILE_NAME.xlsx]WORKSHEET_NAME'!A1
Note that PATH+FILENAME+WORKSHEET is enclosed in single quotes, the FILENAME is enclosed in square brackets, and an exclamation separates the cell reference.
Then copy/Paste or fill right to get the next 314 columns. Note: this formula will return zero for empty target cells.
Once you have the column heading you can copy/paste_special_values if you want to destroy the links to the closed workbook.
Hope that helps
You could use the Python programing language.
While it does not actively works with XLSX fiels, you just have to install the openpyxl external module from here: https://pypi.python.org/pypi/openpyxl -
(You will also have to install Python. of course - just download it from www.python.org)
It will make working with your data in an interactive Python session a piece of cake, and the time to open the workbook without having to load the Excel interface should be a fraction of what you are expecting. (I think it will have to fit in your memory, though).
But this is all I had to type, in an interactive Python2 session to open a workbook, and retreive the column names that start with "bl":
import openpyxl
a = openpyxl.load_workbook("bla.xlsx")
[cell.value for cell in a.worksheets[0].rows[0] if cell.value.startswith("bl")]
output:
Out[8]: [u'bla', u'ble', u'bli', u'blo', u'blu']
The last input line requires on to know Python to be understood, so, here is a summary of what happens: Python is a language very fond of working with sequences - and the openpyxl libray gives your workbook as just that:
an object which is a sequence of worksheets - each worksheet having a rows attribute which has a sequence of all rows in the sheet, and each row bein a sequence of cells. Each cell has a value attribute which is the text within it.
The inline for statement is the compact form, but it could be written as a multiple line statement as:
In [10]: for cell in a.worksheets[0].rows[0]:
....: if cell.value.startswith("bl"):
....: print cell.value
....:
bla
ble
bli
blo
blu
Keep in mind that by exploring Python a bit deeper, you can programatically manipulate your data in a way that will be easier than ininteractivelygiven a data-set this size - and you can even use Python itself to drop select contents to an SQL database, (including its bult-in, single-file database, sqlite), where sophisticated indexes and queries can make working with your data a breeze)

Newbie to VBA, Need a tricky function

Thanks in advance for any help...
(This is for a military unit to help keep track of which Service Members need to do which trainings.)
I need to take a bunch of data (on different sheets, all exported from the same location and formatted exactly the same) and copy them into one master sheet, but with the data inverted...
I have a bunch of sheets (like 25) with a list of names (and other administrative data) that shows who has completed a specific training, each sheet being for a different training. Instead of deficiencies by training, I'd like to have deficiencies by name.
The exported data is always formatted like so:
Title of Training
Last 4 Last Name First Name Rank Unit MOS Skill Level Most Recent
xxxx Last First RNK Unit MOS SkillLevel Date
I'd like a Master Sheet that looks like this:
Training Deficiencies
Last 4 Last Name First Name Rank Unit MOS TitleA TitleB etc
xxxx Last First RNK Unit MOS x x x
xxxx Last First RNK Unit MOS x
xxxx Last First RNK Unit MOS x x
Skill Level and Most Recent will be different for each training, and are irrelevant for the master sheet. The Titles (TitleA, TitleB, etc) can each be copied from Cell(1,1) of the respective sheet, and is also the title of the sheet itself.
I created a sheet with everyone in the unit, so my first step is to create the master sheet (I just delete the whole sheet and create it again):
Sheets("MasterSheet").Delete
Sheets("DataSheet").Copy After:=Sheets("DataSheet")
ActiveSheet.Name = "MasterSheet"
Cells(1, 1).Value = "'Master Sheet"
What I can't figure out how to do is loop through the Last4 on the Master Sheet and compare them against the Last4 in each training sheet.
So for each training sheet:
I want to create a new column on the MasterSheet with the training title as the column heading
Loop through each name on the training sheet and find that Service Member's row.
Put an "X" in the respective training column for that Service Member.
After that, I'd like each Service Member to be highlighted Green, Yellow, or Red based on the number of X's in their row. I think once I get the data copied I'll be able to figure out the highlighting and other formatting.
Thanks again for your help. I can't begin to express how much time this will save (it's currently done by hand). I've been on Google all day and I can find snippets of stuff but I don't really know how to put it all together. I'm using Excel 2007. I have an IT background and understand some code, but it's been a very very long time since I've done some real coding, and VBA is really confusing me.
Thanks again for your help in advance!
I would do like this
create a scripting.dictionary
add all last4 from all sheets to the dictionary
loop the dictionary and output all keys on the master sheet and set the value for each key to the row number
loop all sheets again and look up the row number in the dictionary and set cell to 1
create a formula to sum per row and use conditional formatting for colors
(And use cell format "'x';;' if "x" is a hard requirement)