I'm trying to use the WORKDAY function in a VBA macro that will auto-fill down a column. I've looked around and tried a few variations, with mixed results, none of which do exactly what I need.
I need the code to determine if a cell (column I) is empty, and if so determine the previous workday based on a date in cell (column H). The code I have so far is:
Dim rng As range
Set rng = range("I3", Cells(Rows.Count, "I").End(xlUp))
For Each cell In rng
'
If cell.Value = "" Then
cell.Value = "=WORKDAY(""H3"",-1)"
End If
Next
End Sub
Thank you for your help!
For future reference, the macro recorder is really good at giving you reference code for this sort of thing. You probably want something like:
Dim rng As range
Set rng = range("I3", Cells(Rows.Count, "I").End(xlUp))
rng.cells(1,1).Formula = "=IF(H3="""","""",WORKDAY(H3,-1))"
rng.cells(1,1).AutoFill Destination:=Range("J2:J6"), Type:=xlFillDefault
This will do the same thing as if you'd dragged the formula down (in regular Excel) to all cells in rng
you van change cell.Value = "=WORKDAY(""H3"",-1)" to cell.Formula= "=WORKDAY(""H3"",-1)"
or use: cell.Value = WorksheetFunction.WorkDay(Range("H3").Value, -1)
If you just need the value but not the formula in the cell, you could instead use:
cell.Value = Application.WorksheetFunction.Workday(Range("H3"),-1)
...and loop through the cells into which you need to place the value.
If you want the formula:
cell.Formula = "=WORKDAY(""H3"",-1)"
...and again, loop through the cells where you need it.
These are assuming your function works properly on the worksheet as-is.
Related
I have multiple sheets that should have references to another sheet.
For example cell B3,sheet AC: =Inlife!G4. Now I create two new sheets AC (2) and Inlife (2), the reference for B3,sheet AC (2) should be:
=Inlife (2)!G4
I have tried many variations of the replace option but so far all that I managed to do was remove the formula and leave a value or blank cell.
Dim rng As Range, cell As Range
Set rng = Sheets("AC (2)").Range("B3:B10")
For Each cell In rng
cell = WorksheetFunction.Substitute(cell, "Inlife", "Inlife (2)")
Next
Does anyone know a way to update all the references/formulas in one go?
(I have tried to just use the search and replace function of excel but that gave me an error about the formula)
Please, try:
For Each cell In rng
cell.Formula = Replace(cell.Formula, "Inlife", "'Inlife (2)'")
Next
I would also like to suggest replacing of cell variable with cel. Cell is a range property and it is good to avoid creating such variables. It may create confusions on a complex code...
In the end this worked well.
Dim rng As Range, cel As Range
Set rng = Sheets("AC (2)").Range("B3:B10")
For Each cel In rng
cel.Formula = Replace(cel.Formula, "Inlife", "'Inlife (2)'")
Next
I am trying to compare values from a vlookup to a static value in the column to the left, and then paste the value if it matches.
Column J contain static values
Column K contains a vlookup formula referencing another sheet
I need to loop through column K, and if the value from the vlookup matches the value in the same row in column J, then paste the value into column K.
So if cell J2 = 240.89, and the value of the vlookup in cell K2 = 240.89, then paste the value 240.89 into K2.
So far, the code I have loops through the range but stops at the last pasted value and ignores the vlookup formulas.
Here is the code I have:
Option Explicit
Sub CheckValue()
Dim myRange As Range
Dim cell As Range
Set myRange = Range("K2:K3194")
For Each cell In myRange
If cell.Value = cell.Offset(0, -1) Then
cell.Copy
cell.PasteSpecial xlPasteValues
End If
Next cell
End Sub
Any help is appreciated!
Just for fun another option:
Sub Test()
Dim ws as Worksheet: Set ws = Thisworkbook.Worksheets("Sheet1")
ws.Range("K2:K3194").Value = ws.Evaluate("IF(K2:K3194=J2:J3194,J2:J3194,FORMULATEXT(K2:K3194))")
End Sub
Note: This requires FORMULATEXT, a function available since Excel2013.
Try
For Each cell In myRange
If cell.Value2 = cell.Offset(0, -1).Value2 Then
cell.value = cell.value
End If
Next cell
or maybe use a WorksheetFunction.Round to match the values to a specific precision. I feel like this is more the issue than anything.
I want to have create some code in VBA that will look through the cells in a worksheet in column B. If the code finds a cell value with a decimal (opposed to a whole number e.g. not 1, but 1.1) then print the cell value to right off it in another sheet.
I know it will be a loop and it will use offset hit etc. I havent tried this code in VBA i have just typed it as an example of what i will do in the question.
For each cell in Sheets("Sub Tasks").Range("B1:B" & LastRow)
If cell = '(DECIMAL FORUMLA) Then
You may use something like this:
Dim cell As Range
For Each cell In Sheets("Sub Tasks").Range("B1:B" & LastRow)
If IsNumeric(cell.Value) Then
If Val(cell.Value) <> Int(cell.Value) Then
' The number contains decimals.
Dim valueToTheRight As Variant
valueToTheRight = cell.Offset(0, 1).Value
' TODO: Add `valueToTheRight` into the appropriate place of the other sheet.
End If
Else
' The cell value is not a number.
' TODO: either do something about it or remove the `else` branch to ignore it.
End If
Next
You could evaluate the cells content?
Sub Demo()
Dim cell
For each cell in Sheets("Sub Tasks").Range("B1:B" & LastRow)
If IsNumeric(c.Value2) And InStr(c.Value2, Application.DecimalSeparator) Then
Debug.Print c.Value2
End If
Next c
End Sub
IsNumeric will test if the cell holds a number. i.e. skipping any strings with full stops in. and Instr(c.value2, Application.DecimalSeparator) will test for the decimal
I'm brand new to VBA.
I want to copy specific cells from one sheet to another based on a criteria.
For instance if in the range of H2:H1000 value = "Yes" then copy cells from column A,B,C and D from same row where the criteria meets to another sheet.
My code is just a scratch from what I managed to do, I don't know how to select the cells from the same row where the criteria meets.
Sub FindandCopy()
Dim rngA As Range
Dim cell As Range
Set rngA = Sheets("OFCE").Range("H2:H1000")
For Each cell In rngA
If cell.Value = "Yes" Then
cell.EntireRow.Copy
Sheets("Dashboard").Range("I2").End(xlDown).Select
ActiveSheet.Paste
End If
Next cell
End Sub
You might want to try using v-lookup or a simple sumif function. You can do this outside of vba, in the sheet itself, or you can program it into your existing macro.
Plenty of material online on this so you should be able to solve it.
I used offset (to get from H to A) and resize (to expand A to A-D) and then copy to the first unused row of I on the other sheet.
You don't need to Select/Activate, in fact it's discouraged.
Sub FindandCopy()
Dim rngA As Range
Dim cell As Range
Set rngA = Sheets("OFCE").Range("H2:H1000")
For Each cell In rngA
If cell.Value = "Yes" Then
cell.Offset(, -7).Resize(, 4).Value.Copy _
Sheets("Dashboard").Range("I" & Rows.Count).End(xlUp)(2)
End If
Next cell
End Sub
However, rather than a loop, you should consider using AutoFilter or Find to do this as they are more efficient.
Currently I am using the following code to add a formula to cells in the column for a predefined range of cells. The problem is that the number of cells I need the formula in fluctuates based on how big the data set is.
Range("R9").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]<0,""False"",""True"")"
Selection.AutoFill Destination:=Range("R9:R12000")
Range("R9:R62053").Select
What I want to do is for every cell that has a value in say column B, I want the macro to insert the formula in the corresponding cell in column C, and stop once it reaches a point where the cell in column b has no value.
The code below is based off the OP's comments. Where as, his code seems to be targeting R9:R12000"
Dim cell As Range, Target As Range
With Worksheets("Sheet1")
Set Target = .Range("B9", .Range("B" & .Rows.Count).End(xlUp))
For Each cell In Target
If cell.Value <> "" Then cell.Offset(0, -1).Formula = "=IF(RC[-1]<0,""False"",""True"")"
Next
End With