I am trying to highlight expiry dates in Excel VBA. I need Column B to Highlight dates that will expire within the next month and Column c to highlight dates that will expire within 4 - 6 months. At present the code works if I delete columns and run the code on one column at a time e.g.
Name Green Will Expire within 1 Month
Name 1 01/01/2013
Name 2 17/07/2013
Name 3 03/04/2013
Name 4 24/03/2013
Name 5 16/07/2013
Name 6 26/01/2013
Name 7 28/06/2013
Name 8 01/07/2013
Name 9 09/01/2013
Name 10 31/07/2013
Name (Column A), Green Will Expire within 1 Month (Column B).
If I run the code with these two columns only (Having deleted Column C) the code works fine. If I include my third column, Orange Expires in 4 - 6 Months (Column C):
Name Green Will Expire within 1 Month Orange Expires in 4 - 6 Months
Name 1 01/01/2013 01/01/2013
Name 2 17/07/2013 01/12/2013
Name 3 03/04/2013 03/04/2013
Name 4 24/03/2013 20/11/2013
Name 5 16/07/2013 16/07/2013
Name 6 26/01/2013 26/01/2013
Name 7 28/06/2013 28/06/2013
Name 8 01/07/2013 01/07/2013
Name 9 09/01/2013 09/01/2013
Name 10 31/07/2013 31/07/2013
Only the second for loop works. I need both of these for loops to run without having to delete any columns.
VBA Code:
Private Sub CommandButton1_Click()
Dim Cell As Range
For Each Cell In Range("B2:B1000").Cells
If Cell.Value <= Date + 30 And Not (Cell.Value < Date) And IsEmpty(Cell.Offset(0, 1).Value) Then
Cell.Interior.ColorIndex = 35
End If
Next Cell
For Each Cell In Range("C2:C1000").Cells
If Cell.Value <= Date + 180 And Not (Cell.Value <= Date + 120) And IsEmpty(Cell.Offset(0, 1).Value) Then
Cell.Interior.ColorIndex = 45
End If
Next Cell
End Sub
Any Help is greatly appreciated.
Thank You
The part of your conditional statement will always evaluate to False if Column C is not empty:
And IsEmpty(Cell.Offset(0, 1).Value)
On a related note, the same fragment is in the second loop, so you may want to remove it from there, too. But I am not sure if that part would be required logic. In any case, the same holds: If column D is not empty, this will always evaluate to false, so the body of the If...Then will be omitted.
Related
I am trying to calculate three month average sales in excel w.r.t customerid in excel. I tried by doing it by AverageIfs function but nothing helped.
A B C
Orderdate sales customerid
5/15/2019 7 1
5/15/2019 48.5 1
4/15/2019 92.94 1
3/17/2019 102.85 1
3/18/2019 49 1
3/18/2019 119.95 1
2/18/2019 58.96 1
1/20/2019 14.6 1
5/16/2019 17 6
4/15/2019 148.5 6
4/12/2019 912.94 6
3/17/2019 102.85 6
9/18/2018 22.34 6
Formula I tried: =AVERAGEIFS(B:B,C:C,C2)
output expected:
customerid average(3 months)
1 49.48
6 359.48
Let's start from today's date and the date 3 months ago (Make it dynamic):
Remember to change the cell format from General to Date. Otherwise, it will show [43563]
Next use the date as part of our filter:
Now you should get the most recent 3 months data:
Copy the filtered data into a new spreadsheet
Copy the filtered data into a new spreadsheet
Copy the filtered data into a new spreadsheet
Next Step: get the distinct customer ID:
You will get this:
Last Step:
Use the function "AVERAGEIF":
Done!
I have an excel like
A B START DATE END DATE
1 10 01-jan-2016 02-jan-2016
2 11 01- jan-2051 02-feb-2061
3 1 04-mar-2016 07-mar-2016
4 1 08-mar-2016 10-mar-2016
5 5 01-mar-2016 03-dec-2016
6 5 03-nov-2016 31-dec-4712
I am new to excel. I want to highlight or extract the columns in A column which can be found in B Column along with the start date and end date .
That is result should be like :
A start_date end_date
1 04-mar-2016 07-mar-2016
1 08-mar-2016 10-mar-2016
5 01-mar-2016 03-dec-2016
5 03-nov-2016 31-dec-4712
Can anyone pls suggest something ?
In E2 enter:
=IF(COUNTIF(A:A,B2)>0,"X","")
and copy down. Then filter the table
You can hide any un-wanted columns after that.
I have to create output XLS file based on input xls file header. I am giving below exact requirement. hope its clear. if not then please let me know.
Iput XLS -->
ID Version NameLegacy ProjectNumber OwnerName Language Keywords OwnerSite External Content Relevance Periodic Coremap ValidTo
1 1 Mohan 1000 x ENG ABCD AB No ok no 5 2017-10-14 2018-10-14
2 2 Shayam 1001 y ENG EFGH BC No ok yes 2 2017-10-14 2018-10-14
3 1 Sourabh 1002 z ENG IJKL CD Yes no no 4 2017-10-14 2018-10-14
Based on this Input XLS File, i need Output XLS File as below. Each row of input file will have 12 row of output file and respective value will be filled in Output file from input file.
ID Version IBANAME STRINGVALUE INTEGERVALUE FLOATVALUE FLOATVALUEWITHUNITS BOOLVALUE TIMEVALUE URLVALUE REFERENCEVALUE
1 1 NameLegacy Mohan
1 1 ProjectNumber 1000
1 1 OwnerName x
1 1 Language ENG
1 1 Keywords ABCD
1 1 OwnerSite AB
1 1 External No
1 1 Content ok
1 1 Relevance no
1 1 Periodic 5
1 1 Coremap 2017-10-14
1 1 ValidTo 2018-10-14
2 2 NameLegacy Shayam
2 2 ProjectNumber 1001
2 2 OwnerName y
2 2 Language ENG
2 2 Keywords EFGH
2 2 OwnerSite BC
2 2 External No
2 2 Content ok
2 2 Relevance yes
2 2 Periodic 2
2 2 Coremap 2017-10-14
2 2 ValidTo 2018-10-14
3 1 NameLegacy Sourabh
3 1 ProjectNumber 1002
3 1 OwnerName z
3 1 Language ENG
3 1 Keywords IJKL
3 1 OwnerSite CD
3 1 External Yes
3 1 Content no
3 1 Relevance no
3 1 Periodic 4
3 1 Coremap 2017-10-14
3 1 ValidTo 2018-10-14
Thanks in advance.
There are a few steps to do this using VBA. The main purpose is to identify what you are actually trying to do.
It seems like you are attempting to sort through records to identify what the format of the information is and place it in a column designated for only that format. To do this, I have created a very simple custom Function..
This function takes in the name of the header as a string, and uses Select/Case to find appropriate matches for which column the target sheet needs. This is custom to your specific layout. Feel free to adapt or change it.
FUNCTION TO FIND CORRECT COLUMN ON TARGET
Function ColFinder(header As String) As Long
'Target Column Format = Column #
'"STRINGVALUE" = 4
'"INTEGERVALUE" = 5
'"FLOATVALUE" = 6
'"FLOATVALUEWITHUNITS"= 7
'"BOOLVALUE" = 8
'"TIMEVALUE" = 9
'"URLVALUE" = 10
'"REFERENCEVALUE" = 11
' **** SET VALUES FOR CASES TO MATCH DESIRED FORMAT ABOVE ****
'Default to Column 4 StringValue if unknown
Select Case header
Case "NameLegacy"
ColFinder = 4
Case "ProjectNumber"
ColFinder = 5
Case "OwnerName"
ColFinder = 4
Case "Language"
ColFinder = 4
Case "Keywords"
ColFinder = 4
Case "OwnerSite"
ColFinder = 10
Case "External"
ColFinder = 8
Case "Content"
ColFinder = 11
Case "Relevance"
ColFinder = 8
Case "Periodic"
ColFinder = 5
Case "Coremap"
ColFinder = 9
Case "ValidTo"
ColFinder = 9
End Select
End Function
Using a few other Functions for finding Last Row & Last Column
Last Row Function
Function lastRow(sheet As String) As Long
lastRow = Sheets(sheet).Cells(Rows.Count, "A").End(xlUp).Row 'Using Cells()
End Function
Last Column Function
Function lastCol(sheet As String) As Long
lastCol = Sheets(sheet).Cells(1, Columns.Count).End(xlToLeft).Column
End Function
With these three functions, you can run a simple loop through the source sheet, one row at a time, then look to see which column the data is coming from, and place it on the appropriate target.
This example is using two worksheets in the same workbook. To change, just extend the address to include other workbooks or sheets. This is just the simplest way to show the concept.
CODE TO LOOP THROUGH SHEETS
Sub ColsToRows()
Dim tRow As Long, sCol As Long, sRow As Long
Dim source As String, target As String
Dim tString As String
source = "Sheet1"
target = "Sheet2"
'Set target Row starting point
tRow = 2
'Loop through Source Rows with Data, starting after Header Row
For sRow = 2 To lastRow(source)
'Loop through each header in Source Row
For sCol = 3 To lastCol(source)
'Set Basic Columns on Target
Sheets(target).Cells(tRow, 1) = Sheets(source).Cells(sRow, 1)
Sheets(target).Cells(tRow, 2) = Sheets(source).Cells(sRow, 2)
'Get Header Name as Temp String to find which format it requires
tString = Sheets(source).Cells(1, sCol)
'Label Column 3 of Target
Sheets(target).Cells(tRow, 3) = Sheets(source).Cells(1, sCol)
'Find Appropriate Column using custom Formula [ColFinder(header As String) As Long]
Sheets(target).Cells(tRow, ColFinder(tString)) = Sheets(source).Cells(sRow, sCol)
'Advance to next Target Row
tRow = tRow + 1
Next sCol
Next sRow
End Sub
Source Sheet Example
Target Sheet Example
You can convert the upper table to the lower one using formulas within Excel.
We start from defining all the input range, without the first 2 columns and the headers row as a range named input. This is done by selecting all the range and typing "input" in the "Name box":
Create the following table where you like in the workbook, then define it all and a range named col_index:
STRINGVALUE INTEGERVALUE FLOATVALUE FLOATVALUEWITHUNITS BOOLVALUE TIMEVALUE URLVALUE REFERENCEVALUE
NameLegacy ProjectNumber External Coremap OwnerSite Content
OwnerName Periodic Relevance ValidTo
Language
Keywords
This table basically sorts the original columns into their new place. You can add values, rows or columns to the table and it will stay intact, as long as you keep all of it under the name range col_index.
Then create another table headers:
ID Version IBANAME STRINGVALUE INTEGERVALUE FLOATVALUE FLOATVALUEWITHUNITS BOOLVALUE TIMEVALUE URLVALUE REFERENCEVALUE
Now paste the following formulas in the first row in each of column 1-4:
ID: =IF(C2=sheet1!$C$1,A1+1,A1)
Version: =VLOOKUP(A2,sheet1!$A$1:$B$4,2)
after selecting the first 12 rows under IBANAME, press F2, and paste: =TRANSPOSE(sheet1!C1:N1) as array formula! (press ctrl+shift+enter after pasting to the formula box)
STRINGVALUE: =IF(ISERROR(MATCH($C16,OFFSET(col_index,0,MATCH(D$15,OFFSET(col_index,0,0,1,11),0)-1,5,1),0)-1),"",OFFSET(input,TRUNC((ROW()-ROW($D$16))/COLUMNS(input)),MOD(ROW()-ROW($D$16),COLUMNS(input)),1,1))
Next, drag columns ID and Version 12 rows down, to the next ID (row 14), then change the value in cell A2 to 1, and the value in C14 to =C2, and continue dragging columns ID, Version and IBANAME until the end of the data is reached. Finally, drag the cell D2 left to the end of the row, and then down to fill all the table.
This it how it looks with formulas:
And this is with values:
That's it ;)
I have a row consisting of 31 columns (E27:AI27). All the cells within this range get their input (a number from 1-31) from other cells outside of this range, Let's say from a couple of drop down lists. Depending on how I twerk these drop-down lists the sequence of the numbers in that specific range (E27:AI27) can look like the following alternatives:
Alt. 01: 1 2 3 4 5................................29 30 31 (31 cells total)
Alt. 02: 2 3 4 5 6...........................29 30 31 na (31 cells total)
Alt. 03: 3 4 5 6 7...........................30 31 na na (31 cells total)
Alt. 04: 4 5 6 7 8......................30 31 na na na (31 cells total)
etc etc etc.
Alt. 30: 30 31 na......................na na na na na (31 cells in total)
Alt. 31: 31 na na..................... na na na na na (31 cells in total)
Now what I want to do is to automatically, temporarily, hide those columns that contain string "na" within them. When I then re-twerk my drop down lists again, i want to be able to unhide those columns that change back from "na" to a number. For example, i want to be able to shift between alternative 1 and alternative 2 (see above), depeding on how i twerk my drop down lists. Shifting between these two alternatives would mean that AI27 would go from showing, to hiding, to showing, to hiding and so on (while E27AH27 would all be showing since they would all have numbers inside then, 1-30.
Last but not least, there are two drop down lists that control the values of the cells E27:AI27. Drop down list called month (C18) and drop down list called day (D18). The former (C18) sets the days for a month. If C18=February then there will be 28 days and the last two columns (AH:AI) would be "na". In addition to that the latter drop down list (D18) sets the start day, namly the first number (in cell E27) in the sequence. If D18=21, then the alternative for february month would be:
21 22 23 24 25 26 27 28 na na na na na na na na na na na na.... (containing 23 na)
Can anyone please help me set up VBA code for this?
Try this one :
Private Sub Worksheet_Change(ByVal Target As Range)
'change the "E23" with the cell with your dropdown
If Not Intersect(Target, Range("E23")) Is Nothing Then
Call UnhideHideColumns
End If
End Sub
Sub UnhideHideColumns()
Dim bytColumnCheck As Byte
Dim blnNeedToUnhide As Boolean
Dim intFirstNAColumn As Integer
'check all columns and find out if they are already hidden
For bytColumnCheck = 5 To 35
'if any of the columns is hidden, unhide all the columns to the right and exit loop
If Cells(1, bytColumnCheck).EntireColumn.Hidden = True Then
Range(Cells(1, bytColumnCheck), Cells(1, 35)).EntireColumn.Hidden = False
Exit For
End If
Next bytColumnCheck
'find first occurence of "na" in values, if any exists
If Not Cells.Find("na", LookIn:=xlValues, after:=Range("E27")) Is Nothing Then
intFirstNAColumn = Cells.Find("na", LookIn:=xlValues).Column
'now hide the columns to the right of the first "NA", including
Range(Cells(1, intFirstNAColumn), Cells(1, 35)).EntireColumn.Hidden = True
End If
End Sub
Note that the Call UnhideHideColumns will be called every time the cell with the dropdown changes, even let's say from Set1 to Set1, or from 5 to 5. So it can trigger the macro unnecessary often.
This worked for me:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 9 To 40: Cells(28, i).EntireColumn.Hidden = Cells(28, i) = "na": Next
End Sub
I am stuck, and would like a little hlep. I'm trying to get a qty on Column D12, d14, d16 .... to d42. Than multiply this qty by a value. the value sheet look like this.
Value Sheet
A B C
ItemName Quality Confort
1 Chair 2 1
2 Bed 0 3
3 Table 1 1
Quantity Sheet
A B C D
ItemName QTYColumn
12 .. Table 2
13
14 .. Chair 5
15
16 .. Bed 6
Total Sheet
A B
Quality 12 (2*1 + 5*2 + 6*0 )
Confort 25 (2*1 + 5*1 + 6*3 )
I'm pretty sur I have the hardest part done. I can check and grab the quantity from all the sheets I want. I also got a function done where you pass the name of the item, and the stats name, and it return me the results I want.
so, I got this part of the code atm which doesnt work, and its driving me nuts.
For Counter = 12 To 42 Step 2
For Each qColumn In QTYColumn
Set QTY = Range(qColumn & Counter)
Dim ItemName As Range
ItemName= QTY.Offset(-2, 0).Select
total = total + (QTY * GetValue(ItemName, "Confort"))
Next qColumn
Next Counter
My problem is with the ItemName variable. Its always empty and as soon as I get to it with the debugger, the function stops and it closes. Anyone have any idea as to why ? it's important for me to get it base on the offset -2 and not the column adress because it might be different depending of the sheet, and the only "sure" way to find it is the get the 2nd cell to the left of the quantity cell.
ItemName= QTY.Offset(-2, 0).Select does not mean anything !
Either you Select:
QTY.Offset(-2, 0).Select
or you get the value:
ItemName= QTY.Offset(-2, 0).Value '(value can be omitted here)
But then, Dim ItemName As Range does not make sense. It should be a String or a a number.
or you get the range:
Set ItemName= QTY.Offset(-2, 0) ' then you need Set