Change range value based on criteria - vba

I need to show a range if cell.value (D2) is less than today and another of cell.value (D2) is greather than today.
The copying and pasting may consume too much from excel, and it is not working anyway, is there another way to do this?
The cell value is always J3:AB3 , donĀ“t matter what date is in D2
This is what I have so far:
Dim C As Range
Set C = ThisWorkbook.Sheets("SIN").Range("D2")
If CDate(C.Value) <= Date Then
ThisWorkbook.Sheets("PARA").Range("J2:AB2").Copy Destination:=ThisWorkbook.Sheets("SIN").Range("D5:V5")
Else
ThisWorkbook.Sheets("PARA").Range("J3:AB3").Copy Destination:=ThisWorkbook.Sheets("SIN").Range("D5:V5")
End If

Related

Excel VBA - Copy Paste Range in column if Date matches

My macro is used to copy/paste live data to overwrite an old budget.
Essentially I want to do the following:
If the date in T6 = date in Range(G6:R6) then copy Range(T10:T30) to Range(?10:?30), where ? is the column of the cell that matched the date.
This should match what you're asking for. In the future, you should share the code that you've attempted so far.
Dim cell As Range
For Each cell In Range("G6:R6")
If cell.value = Range("T6").value Then
Range(Cells(10, cell.Column), Cells(30, cell.Column)).value = Range("T10:T30").value
End If
Next cell

How do I insert multiple date columns in order to meet a specific date?

I am really new to codes and Excel VBA, and hopefully, you guys could help me out with my question. Any tips, feedback, and comments are greatly appreciated!
Within a workbook, I want to make sure that my cell (I1) of worksheet (Sheet1) has the specific date as written in a different sheet (ie. Menu). I want I1 to be the starting point whereby subsequent dates will occur by going across the row (I1, J1, K1, etc.). In this case, if my specific date required is 15/8/16 and one of my sheet (Sheet 1) has its cell I1 written as 20/8/16, I want to know how to construct my code in such a way that,
If I1 in Sheet 1 is currently at 15/8/16, then do nothing. But if I1 in Sheet 1 is off a different later date than 15/8/16, the I1 will now begin at 15/8/16, and subsequent dates are added until it reaches the default date that was initially there at I1 (now 20/8/16 is at cell N1).
My current code is as follows:-
If ActiveSheet.Range("I1") <> MainSht.Range("D6") Then
ActiveSheet.Range("I1") = MainSht.Range("D6")
End If
Do
If Cells(1,z+1)>Cells(1,z+1) Then
Cells(1,z+1) = Cells(1,z)+1
End If
z = z+1
Loop Until Cells(1,z+1) = MainSht.Range("D7")
*Mainsht (D6) is my start date, (D7) is my end date.
My code currently does not have the insert column section because I have problems in applying both insert column and date increment code together. With my current code, my date range never expanded as it is still within the same earlier date range (same last column as before, hence last cell for date column remains as it is). How do I construct in such a way that the missing dates in between are added, and it is added by inserting columns in a repeated process?
Thanks in advance if anyone could help me out in this. Thanks for your understanding as well.
Please check below code to add columns
Dim start_date, end_date As Date
start_date = ThisWorkbook.Sheets("Sheet1").Range("L1").Value
end_date = ThisWorkbook.Sheets("main").Range("D7").Value
If start_date < end_date Then
Do Until start_date = end_date
ThisWorkbook.Sheets("Sheet1").Activate
Range("L:L").Insert (xlRight)
start_date = start_date + 1
Range("L1").Value = start_date
Loop
End If
you could try this:
Option Explicit
Sub main()
Dim diff As Long
With Worksheets("Work").Range("I1") '<--| reference working sheet range "I1" (change "Work" to your actual working worksheet)
diff = .Value - Worksheets("Menu").Range("D6") ' <--| evaluate the difference between referenced range value and worksheet "Menu" cell "D6" (change "Menu" to your actual "main" sheet)
If diff > 0 Then
With .Resize(, diff) '<-- reference referenced range resized to the necessary columns number
.EntireColumn.Insert xlRight '<-- insert columns
With .Offset(, -diff).Resize(1) '<--| reference referenced range first row
.FormulaR1C1 = "=RC[1]-1" ' <--| insert formulas that substracts one from the value of next cell on the right
.Value = .Value '<-- get rid of formulas
.NumberFormat = .Offset(, diff).Resize(, 1).NumberFormat '<--| format cells as the passed range
.EntireColumn.AutoFit '<--| adjust columns width
End With
End With
End If
End With
End Sub
Just change "Work" and "Menu" to your actual worksheets names

Find if date is present in Range of cells (Excel VBA)

I have a cell F2 that contains a Date, and is formatted as Custom Date field displaying only day and month.
Then i have a Range of Cells C3;C60 that contains also Dates and is formatted as European Date field displaying dd/mm/yyyy
I am writing a VBA that makes a check on those fields, but it is not working.
in the sample below the variable c can be any cell between F5 and F20.
I get RuntimeError 91.
If Worksheets(1).Range("C3", "C60").Find(Worksheets(1).Cells(2, c.Column).Value) = True Then
c.Value = "Whatever"
Else
.Find() returns a range, your if statement is checking to see if it's TRUE. That will fail.
Dim rng as Range
Set rng = Worksheets(1).Range("C3:C60").Find(Worksheets(1).Cells(2, c.Column).Value)
if not rng is Nothing then
c.Value = "Whatever"
Else
Note the inclusion of Gary's answer
Substitute:
Range("C3:C60")
for
Range("C3", "C60")
There may be other problems.

Excel search for empty cells, check conditions, write text

I have been doing some basic VBA programming in Excel 2010 but I have been struggling with this challenge for some time. Basically, I have a sheet that is formatted like this (It actually has 62 columns and rows=# of days in the given month):
Column A will be hidden but is used in a few formulas.
Row 15 shows whether or not the station is open 24/7(all) or only Monday-Friday(M-F).
the values presented are arbitrary counts. However, a blank count represents a problem unless... the station is M-F and
I need to get my code to identify a station that is open M-F and then fill in any particular Sat. or Sun (for that station) with the word "closed." then search for the next station that is M-F and repeat the process.
Initially I was having my code start with an actual value and then use several activecell.offset functions to find empty cells and then check conditions but I couldn't get it to work out. Then I tried to check from the station name or the schedule row but I couldn't get the multiple if/nested offset statements to work either.
I would really appreciate any help or insight you could provide that would show me the best approach. I don't really need the code that does it I just need a pseudo code walk-through unless you are kind enough to write out the code.
Thanks for your help!
I had a similar problem I worked out before. I modified it to your spreadsheet:
Dim d As Long, s As Long
d = 1 'weekdays column
s = 40 'status row
Dim r As Long, c As Long
r = ActiveSheet.Cells(Rows.Count, d).End(xlUp).Row
c = ActiveSheet.Cells(s, Columns.Count).End(xlToLeft).Column
Dim i As Long, cell As Range
i = 0
Dim days() As Long
For Each cell In Range(Cells(1, d), Cells(r, d))
If cell.Value = "Sat" Or cell.Value = "Sun" Then
ReDim Preserve days(i)
days(i) = cell.Row
i = i + 1
End If
Next cell
For Each cell In Range(Cells(s, 1), Cells(s, c))
If cell.Value = "M-F" Then
For i = LBound(days) To UBound(days)
Cells(days(i), cell.Column).Value = "closed"
Next i
End If
Next cell

Trouble with getting DateAdd to correctly formulate

So I'm having two issues that I cannot seem to get unkinked. I run reports from a master sheet based off of a template and each finished sheet will have varying numbers of rows. What each finished sheet has in common are two columns(one for a begin date [Column F] and on for an expiration date[Column H]). For each row with a date in Column F I need to add 60 days to the date and put that date in Column H. I have tried working with variations of:
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value + 60
Next cell
I have tried this also with combinations of different while statements that I use for other things where I am putting values in one column based off of another, but I can't get them to work either.
Some of the problems I am having are: first, when I do manage to get it to enter a date in Column H it always enters 2/29/1900. It doesn't matter if there's a date in Column F or not, or what that date is. Second, when I try to set a range for the selection (this is when I try to combine with "While" statements) it pastes a date number in the entire range instead of only the cells with a date in Column F.
How can I get the macro to only add a date in Column H if there is a date in Column F, and how can I get the darn thing to add 60 days correctly?
Sub Tester()
Dim c As Range, val
For Each c In ActiveSheet.Range("E2:E100")
val = c.Value
If Len(val) > 0 And IsDate(val) Then
c.Offset(0, 2).Value = val + 60
End If
Next c
End Sub