Delete specific content in Excel - excel

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"))

Related

Copy non-contiguous cells and paste in a row retaining number format Excel VBA

I hope someone can help me with this as it's driving me up the wall!
There are 5 non-contiguous cells in a worksheet that I want to copy to the next empty row on another worksheet whilst retaining the number formatting (which varies). I have this so far but am struggling working out how to retain formatting. Can anyone please help? Thanks I anticipation.
`With wsCalc
For bRun = 1 To 4
bData(bRun) = Application.Choose(bRun, .Range("g2"), .Range("b2"), .Range("R2"), .Range("Q14"))
Next bRun
End With
wSResults.Cells(Rows.Count, "a").End(xlUp).Offset(1).Resize(, 4).Value = bData
`
Here's a possible solution, using your hard-coded cell addresses. You will have to set wsCalc and wsResults to their proper worksheets. Slightly more elegant would be to define a "non-contiguous" range on your wsCalc sheet (select the 1st cell, keep Ctrl pressed and select the next one etc, then type a name in the drop-down box just to the left of the formula bar).
Option Explicit
Sub CopyWithFormat()
Dim wsCalc As Worksheet
Set wsCalc = ActiveSheet 'Or whatever your calc sheet is
Dim rngSource As Range
Set rngSource = wsCalc.[G2,B2,R2,Q14]
Dim wsResults As Worksheet
Set wsResults = ActiveSheet 'Or whatever your result sheet is
Dim clDest As Range
Set clDest = wsResults.Cells(Rows.Count, "a").End(xlUp).Offset(1)
Dim cl As Range
For Each cl In rngSource.Cells
clDest.Value = cl.Value
clDest.NumberFormat = cl.NumberFormat
Set clDest = clDest.Offset(1)
Next cl
End Sub
Instead of using .Value, try .Text. It retains formatting. See below.
Gary's Student is right, text is read only, it should be used for the input not the output.
bData(bRun) = Application.Choose(bRun, .Range("g2").Text, .Range("b2").Text, .Range("R2").Text, .Range("Q14").Text)
I also agree with other answer the entire code could be set up more straight forward.

Excel List of Blank Cells

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

macro to choose sheet, column, and rows to filter based on cell criteria

New to Excel macros, trying to get something figured out for my work! Any help is greatly appreciated!
I'm trying to make a macro that will choose the sheet, the column, and the row when filtering, all based on cell criteria.
So in Sheet1, if someone types in the sheet name "Sheet6" in Cell A1, column header "col_B" into Cell A2, and the filter criteria "banks" into Cell A3, then the macro will look in "Sheet6", find the column with the header "col_B" and filter all rows with the text "banks". Is this possible?
Thank you ahead of time for the help!
You may be able to adapt this:
Sub FeedingTheVampire()
Dim sh As Worksheet, cHead As String, crit As String
Dim rcol As Range
With Sheets("Sheet1")
Set sh = Sheets(CStr(.Range("A1")))
cHead = .Range("A2")
crit = .Range("A3")
End With
Set rcol = sh.Range("1:1").Find(What:=cHead, After:=sh.Range("A1")).EntireColumn
rcol.AutoFilter Field:=1, Criteria1:=crit
End Sub
I just got very close using code from another workbook. I don't fully understand the other code, as I got some of it off of google searches. The only thing this code doesn't do is reference the column (that R1 variable is supposed to, but I'm not sure how to put it in). A lot of it looks similar to the post from Gary's Student. And I'm sure there is information in that to make this work. So, thank you again Gary's Student!
Sub Filter_Stuff()
Dim r As Range,
Dim strName As String,
Dim R1 As String,
Dim s2 As Worksheet, s1 As Worksheet, sA As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
strName = Range("C5")
Set sA = Sheets(strName)
R1 = s2.Range("C6")
Set r = sA.Range("1:1").Find(What:=s2.Range("C6").Value, After:=sA.Range("A1"))
r.AutoFilter Field:=1, Criteria1:=s2.Range("C7").Value
End Sub

Excel Macro or VBA for making selected range values absolute

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.

store columns of a formula with vba

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

Resources