Date not showing after paste - excel

I have a report file, in some section i am copying a table from a remote computer into local excel file, but after copy date's are not like date, i need to search & replace everytime ( search "." replace with "." )then dates comes. I have a vba code but not correcting in all sheets.
Sub tarihecevir()
Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
fnd = "."
rplc = "."
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
End Sub
It works with normal ctrl+H but macro doesnt fix it.
Please help me.

Related

VBA Excel Find string in column and offset delete and repeat

I have a working code to find a specific string in a column of a specific sheet, offset and clear the contents of a specific cell. However it only clears the first occurrence of this search and I would like to have the code work on all occurrences. Can someone help me to wrap a Loop or a FindNext around this code because I wasn't able to. Please see here below the code I already have. Thnx
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
On Error Resume Next
Worksheets(2).Columns("A:A").Select
Set Action6 = Selection.Find(What:=SearchValue6, After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Action6 Is Nothing Then
'MsgBox "No clearings made in " & ActiveWorkbook.Name
Else
Action6.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.ClearContents
End If
Please, try using the next updated code and send some feedback:
Sub FindMultipleTimes()
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
Dim ws As Worksheet: Set ws = Worksheets(2)
Dim firstAddress As String
Set Action6 = ws.Columns("A:A").Find(What:=SearchValue6, After:=ws.Range("A1"), LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Action6 Is Nothing Then
firstAddress = Action6.address
Do
Action6.Offset(0, 1).ClearContents
Set Action6 = ws.Columns("A:A").FindNext(Action6) 'find the next occurrence
Loop While Action6.address <> firstAddress
Else
MsgBox SearchValue6 & " could not be found in column ""A:A"" of sheet " & ws.name
End If
End Sub
I only adapted your code, but do you want letting the workbook necessary to extract SearchValue6 value, open?

Replace the Cell Value throughout the Entire Workbook

I am using FndRplce() that code which replaces fnd = "Ilya Malikzada" with rplc = "Arham" throughout the entire workbook.
But how to create such a code which works like: When i change any name from the below attached picture that effect should apply on entire workbook.
For example "Ilya Malikzada" A2 has this first name so when i write on cell A2 Arham then wherever is "Ilya Malikzada" on entire workbook should replace with Arham.
your help will be much appreciated. Thanks
Sub FndRplce()
Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
Dim ReplaceCount As Long
fnd = "Ilya Malikzada"
rplc = "Arham"
For Each sht In ActiveWorkbook.Worksheets
ReplaceCount = ReplaceCount + Application.WorksheetFunction.CountIf(sht.Cells, "*" & fnd & "*")
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
End Sub
If you want the name input to be a variable you can reference the cell specifically to do so. Just make it obvious so the user knows that's the input. I use Cell A1, but you can adjust this and the sheet name accordingly. This reference can be a excel-named reference as well as long as you keep it within it's quotes
The adjustment I made was to your rplc line like so
ActiveWorkbook.Sheets("YourSheetName").Range("A1").Value
Sub FndRplce()
Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
Dim ReplaceCount As Long
fnd = "Ilya Malikzada"
rplc = ActiveWorkbook.Sheets("YourSheetName").Range("A1").Value
For Each sht In ActiveWorkbook.Worksheets
ReplaceCount = ReplaceCount + Application.WorksheetFunction.CountIf(sht.Cells, "*" & fnd & "*")
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
End Sub

Clear different range of data in multiple worksheets

I have a workbook that is used as a template to fill in data. The data is cleared and the workbook is reused.
The workbook has multiple worksheets and the range that needs to be cleared is different in every worksheet.
Let's say I want to clear the data in the range A10:Y50, I put value "Start" in the cell Z10, as a starting point to clear data. "Start" is located in different cell in every worksheet.
The code is clearing data based on the "Start" located in the first worksheet and not independently for each worksheet.
Sub TestReset()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
If sht.Name <> "Sheet1" And sht.Name <> "Sheet2" Then '
Dim iRow As Long, iMax As Long
iRow = Cells.Find(What:="start", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
iMax = Cells(iRow, "A").End(xlDown).Row
sht.Range("A" & iRow & ":AY" & iMax).ClearContents
End If
Next sht
End Sub
As #Tony Dallimore mentioned in his comment you need to specify in what sheet you are looking for specific cells (if you dont specify it it assumes you are looking in ActiveSheet). So it is always best to specify with what sheet you work. It is good to use With statement for that. When you use With then it is enough to use only dot "."
Sub TestReset()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
With sht
If .Name <> "Sheet1" And .Name <> "Sheet2" Then '
Dim iRow As Long, iMax As Long
iRow = .Cells.Find(What:="start", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
iMax = .Cells(iRow, "A").End(xlDown).Row
.Range("A" & iRow & ":AY" & iMax).ClearContents
End If
End with
Next sht
End Sub
Also use Dim iRow as Long before for each loop... But here is added example of how you can manage sheets to skip (Create sheet "Setup" and in cell A1 add name to skip, for example Sheet1, in cell A2 add Sheet2 and it should do the trick.
Sub WorkInUnSpecifiedSheets()
Dim xRng As Range
'sheet "Setup" must exist and Range A1 contains name of sheet to skip, current region might not work on some PCs. But it is simple
Set xRng = ThisWorkbook.Sheets("Setup").Range("A1").CurrentRegion
'you can also use another method to specify range... Named ranges for example or using last row and last column...
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets 'i would recommend using ThisWorkbook or Workbook variable instead of Active
If xRng.Find(What:=sht.Name, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) Is Nothing Then
'your code to work in unspecified sheets
End If
Next sht
End Sub

Search and selecting a string in a Worksheet using VBA

I know selection is not good to use. However, I need to find a string in another sheet and need it to be selected (it can even select and change the color) so the user will able to see it.
My code is only taking me to the sheet but not to the cell where the string I need to find is.
Sub Risk1()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "1."
Application.FindFormat.Clear
Sheet2.Activate
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
FirstFound = cl.Activate.cell
End If
Next
End Sub
The string that I am looking for can be in any cell of column A.
Try this. If it's in one sheet no need to loop through all of them. It's still not clear to me what happens if the value is found more than once?
Btw you don't need VBA for this, the worksheet Find will do exactly this.
Sub Risk1()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "1."
Application.FindFormat.Clear
With Sheets("Over").Columns(1)
Set cl = .Find(What:=SearchString, After:=.Cells(1, 1), LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cl Is Nothing Then Application.Goto cl 'better than select or activate and sheet does not need to be active first
End With
End Sub

Excel VBA change value

I have Excel sheet where is phone numbers at international format (00358xxxxxxx)
and I need to replace that 00358 with 0
I manage to remove that 00358 but I lost leading 0 from phone number.
All cells are text-type
Worksheets("Sheet1").Range("A:B").Select
Selection.NumberFormat = "#"
Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
fnd = "00358"
rplc = "0"
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
I think you can use:
rplc = "'0"
Good luck.

Resources