Should Loop be used if the source data is always changing? OR is there a better method? [closed] - vba

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
So in sheet 1 i have lets say formulas from cell B2 to E6 f(CN#) that is dependent on the values in A2 to A6, lets call them customer numbers (CN#).see picture below.
these customer numbers would be the same for all the formulas in columns next to it so the whole range is A1-E5 These lines of formulas can increase or decrease monthly so it could be in any given month A1-E5 or A1-E8 or A1-E3.
My problem is that i need to copy and paste these ranges one after another and change the customer numbers or CN# each time a new range is posted. These CN# are located on sheet2
Sorry for the bad grammar but this thing is kicking my butt
Edit:
the formulas f(CN#) are all different. I guess i should explain what i am doing currently.
I take A2-E5 and copy it down to A6-E11. I then change the Value CN1 to CN2. wash rinse and repeat till i get to my last CN#.
If the number lines and columns were static that was an easy fix, but they are not and they change monthly.
The issue is repeating the formulas (B2toE6) which varies then copying them. lets call that a set. so i'd need a set for each customer number. i was thinking of using a dynamic range like
Range(Cells(1,2),Cells(1,Columns.Count).End(xlToLeft)).Select
but im lost from that point forward.

You will have to have a helper cell somewhere: something that tells excel how many times to repeat - this will mean changing a single cell, rather than trying to work things out for multiple cells.
I made a couple of assumptions, which should be relatively easy to modify.
Your list you want to make starts at A2
Your list of customers starts at Sheet2!A2
Your helper cell is at B1
Your formula, starting in A4 and running down would be:
=OFFSET(Sheet2!$A$2,INT(ROW()-ROW($A$2))/$B$1,0)
The list will start producing 0 when you run out of customers. If you wanted to be tidy, you could wrap the function in an IF statement
=IF(OFFSET(Sheet2!$A$2,INT(ROW()-ROW($A$2))/$B$1,0)=0,"",OFFSET(Sheet2!$A$2,INT(ROW()-ROW($A$2))/$B$1,0))
Or you could use conditional formatting to set the cell color and the font color to be the same to make it "hidden"

Related

Use VBA loop to lookup values from closed workbook using reference and paste in active workbook using same reference ID [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've gone through many iterations of the logic and can't seem to put it together... here is the goal:
I need to import data from ten workbooks, that are each updated daily with a new sheet titled by the convention "9/28/17"... and copy this information into a master sheet that grows by a column per day.
I think the code should be structured where it loops through the ten defined ranges in the defined sheets looking for a reference value (in an absolute location, first column of master sheet) and returning a value from the closed workbooks that is offset a few columns from the reference id to the right of the reference...is this possible? If so, how would I write this into the code?
It seems to me that the userform needs to include the ability to:
define a location for each of the 10 workbooks (so I can update each month)
the date (each workbook contains one month of dated sheets)
add a column on the master sheet to put the retrieved values into, with the date in the first row
I would greatly appreciate help in determining an appropriate process for the code to follow. I have pieces of code that would do parts of this using hardcoded references, but due to the possible addition of rows to the database I like the idea of a lookup that would eliminate the need to change the code and provide validation that the data is going in the right places.
This is the structure of the code you would need to write.
Sub Project_Plan()
' The Master workbook contains this code
' Assign this workbook to a Variable
' create the column in which to store the day's data
' identify that location by variables
' write the date in the first row
' (the same date defines the names of the worksheets in the 10 workbooks)
' Provide the location of the 10 workbooks
' Provide their names
' For Each of the 10 workbooks
' Extract the data from the daily sheet
' Write the data to an Array
' Find the location in the defined column of
' the Master workbook and write the data there
' using the array as a source
' Next Workbook
End Sub
80% of the work described above takes 20% of the total time you will have to devote to the project's completion. 80% of your time will go into preparation of minute detail, such as how to determine the last row of a column where you don't know how many entries there will be.
You seem to have completed about 20% of the preparation for the 20% of the project. You have reached a point of vantage where you can see a myriad of questions coming towards you which you don't even know how to formulate. On this and other forums help is available only after you found the questions.
To further advance your preparations you will need to start. I suggest you take the blueprint I have provided above (which I guess isn't so much different from what you saw yourself) and work your way down. As you come nearer to a solution you will gain a better view of the questions you will need answers for.
My guess is that you will need about 50 lines of code which will throw up about 20 serious questions. Once you found them you will get answers here rather quickly.
Start with a blank code sheet. Write Option Explicit at the top, give a meaningful name to your main procedure, and start declaring the variables you will need for the first step. The better the names you give the more time you will save using them.

VBA Excel Convert 3 Columns into an Arrary [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have been searching for a way to do this and I feel like I'm hitting a wall. . . I'm new at VBA, so I may not be going about this the right way.
I have a table that looks like this:
I need it to look like this:
The client needs to use this data for various purposes. They tried a Pivot Table, but they hated how it looked and their end users didn't like it either. They also do a lot of other tweaks to the data after it's in the final format.
I was able to get the Names and the Questions to copy/paste to a new sheet in the correct layout. I was then trying to do a For Each loop to filter the Name and copy/paste/transpose the answers to this new sheet. I went this method because the number of questions and names vary all the time.
I do not understand the code I've been seeing and I'm unsure how the older examples I've found online can be manipulated to match my case.
Any suggestions? Or is there another post that is doing something pretty close to what I'm trying to do that I've missed?
Thank you!!
You can do this simply with Power Pivot (a free MS add-in in 2010-2013 (aka Data --> Get & Transform in 2016)
Merely
select the Questions Column to Pivot (create new columns)
Values column --> Answers
Advanced Options Do not aggregate
This is the result using your data as posted:
The only formatting change I made was to turn off the Filter Button under Table Options.
Asuming that the names are sorted and every name does have the same questions, you could run a simple "double match -1" to get the answer as a worksheet formula. (Having list 1 in sheet1 and list 2 in sheet2)
=INDEX(sheet1!$C:$C,MATCH(B$1,INDEX(sheet1!$B:$B,MATCH($A2,sheet1!$A:$A,0)):sheet1!$B$99999,0)+MATCH($A2,sheet1!$A:$A,0)-1)
Formula for B2. Fill right and down.
If you don't want the formulas, hide the sheet and copy/paste values only via VBA to another sheet.
Names do not need to be in complete order but being together "aabb" and "bbaa" will work but "abab" or "baab" will not. Questions do not need to be in any order, but having no "q1" for someone will give him the next "q1" which is found by another name.

Searching an excel with two search words (multiple lookup) using vba macro [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am using vba macros.
Task:
1) User will give two inputs viz. number of devices sold (column B) and week number (column A).
2) Search the excel file using these two and get the earnings.
(Conditions: 1. We dont have to modify anything in the excel file.2.And we have to first search devices sold and then we can go to week number.)
Note: Actually i have a different problem, this is generalization for that.
I used find function but I dont understand why its working.
Please tell what is to be done.
Consider the input is 11 and week 3 (so search for 11 and then check if its corresponds to week 3 or not. If not, move to next. I tried this but ended up in an infinite loop.)
You are looking for a Lookup with multiple criteria. A quick search gives several useful links, listed below. I have marked with * those that I have answered.
I have filtered out some of the links that actually deal with other issues.
As seen in the links, there are several options to achieve your goal. I personally prefer those that do not need array formulas, using an answer to question 1 with LOOKUP or an answer to question 2 with SUMPRODUCT.
Since you mentioned that you need a VBA solution, you can use WorksheetFunction to use an Excel formula in VBA code, or check answers to question 7.
Multiple Column vs Multiple Column Lookup *
Multiple-column based lookups in Excel *
Lookup Database (Multiple criteria)
Excel Lookup with multiple queries
Multiple VLOOKUP LOOKUP IF Statement?
Returning a value if three columns match in excel *
VLookup multiple columns
Comparing two columns in one Excel sheet, to two columns in another sheet, and if they match, copy data from another column *
Macro for vlookup with 2 vertical criteria *
http://spreadsheets.about.com/b/2014/01/08/excel-lookup-formula-using-multiple-criteria.htm
http://fiveminutelessons.com/learn-microsoft-excel/use-index-lookup-multiple-values-list
http://blogs.office.com/2012/04/26/using-multiple-criteria-in-excel-lookup-formulas/
http://www.xl-central.com/lookup-multiple-criteria.html
http://www.get-digital-help.com/2012/05/22/lookup-multiple-values-in-different-columns-and-return-a-single-value/
PS: The (probably partial) list of (probably) duplicated questions provided above shows the usefulness of what was proposed in https://meta.stackexchange.com/questions/211366/group-duplicate-questions-for-convenience
Concatenate week and number of devices and then search like usual one keyword search.
You dont need any VBA for such a simple thing. Use this array formula:
=INDEX(C2:C5;MATCH(1;(B2:B5=11)*(A2:A5="week 3");0))
Dont forget to enter the formula with Ctrl Shift Enter (it is an array formula)

Using a cell reference entered into a cell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In cell K1 VBA inserts a cell reference that changes based on certain conditions outlined in the vba code.
For this example lets say the value of this cell is $A$13
Now in cell K2 I want it to equal what ever is in cell reference provided by K1.
So in other words how do I use the cell reference provided in cell K1 in excel formulas because when ever I press =K1 I receive the value of the cell i.e. $A$13 and not the value of cell $A$13
You want to use the function INDIRECT()
=INDIRECT(K1)
Please note this however: INDIRECT() is a volatile function, meaning that it will recalculating more often than necessary because of the uncertainty of the reference. For lighter workbooks this poses no problem. However, for more complex, heavier workbooks this can heavily slow the calculations. I have seen workbooks take 3-4+ minutes to open only to take ~5 seconds to open after eliminating all calls to INDIRECT().

How to reference a value to a range of numbers in another list then have values returned from 2 different places where the value is found [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I hope my title made some sense. In essence I am trying to do this with very little programming experience and I understand now that I very much need help. I have been attempting to use Visual Basic because I am trying to sort through a very large list of unrecognized genes. Overall, I am trying to check whether ANY of my "midpoint" (original gene location) values lies in-between any row's "startpoint" and "endpoint" (known gene locations). If it does, then I need both code and name returned to me somehow, even if it matches in more than one row. To make matters worse, it should only do this if its "midchrom" matches the respective row's "chrom".
The list happens to be 3k rows long, hence why I would like to do it this way. I hope I explained myself well enough.. An example of the list is copied at the bottom (I hope csv is fine, first time asking here). I have been able to write no code at all since I don't know how to approach the problem at all, and I've been at it for 2 days, so I have sadly nothing to post.
Any suggestions appreciated! Thank you!
midchrom,midpoint, code, chrom, name, Startpoint, endpoint
chr1, 10052729.5, NM_018896,chr1, Name1, 117328105, 117475235
chr1,10249243,NM_001177888,chr1,Name2,109035529,109052409
chr1,1103696.50,NM_001256327,chr1,Name3,125766078,125845734
chr1,1112610.00,NM_001256360,chr1,Name4,125766078,125845734
chr1,11947610.5,NM_198383,chr1,Name5,12470846.00,12460843.00
chr1,12249877.50,NM_198396,chr1,Name6,125766225,125845740
chr1,12383427.00,NM_001256325,chr1,Name7,109035526,109052415
chr1,12410787.50,NM_020084,chr1,Name8,12383158,12451963
chr1,12470843.00,NM_001003406,chr1,Name9,12383158,12451963
chr1,12494320.50,NM_009783,chr1,Name10,109035538,109052412
chr2,12529476.00,NM_171733,chr1,Name11,125766225,125845740
chr1,12809629.00,NM_001256334,chr1,Name12,125766078,125845734
chr1,12909892.00,NM_001256329,chr1,Name13,175711654,176178148
chr1,13200078,NM_001029326,chr1,Name14,175711654,176281155
chr13,13706259.5,NM_001256328,chr2,Name15,192931857,192942982
chr1,1451461.5,NM_001193140,chr2,Name16,192931857,192942982
chr1,14004645.5,NM_001256359,chr1,Name17,176007934,176281155
chr1,15906995,NM_031601,chr1,Name18,142223122,142606517
chr1,15956960.5,NM_001044308,chr13,Name19,13709000,13706230
As pnuts suggested, you can do this without VBA by creating a matrix:
Add two rows above your data
Tranpose columns "midchrom", "midpoint" and "name" to top-right of your data as in the screenshot. use Paste Special->Values & Transpose
Place the following formula in the top left of the new "table/matrix": =IF(AND(H$1=$D4,H$2>=$F4,H$2<=$G4),H$3,"") - and copy it down and across. If it's 3k gene, the calculation might take a bit, but shouldn't be too long
Every row now shows the names of all genes whose' midpoints lay in between the start- and endpoint of the row's gene (assuming it the same chromatin)
Here's the sample file.