Excel column range highlighting, and insertion of calculation data - excel

I have the following problem:
I need to highlight period of production order start and finish week, through all columns - as you see in header, there are a lot of production orders.
Is it possible with vba?
Second problem:
Each production line has time capacity, so production minutes should be divided by chosen production line weekly capacity by the end of highlighted period.
This is also need to be done on button click e.g. with vba, or formulas, but I have no idea where to place those formulas, because whole table and header supposed to be dynamic - production plan updates weekly.
UPDATE:
Here's code, that inserts formula in order manufacturing period from start to finish week, i need to loop it through all columns and insert this formula according to following orders start and finish week. And I have no idea how to do that, so help is very appreciated :)
Sub test()
Dim Rng, Rng2, Rng3 As Range
Dim v1, v2 As String
x = Range("E8")
w = Range("E11")
'Search Column or range
With Sheets("forecast").Range("D:D")
Set Rng = .Find(What:=x, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
Set Rng2 = .Find(What:=w, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
'MsgBox Rng.Offset(0, 1).Address
'MsgBox Rng2.Offset(0, 1).Address
v1 = Rng.Offset(0, 1).Address
v2 = Rng2.Offset(0, 1).Address
Range(v1 & ":" & v2).Select
Selection.formula = "=E$12/(E$10-E$7)"
Selection.NumberFormat = "0"
End With
End Sub
Example of production plan:
http://postimg.org/image/3vkx0fv5d/ http://s8.postimg.org/cdud4s1o4/stack3.jpg

As for highlighting you need to put in a "conditional format". (It's on the home ribbon.)
Select the cells from column D11 till the end of the column range needing the formula. Click conditional format and enter the formula:
=AND($B11>D$6;$B11<D$7)
Make sure the dollar marks are in the right spots. And set a format to apply, for example fill to be red. Apply the formula, you just made a conditional format for column D. Now select cell D11 and click "format painter" button and select the whole range to which you want to apply the formula. Done.
update,Adding year:
I only see one year in the sheet. i.e. at the top. So if start/finish week could be of some other year as well that should be added as extra lines start year and finish year. Then create extra lines and columns for the excel date_formats created with help of the formula:
creating excel_date_format
Then you can use the same CF as before now using the newly added column and lines as in:

Related

Conditional Hyperlink if Value from Sheet1 Matches Value in Sheet2

I would like to create a conditional hyperlink in Column A (Link) of Sheet 1 to a matched cell in Sheet 2 when a value in Column B (Number) of Sheet 1 matches that of Column A in Sheet 2 (Number).
In the first instance, I don't even seem to be able to create a hyperlink between 2 sheets...
I also tried using the MATCH functions to at least confirm if I could accurately reference matching cell in a different worksheet and it seems to work; but just can't seem to (1) get hyperlink between sheets to work and (2) create hyperlinks conditionally to when there are matching values.
Unsure if I may need to use INDEX as well or Macros; just a bit lost on how to achiever my goal overall and would appreciate any tips from the community <3
In formula way, you can try function =CELL("address",INDEX(MATCH))) to return matched cell's address, then create link to it.
One more note, if you want to create link within your workbook, use '#" before cell's address.
=HYPERLINK("#"&CELL("address",INDEX(Sheet2!$A$2:$A$6,MATCH(B2,Sheet2!$A$2:$A$6,0))),B2)
Sheet1:
or Macro way:
Sub FindnLink()
Dim a As String
Set source_range = ThisWorkbook.Sheets("Sheet1").Range("B2:" & Range("B2").End(xlDown).Address)
Set search_range = ThisWorkbook.Sheets("Sheet2").Range("A2:" & Range("A2").End(xlDown).Address)
For Each cell In source_range
cell.Offset(0, -1).Select
a = cell.Value
Excel.ThisWorkbook.Sheets("Sheet1").Hyperlinks.Add Anchor:=Selection, Address:="", _
SubAddress:="Sheet2!" & search_range.Find(What:=cell.Value, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Address, _
TextToDisplay:=a
Next cell
End Sub

Searching for Dates in Column in VBA

I am trying to search for a date in a column in VBA. I have verified that the value exists within the column and I have also formatted the date to be that of the dates in the reference column. Have been scratching my head and i'm not sure what i'm doing wrong. I've seen several questions related to this, and I have even mirrored one of the solutions, which has not worked.
Notes:
1) All dates are stored in column 1 (A) 2) Date format is "m/d/yyyy" 3) Sheet 13 is where the range lives 4) I also have a named range that contains the date i'm searching for and I access that range by referencing the cell rather than the named range since VBA doesn't seem to like that. Any thoughts?
searchString = Format(Sheet3.Cells(5, 4).Value, "m/d/yyyy")
With Sheet13.Range("A:A")
Set searchCell = .Find(What:=CDate(searchString), After:=ActiveCell, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
End With
You need to match the date (stored as number value in Excel):
Dim DateVal As Date
DateVal = Sheet3.Cells(5, 4).Value
With Sheet13.Range("A:A")
Set searchCell = .Find(What:=DateVal, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
End With
Another example of how using references like ActiveAnything can lead to errors.
In your code, if ActiveCell doesn't happen to be in sheet13 column A, your code will error.
Change to After:=.Cells(1,1) or something similar so you ensure you are starting with some cell within in the column you are using as the SearchRange.

VBA Copy a table of user input data to a database tab, based on the employees name

I have a tab in excel where managers can input their forecast of Holidays, Sickness, Training and other lost time for the next 4 months.
To make this smoother I've made it so they select their own name and their team shows in the table and they can input the lost days for the team.
What I'm having issues with is the code that then takes this information and copies it to another tab, and puts it in against the right employee. This data tab has every employee so it needs to search down column B for the matching name and copy the data into the corresponding columns. I've tried a few different ways but none quite do what i'm looking for
First image is the screen the manager sees to enter their forecast of lost days
second image is the screen that the data needs to be copied into the archive it for it to be used elsewhere
Hopefully this coveres my query, any help would be wonderful
Closest code I could get working below, only copies 1 value and for only 1 employee however
Sub Find_First()
Dim FindString As String
Dim Rng As Range
FindString = Sheets("Calc").Range("G7").Value
If Trim(FindString) <> "" Then
With Sheets("DATA Build").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Sheets("Calc").Range("I31").Copy Sheets("DATA Build").Cells(Rng.Row, "BD")
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub

Find Code from Another Sheet and Return Info From Same Row

I have searched to find the answers to get to where I am but am now stuck! I am a relative beginner with VBA.
I have a Workbook that lists a few hundred orders that we are producing for our customer.
The order details are on the first sheet called "In Progress" and on the 3rd sheet called "StyleData" are more details about each product such as its composition, design reference, SKU etc...
At present my code searches column A on the Data sheet based on the 6 digit style code in the active cell on the In Progress Sheet, then goes to that cell. I have put a MsgBox in purely to put a pause in the code so I know where it has got to.
What I want it to do after finding the style code on the data sheet is return a value on the same row from column H, preferable in a format that the use can select and copy, then it will return to the original cell at the start of the macro.
Code as follows:
Sub get_composition()
Dim item_no As String
Dim data_sheet As Worksheet
Dim found_item As Range
Set Rng = ActiveCell
item_no = ActiveCell.Value
Set data_sheet = Sheets("StyleData")
If Trim(item_no) <> "" Then
With Sheets("StyleData").Range("A:A")
Set found_item = .Find(What:=item_no, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not found_item Is Nothing Then
Application.Goto found_item, True
Else
MsgBox "Nothing found"
End If
End With
End If
MsgBox "Return to Original Cell"
Application.Goto Rng
End Sub
if I understand what you want :
you arrive at cell "found_item" and want to return a value from the same row.
If it's so, you can use method Offset on "found_item"
found_item.Offset() allow you to navigate from the current range
https://msdn.microsoft.com/en-us/library/office/ff840060.aspx
If you are on column A, found_item.Offset(, 1) will return the range on the same line but column B

Excel VBA - Using Find method on a range of dates

I am trying to find if a certain date is in a range of dates.
This is the range of dates:
01/01/2013
11/02/2013
29/03/2013
20/05/2013
01/07/2013
05/08/2013
02/09/2013
14/10/2013
11/11/2013
25/12/2013
26/12/2013
Here is the VBA code:
' Format Holiday Rows '
With ConfigData.Range("B8:B18")
Set holidays = .Find(s1.Cells(row_count, 1))
If Not holidays Is Nothing Then
MsgBox s1.Cells(row_count, 1)
End If
End With
In the above code, the first MsgBox that pops up reads "11/01/2013". This makes absolutely no sense, as that value is not in the range.
Note: ConfigData.Range("B8:B18") refers to the range of dates shown above.
ALSO: This code is within a for loop that increments the value of s1.Cells(row_count, 1). Starting at 01/01/2013 until 31/12/2013
If you just want to confirm a calendar day in your series is within the holiday list, then you could even use vlookup:
Dim strFound As String
On Error Resume Next
strFound = Application.Vlookup(s1.Cells(row_count, 1), .Range("B8:B18"), 1, 0)
If IsError(strFound) Then
MsgBox "Not Found"
Else
'-- Found
End If
On Error GoTo 0
The following code works for me:
Sub thing()
Dim cell As Range, _
holidays As Range
For Each cell In Range("D1:D365")
With Range("A1:A11")
Set holidays = .Find(cell.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not holidays Is Nothing Then
Debug.Print cell.Value
End If
End With
Next cell
End Sub
If this doesn't work, I'd suggest it's likely you have a cell formatting issue. Select one of your date cells. Go to the immediate window (Alt+F11, then Ctrl+G from Excel), type ? Selection.Value2 and press enter. Does that return a numeric value (~41000)?
Alternatively, you could reenter the dates in a completely new sheet (enter the first couple manually and drag down, do not copy and paste as formatting will be copied also) and try again. This should at least remove odd formatting as a potential issue.
It is important to note that excel uses american date formatting. ie mm/dd/yyyy and it can therefore be a little tricky to get the .Find() function to work properly. Make sure your variables are formated properly in order for excel to hopefully give you what you're looking for:
Dim strdate As String
Dim aCell As Range
strdate = ActiveSheet.Cells(1,1)
strdate = Format(strdate, "Short Date")
On Error Resume Next
Set aCell = Cells.Find(What:=CDate(strdate), After:=Range("A1"), LookIn:=xlFormulas , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If rCell Is Nothing Then
MsgBox("Date cannot be found. Try Again")
End If
End Sub
Of course there are a lot of annoying things that can happen with the date formatting, but this is assuming the dates you're looking for ar in the "Short Date" format.
'To find a cell elsewhere in a worksheet with the same specific date as a reference cell:
'First copy all dates to cells immediately to their left.
'Format the copied cells as "General"
'Run this code - then use the dateRow and DateCol variables (eg in vlookup)
'Works in Excel 2013 (The "General" column must not be hidden - Hide by formatting in background colour)
Dim dateVal
Dim DateRow
Dim DateCol
dateVal = Range("j8").Value 'must be in general format
Cells.Find(What:=dateVal, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
DateRow = ActiveCell.Row
DateCol = ActiveCell.Column
MsgBox (DateRow & " " & DateCol)
End Sub

Resources