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
Related
I would like to be able to Check for criteria "owners" In column A and SUM cell from the same row column E with the cell above and repeat through the spread sheet where owner is found.
I have tried to use Function =SUMIF(A3,"*owner*",E2:E3) This returns the Value above the cell in the row with owner but does not SUM them.
I have also tried this VBA CODE and it does the same thing.
Sub vba_sumif()
Dim gRange As Range
Dim sRange As Range
Set gRange = Range("A3")
Set sRange = Range("E2:E3")
Range("G2") = _
WorksheetFunction.SumIf(gRange,"*Owner*", sRange)
End Sub
Ideally it would return the summed cells in the above cell. using VBA
Thanks again,
Aaron
Well first of all, I would suggest just using the formula unless you have a reason not to:
Formula:: Your first error with the formula is both ranges need to be the same size.
VBA:: Same thing here with range sizes, then you also need to remember to make sure Excel knows what sheet you're referring to. I like to do this with a With My_Worksheet_Namestatement, then include a . before any range from that sheet. Just make sure to change the sheet name to your sheet name
Option Explicit
Sub vba_sumif()
Dim TableSheet As Worksheet
Dim gRange As Range
Dim sRange As Range
Set TableSheet = Worksheets("TableSheet")
With TableSheet
Set gRange = .Range("A2:A13")
Set sRange = .Range("E2:E13")
.Range("G2").Value = _
WorksheetFunction.SumIf(gRange, "*Owner*", sRange)
End With
End Sub
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 have a list of data format shown below.
???m,"N0000001","????(M)","201405","201405","0.57674","0.60831"
???{???Y,"N0000003","????(M)","201402","201402","0.78170","0.68470"
?}???n?j?`??,"N0000004","????(M)","201407","201407","1.54956","1.54956"
???????Y,"N0000015","????(M)","201412","201412","0.95776","0.98891"
and I want to delete the 'N000000' these part. only leave the valid number. the output should like this
???m,"1","????(M)","201405","201405","0.57674","0.60831"
???{???Y,"3","????(M)","201402","201402","0.78170","0.68470"
?}???n?j?`??,"4","????(M)","201407","201407","1.54956","1.54956"
???????Y,"15","????(M)","201412","201412","0.95776","0.98891"
Does anyone know what kind of operation should I do?
Try this.
Sub test()
Dim rngDB As Range
Dim Ws As Worksheet
Set Ws = ActiveSheet
Set rngDB = Ws.UsedRange
rngDB.Replace "N000000", ""
End Sub
your data list is very unclear. Assuming the data commening with N000000 is in an individual cell, the formula you could use would be:
for data in cell A1, place formula in cell B1:
with ';' as a delimiter:
=right(A1;LEN(A1)-LEN("N000000"))
with , as delimiter
=right(A1,LEN(A1)-LEN("N000000"))
So I have a big excel sheet with a bunch of empty cells in various locations. I want an easy to work with list of which cells are empty. I was hoping to make a new worksheet that was populated with the locations of the empty cells. I wanted to have this to just populate the cells I want it to. I kept the header from the worksheet I will be checking and added a blank cells count, so I want the following cells in the column to be populated by the list of empty cell locations.
Now I know I can use =ISBLANK to test if a cell is empty or not, but I only care about the cells that return TRUE. So I figure I'll need a loop. And I want the location of the cell so I can use =CELL. And to make this most readable I want to do this on a column by column basis.
But I want to populate a spreadsheet with this information in a manner similar to how functions work (I just want to copy and paste it to other cells and columns). But it's pretty clear that I am going to need VBA.
My question is how can I create a macro to populate my spreadsheet with a list of empty cells? How do I apply it to the cells?
I assume you have data in sheet1, I have used sample range// Range("A1:c15") however you can define range as per need and blank cells address will be published in next sheet.
Sub FindBlank()
Dim rng As Range
dim i as long
For Each rng In Sheet1.Range("A1:c15").SpecialCells(xlCellTypeBlanks)
i = i + 1
Sheet2.Cells(i, 1) = rng.Address
Next
End Sub
If you want a list of the cells that are empty, you can use Range().SpecialCells(xlCellTypeBlank):
Sub getEmptyCellAddresses()
Dim rng As Range
Dim ws as Worksheet
Set ws = Sheets("Sheet1") ' CHANGE AS NECESSARY
Set rng = ws.Range("A1:A15").SpecialCells(xlCellTypeBlanks) ' Edit/change range as necessary
ws.Cells(1, 2).Value = rng.Cells.Address ' Change `ws.cells(1, 2)` to whatever destination you like
End Sub
Edit: Ah, beaten by 16 seconds by #RamAnuragi ...but anyways, they're slightly different ways to tackle the question so I'll leave it.
Edit: For funsies, here's another way to put them all in a column, one row per cell...and more, per your comments.
Sub listEmptyCells()
Dim emptyAddresses() As String
Dim i As Long
Dim ws As Worksheet
Dim rng As Range
Set ws = Sheets("Sheet1") ' CHANGE AS NECESSARY
Set rng = ws.Range("A1:A15")
If WorksheetFunction.CountBlank(rng) = 0 Then
MsgBox ("No empty cells in the range")
Exit Sub
End If
emptyAddresses() = Split(rng.SpecialCells(xlCellTypeBlanks).Address, ",")
For i = LBound(emptyAddresses) To UBound(emptyAddresses)
ws.Cells(i + 1, 2).Value = emptyAddresses(i)
Next i
End Sub
I have several Named Ranges that contain constant data in one worksheet.
I have a target range where one of the Named Ranges will be copied to, on another worksheet.
The Named Range that will be copied is selected based on user input into other cells.
I have managed to create the name of the relevant Named Range in a single cell.
What I can't do (as I'm a VBA Noob who thought he could do all this without using VBA!), is create a Macro that reads the relevant cell, and then copies whatever Name Range it reads, into the target range.
Any assistance most humbly and gratefully accepted.
Let's say, the name of your range is MyRange
So to copy the range you have to do this
Range("MyRange").Copy
Now let's assume that Cell A1 of Sheet1 has the word MyRange. In such a scenario, you can retrieve the value of the cell A1 using Range("A1").Value
So
Range("MyRange").Copy
becomes
Range(Range("A1").Value).Copy
Here is a complete example. I am assuming that you want to copy to say Cell A1 of Sheet2
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
wsI.Range(wsI.Range("A1").Value).Copy wsO.Range("A1")
End Sub
i am not sure if thats what you need, but, if you need just to copy the content of the A1 cell from sheet1 to sheet2 for example, just do this:
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
you may wnat to encapsulate the code into a sub as well:
Sub copyvalues()
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
End Sub
...and finally, you must to insert a button, using onclick() event to fire the sub(which you must to allocate into a module)
sorry my bad english, hope it helps you.
Public Function copyNamevalues(srcName As Name, destName As Name)
srcName.RefersToRange.Cells.Copy (destName.RefersToRange.Cells)
End Function