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
Related
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.
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.
Can anybody help me out in making all values of my selected range values Absolute (or applying absolute formula on each cell in a range) by single button using Excel VBA?
A B C
5.119999886 -13.06999969 -13.14000034
-5.76999998 -12.52000046 -12.78999996
-5.88000011 -13.69999981 -14.13000011
5.46999979 -12.61999989 -12.48999977
Consider:
Sub dural()
Dim rng As Range
Set rng = Range("A1:F10")
rng.Replace what:="-", lookat:=xlPart, replacement:=""
End Sub
Not exactly VBA, but if required can be recorded.
Select your Data Range, press Ctrl H (Replace), in Find what add " - " sign and replace all. This will remove the negative sign from all numbers and it will become absolute.
Note: As mentioned in comment that OP agrees that this works for him, hence posting as an answer.
You can do something like this:
Sub MakeAbsolute()
Dim c As Range
Dim rngToAbs As Range
'Set the worksheet name and range appropriately
Set rngToAbs = Worksheets("Sheet1").Range("A1:C2")
For Each c In rngToAbs
c.Value = Abs(c.Value)
Next c
End Sub
You need to change Sheet1 to your worksheet name and A1:C2 to whatever range of cells you want to take the absolute value of.
Hopefully the code is simple enough to understand, but the basic idea is that we will declare a specific range, loop through each cell in that range, and change the value of that cell to be the absolute value by applying the Abs function.
I'd like to create a macro that selects a rectangular range of cells and sets the name of every one of those cells to the value/contents of the cell.
In terms of what I've thought so far, I get an error though with the cell.Name line.
Public Sub NameCell()
Dim rng As Range
Dim cell As Range
Set rng = Range("A1:D1")
For Each cell In rng
cell.Name = CStr(cell.Value)
Next
End Sub
Is this what you meant?
Sub setVal()
Range("A1:C6").Select
Selection = "value"
End Sub
I believe this may work for you unless I also misunderstood the question.
Dim r As Range
Dim cell As Range
Set r = Sheet1.UsedRange
For Each cell In r
Sheet1.Names.Add Name:=cell.Value, RefersTo:=cell
Next
Keep in mind, though, that you would want to check that the cell.Value is valid (no spaces, etc.) for a named range.
To replace a range of cells with their values (removing any formulas from the range), you would use something like this.
Public Sub NameCell()
Dim rng As Range
Set rng = Range("A1:D1")
rng.Value = rng.Value
End Sub
I am being asked to pull some old information out of an Excel file and put it into a new Access Database. The first thing I need to do is gather information from the columns that are in a formula for each department. So I have a formula that looks like this =YP199+YT199+ZL199+ZT199 and I need to take column YP199 get the info I need and so on. Once I can get the column from the formula the rest shouldn't be to hard.
This code will give the the cell addresses of all the cell addresses which are direct precedents of the formula. Please note that I have not done any error handling. I am sure that you can take care of it?
I am assuming that Cell A1 of Sheet1 has the formula =YP199+YT199+ZL199+ZT199
Sub Sample()
Dim ws As Worksheet
Dim rng As Range, acell As Range
Set ws = Sheets("Sheet1")
With ws
Set rng = .Range("A1")
For Each acell In rng.DirectPrecedents
Debug.Print acell.Address
Next
End With
End Sub
The above code when run will give you this.
$YP$199
$YT$199
$ZL$199
$ZT$199