macro code to show much input range in dropdown list - vba

I need a macro code to display more than one input range in dropodown list that I created in sheet2 via the form control.
I want an event with IF logic, where I have many names range from another sheet with the name eg: DaftarA (in sheet1 C1:C30), DaftarB (in sheet1 C40:C60), DaftarC (in sheet1 C70:C90).
How to write macros if the value in sheet2 C1 1, the dropdown list will display the input range of (name range) DaftarA, if the value is 2 then displayed in the dropdown list is (name range) dDftarB and so on ..
is there anything that can help? thank you

You can define a name for the list through the offset formula and use that name as a list for data validation drop down box.
=OFFSET(Sheet1!$B$1,Sheet1!$F$1,0,Sheet1!$E$1,1)
where cell B1 is top of the list, cell F1 shows how much you offset down, cell E1 will control the length of the list. It will work however only if your multiple ranges are on the same sheet and in the same column

It will also work if you define a name with the following formula
=IF(Sheet1!$F$1=1,test1,IF(Sheet1!$F$1=2,test2,IF(Sheet1!$F$1=3,test3)))
However in this case you need to name each particular range you want(like in this example test1, test2, test3) and of course you can't put too many nested formulas as it will get messy.

Related

Index, Match and Address functions in a formula but how do incorporate a certain condition to extract only certain data?

My Workbook has three sheets: "Wire Sheet", "Paste Data" sheet and "Wire instructions"
I need the "Wire sheet to dynamically populate with the data from the other two sheets based on certain conditions, i.e. date and data from that date.
I am using the index, match address functions to find the names from the "paste data" tab based on a specific but I can't figure out how to put a condition in there to only return names that are "buys"...
Here are my formulas:
This is just to find the cell:
S17= =MAX(IF('Paste Data'!B:B = 'Wire Sheet'!K2, ROW('Paste Data'!B:B) - ROW(INDEX('Paste Data'!B:B,1,1))+1))
S18= =ADDRESS(MATCH(K2,'Paste Data'!B:B,0),2,3,1,"Paste Data")&":"&ADDRESS(S17,5,3,1)
S19= =INDEX(S18,1)
here is the formula i am using to return the data:
=IF(INDEX(INDIRECT(S19),1,4)=0,"",INDEX(INDIRECT(S19),1,4))
Example on how spreadsheet works:
Currently: if you place a specific date in cell K2 on the "wire sheet", the boxes (cells A18:O67) populate with the data from the other two sheets based on that specific date. The data coded "Sell" is being populated in the boxes on the wire sheet, but should not be.. here is an example:
For example: If you place the date 7/13/2018 in K2 cell, the cell B57 is populated with letters SCR, which shouldn't be the case, as if you go to the "paste data" sheet, this is a "sell".
enter image description here
Then if you place the date 7/16/2018 in K2 Cell -- the cell B18 is doing the same thing, its populated with WQ when it shouldn't be, as it is a "sell", on the "paste Data" sheet. I would like it to just skip over this row of data and go and find the next row that is coded "buy"..
enter image description here
I just want to place the data that are "buys", on the wire sheet. All the sells should be passed over. I am not sure how else to explain this, which might be part of the issue.
enter image description here
Thanks in advance.
https://my.pcloud.com/publink/show?code=XZNzcB7ZpIigcTAabUz2vpPaXQ4nSVUfdc2X
+-----[editRevision2]----+
My way :
Y5 =COUNTIF('Paste Data'!B:B,'Wire Sheet'!K2)
Z6 =IF((ROW(Z6)-5)<=Y$5,ROW(Z6)-5,"")
AA6 =INDEX('Paste Data'!F:F,MATCH('Wire Sheet'!$K$2,'Paste Data'!B:B,0)+'Wire Sheet'!Z6-1,1)
AB6 =IF(AA6="Buy",AB5+1,AB5)
AC6 =IF(AA6="Buy",INDEX('Paste Data'!E:E,MATCH('Wire Sheet'!$K$2,'Paste Data'!B:B,0)+'Wire Sheet'!Z6-1),"")
AD6 =IF(AA6="Buy",INDEX('Paste Data'!K:K,MATCH('Wire Sheet'!$K$2,'Paste Data'!B:B,0)+'Wire Sheet'!Z6-1),"")
select Z6:AD6 and extend to D15, then
B18 =IFERROR(INDEX($AC:$AC,MATCH(A18,$AB:$AB,0)),"")
E19 =IFERROR(INDEX($AD:$AD,MATCH(A18,$AB:$AB,0)),"")
copy B18 to J18, B31, J31 ...
and copy E20 to J20, B33, J33 ...
+-----[ProviousEdit]----+
Make
B18 =if(SUMIFS('Paste Data'!$K:$K,'Paste Data'!$E:$E,IF(INDEX(INDIRECT(S19),1,4)=0,"",INDEX(INDIRECT(S19),1,4)),'Paste Data'!$B:$B,$K$2,'Paste Data'!$F:$F,"Buy")=0,"",IF(INDEX(INDIRECT(S19),1,4)=0,"",INDEX(INDIRECT(S19),1,4)))
and adjust the INDEX(INDIRECT(S19),1,4) accordingly for J18,B31,J31 ...
Hope that solves.. (:

Need to copy/paste row (x number of times) when cell value equals X

I'm trying to use Excel for a Jewelry Order Form.
In the order form (sheet1), a user may select from a cell that is formatted into a drop-down list, a number representing the number of stones in a piece of jewelry. For example, if there are 10 stones in a ring, then the user selects 10 from the drop-down list.
The details for each of the 10 stones needs to be captured in the order form (Sheet1). For example, each stone will have 4 data elements... a stone type, weight, color, cut... So I created the desired formatted row of data (in Sheet2) where each cell is a drop-down for a user to select from.
I want to create a control button to do the following actions:
Delete rows 19:150 in Sheet1
This will clear out any prior stone details that may be displayed.
Find the value in cell C13 in Sheet1
This value will be used to determine how many rows should be pasted/displayed
Copy row, range A2: D2 in Sheet2
This is template row data where each cell in the row is its own drop-down list.
Paste row in B19 in Sheet1
This is the template row pasted into an order form.
4a) Paste as many rows as the value in step (2) above.
For example, if the value in step 2 from above is "3", then the stone details row will need to be pasted 3 times in the order form.
The furthest I've been able to get is the creation of the control button, and the delete clause...
Private Sub CommandButton1_Click()
Sub deleteMultipleRows()
Rows("19:150").Delete
End Sub
For the delete statement, you should probably use Sheet1.Rows("19:150").Delete as that will ensure that excel knows which sheet to delete those rows from.
You can declare a variable and assign it a value like so:
Dim rowCount as Integer
rowCount = Sheet1.Range("C13")
If you try recording a macro for the copy and pasting, you should see some sample code.
Note:
If the result looks something like this:
Sheet1.Activate
Row(2).Select
Selection.Copy
You can combine such statements like so:
Sheet1.Row(2).Copy
Since most .Activate commands can be ignored, and .Select and Selection. commands can be combined (and should be in most situations). The Sheet1. added before the .Row(2) tells Excel to specifically use Row 2 from Sheet1. Without the qualifier, i.e. just Row(2), Excel would use Row 2 from whichever sheet happens to be currently active.
You can then use a variable and the value from the sheet to loop through either pasting or copy/paste combo like so:
Dim counter as integer
For counter = 1 to Sheet1.Range("C13")
'Add code for copy/paste here
Next counter
Or if you have the rowCount variable declared and assigned, you could use it here like this:
Dim counter as integer
For counter = 1 to rowCount
'Add code for copy/paste here
Next counter

Excel VBA Compare cell value to list and overwrite value in separate sheet

In a workbook I have, users either manually enter an account code or select one from a list and the account codes are placed in column C (C7:C446) in a sheet called "JE". The account codes look like this ####### - ### - ## - ######. In column D (D7:D446) in sheet "JE", there is a formula that captures the last 6 digits of the account code. In a sheet called "required_refs", there is a list of 6 digit codes in column A. If the value in the D column in sheet "JE" equals any of the values in column A of "required_refs" sheet, I would like the value in the D column cell to overwrite the cell value in cell D1 in a separate sheet called "references" (I know that may have been confusing, sorry)
Example: if the value of D25 matches any of the values listed in column A of sheet "required_refs", upon double clicking a red colored F25 cell, put the value of D25 (of sheet "JE"), and put it in cell D1 on sheet "references".
I've taken a crack at it as best I know how. I've placed this code in sheet JE:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim project As Range: Set project = Range("D7:D446")
Dim param As Range: Set param = Worksheets("references").Range("D1").Value
For Each cell In project
If project.Value = Worksheets("required_refs").Range("A:A").Value Then
Call gotoRef_ 'macro that simply selects/navigates to the required_ref sheet
project.Value = param
End If
End Sub
Thanks so much in advance for any suggestions on how to complete this. I can elaborate on this further if needed.
This will do what you want:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("F7:F446")) Is Nothing Then Exit Sub
Dim varReference As Variant
varReference = Columns("D").Cells(Target.Row).Value2
If Not IsError(Application.Match(varReference, Worksheets("required_refs").Columns("A"), 0)) Then
Worksheets("references").Range("D1").Value = varReference
End If
End Sub
Important Points:
Whenever working with event handlers, always limit the scope of the target range in the first line. Otherwise, it might not work correctly or it could slow done your spreadsheet.
Make sure your JE sheet column D values and required_refs sheet column A values are all either text or numbers. Otherwise the values won't be compared correctly.
Note the usage of Application.Match() instead of WorksheetFunction.Match() to access the worksheet function. This, coupled with the use of a Variant type variable, allows us to trap the error that occurs if the match fails.
You can always do this on the sheet. Consider the MATCH function. See here for how to use MATCH.
Or another great tool if you're searching for something in a table associated with a value in another column (not your case I don't think)--VLOOKUP formula. Place this formula in the D cell of the sheet you want to place the numbers in. VLOOKUP is in the following format:
=vlookup(lookup value,table_array,column index number, [range lookup])
The lookup value is the 6 digit code you're looking for (on the JE sheet)
The table_array is simply selecting the values you want to search for (required_refs sheet)
The column index number would be one, since the table only has 1 column. It's basically the column number of the value you're looking for.
And range lookup is for if you think there might be more than one place where it matches.
For your case I think it would look like this:
=vlookup('JE'!D1,'required_refs'!A1:A,1,FALSE)
Then just lock the values you want to keep and click and drag down.
Explanation for VLOOKUP here

How can i compare the data from a sheet to another?

As I have some columns with data in sheet 1 & the same column with approx. same data except some in sheet2 .So I want to compare the data from sheet1 to sheet2 & it should show something what will be easy that data is "Differ" on those cells from sheet1 to sheet2 .
By anyway I want to know where the data are different.
Lets assume your first sheet is Sheet1 and the 2nd is Sheet2,
Go to the cell a1 on Sheet2 and on home ribbon select conditional formatting -> highlight cells rules -> equal to -> and on the open window, enter the formula:
='Sheet1'!A1
press apply.
Now use the format painter brush when you are on the same cell on select all the range you have.
you can choose the color you wish to distinguish between the equal cells with non equal cells on the 'with' on the opened window

Insert row with data into excel sheet from another sheet dynamically

I have two different excel sheet first sheet contain some country list and second sheet is blank. There is a dropdown column with every country which contain 'Yes' 'No'. When I select yes I want that, the selected country row will automatically add to second sheet with all data. When I select more country they will dynamically insert/add to second sheet with maintaining country order in the first sheet.
These can be done with macro but I want to do this with formula.
Try this:
=IFERROR(INDEX(Sheet1!$A$1:$C$7,SMALL(IF(Sheet1!$D$1:$D$7="Yes",ROW(Sheet1!$D$1:$D$7),1E+99),ROW($A1)),COLUMN(A$1)),"")
Input formula in Cell A2 of Sheet2 and press Ctrl+Shift+Enter.
Then copy to remaining cells. Adjust the addresses in the formula to suit. HTH
Result: