I'm pasting some dates from a csv file into excel using a macro, and using a find/replace to alter the format into one excel can recognise, from 2019_10_22_08_43_23 to 22/10/2019 08:43:23, for example. Excel won't recognise this text as a date until you manually select the cell and press enter, and therefore won't display the dates on a graph correctly until this has been done. My current solution is using
For Each c In cycleRange.Cells
c.Select
SendKeys "{F2}", True
SendKeys "{ENTER}", True
Next
to manually press enter in each cell but this takes quite a long time. I have tried using TextToColumns but this isn't working. If I manually select the cell range, and navigate the data menu to click TextToColumn myself it corrects the cell format, but doing this through a macro doesn't do anything.
cycleRange.Select
Selection.TextToColumns Destination:=cycleRange, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
What's the fastest method to replace looping through each cell and using Sendkeys to activate them?
You want to convert each cell to number. Here is the code:
Dim cellValue As Double
For Each c In cycleRange.Cells
If IsNumeric(c.Text) Then
cellValue = c.Value
c.Clear
c.NumberFormat = "0.00"
c = Val(c.Text)
' c = c.Text * 1
' c = CDbl(c.Text)
Else
MsgBox ("Not a number (" & c.Address & ")")
End If
Next
It would be easier to combine the string conversion and cell formatting into one VBA operation
Something like this
Sub Demo()
Dim rng As Range
Dim cl As Range
Dim dat() As String
Set rng = [A1:A10] ' update to suit your needs
For Each cl In rng
If cl.Value2 Like "####_##_##_##_##_##" Then
dat = Split(cl.Value2, "_")
cl = DateSerial(dat(0), dat(1), dat(2)) + TimeSerial(dat(3), dat(4), dat(5))
cl.NumberFormat = "yy/mm/dd hh:mm:ss" ' update to suit your required format
End If
Next
End Sub
You can simulate to enter a cell and push enter by replacing a specific number by itself i.e. you replace 1 by 1. As such, the format changes. Doing this for 0, 1, then 2 up to 9 will cover all your cells, whatever figure is in the cell.
Here a macro:
Sub Repl1by1()
Cells.Replace What:="0", Replacement:="0", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="1", Replacement:="1", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="2", Replacement:="2", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="3", Replacement:="3", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="4", Replacement:="4", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="5", Replacement:="5", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="6", Replacement:="6", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="7", Replacement:="7", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="8", Replacement:="8", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="9", Replacement:="9", LookAt:=xlPart, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
Related
I am trying to create a script in excel to duplicate sheets, rename with cell value from active sheet and then replace the formula values with the values from the row below on the main sheet labelled 'Master Working'.
For the last part, I'm trying to amend the below code recorded with a macro such that instead of '19' to '20', the existing number in the formula becomes N and replaced with N+1.
The formulas for each cell in the range are a simple lookup like the below from main tab.
='Master Working'!B19
Range("D5:D8").Select
Selection.Replace What:="19", Replacement:="20", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("B14:M14").Select
Selection.Replace What:="19", Replacement:="20", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
First read your value to replace into a variable N:
Dim N As Long 'read N value from master worksheet
N = ThisWorkbook.Worksheet("Master Working").Range("B19")
Then Replace using that variable N to replace it with N + 1.
ActiveSheet.Range("D5:D8").Replace What:=CStr(N), Replacement:=CStr(N + 1), LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveSheet.Range("B14:M14").Replace What:=CStr(N), Replacement:=CStr(N + 1), LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Note that I changed LookAt:=xlPart to LookAt:=xlWhole otherwies it will replace in 105193 the 19 by 20 like 105203 which is probably not what you want.
I've created this code that is supposed to replace the commas in a selection with dots if there are any. The code, however, doesn't work if the selection starts a cell containing a dot, but it works if it starts with a cell containing a comma. It even works if it starts with a comma, then a cell with a dot and then again a cell with a comma. Here is the code:
Public Sub DeleteDotsReplaceCommasWithDots()
For Each cell In Selection
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
If InStr(ActiveCell.Value, ".") > 0 And InStr(ActiveCell.Value, ",") > 0 Then
Selection.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
ElseIf InStr(ActiveCell.Value, ".") = 0 And InStr(ActiveCell.Value, ",") > 0 Then
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormul
End If
Next cell
End Sub
Any idea about why this is happening? Thanks :)
You seem to be running your search/replace on ActiveCell, rather than each cell you're cycling through with your For..Each loop. I've tidied it up slightly using the With cell.. End With block.
Try this:
Public Sub DeleteDotsReplaceCommasWithDots()
For Each cell In Selection
With cell
.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
If InStr(.Value, ".") > 0 And InStr(.Value, ",") > 0 Then
.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
ElseIf InStr(.Value, ".") = 0 And InStr(.Value, ",") > 0 Then
.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula
End If
End With
Next cell
End Sub
I have a set of data which only pulls as MMM-DD-YYYY. I'd like to convert it to a date (MM/DD/YYYY format) to look it up versus another set of data.
I recorded a macro to simply replace the months with their respective numbers individually but I know there has to be a better way to do this. Below is my
current code:
With ws1.Cells
.Replace What:="jan-", Replacement:="01-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="feb-", Replacement:="02-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="mar-", Replacement:="03-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="apr-", Replacement:="04-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="may-", Replacement:="05-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="jun-", Replacement:="06-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="jul-", Replacement:="07-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="aug-", Replacement:="08-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="sep-", Replacement:="09-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="oct-", Replacement:="10-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="nov-", Replacement:="11-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="dec-", Replacement:="12-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End With
This will convert your text string into a true date for the active cell:
Sub datefix()
Dim s As String
s = ActiveCell.Value
arr = Split(s, "-")
ActiveCell.Value = arr(1) & " " & arr(0) & " " & arr(2)
End Sub
You can format it or loop it to your heart's content.
(I am using US locale)
EDIT#1:
With your desired format:
Sub datefix()
Dim s As String
s = ActiveCell.Value
arr = Split(s, "-")
ActiveCell.Value = arr(1) & " " & arr(0) & " " & arr(2)
ActiveCell.NumberFormat = "mm/dd/yyyy"
End Sub
Before:
and after:
I have a column in excel that has two values "Disqualified" and "Open".
I want to use an If Statement using VBA to change the disqualified values to 0 and the Open values to 1.
Here is the excel formula that shows what I want to do
=IF(H:H="Disqualified","0","1")
I think I need a for loop to loop through all the values in column H but can't seem to get this to work. Thanks
Taking the code in your self-answer, it can (and should) be refactored to get rid of the Select parts (which will almost always generate problems in the future).
Sub changeValues()
With Worksheets("your_sheet_name")
With .Range("H1:H" & .Range("H" & .Rows.Count).End(xlUp).Row)
.Replace What:="Disqualified", _
Replacement:="0", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="Open", _
Replacement:="1", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End With
End With
'I doubt if the next line is needed
'ActiveWindow.SmallScroll Down:=-66
End Sub
Well there is probably a better way of writing this but this macro does the job.
Sub changeValues()
'
' changeValues Macro
'
'
Range(Selection, Selection.End(xlDown)).Select
Selection.replace What:="Disqualified", Replacement:="0", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.replace What:="Open", Replacement:="1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveWindow.SmallScroll Down:=-66
End Sub
I have some macro like:
Columns("F:M").Select
Selection.Replace What:=",", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
But i want to put the current date (or even just a string of text) to be put in cell A of the row where a replace occurred.
I imagine you will need to change your replace to a find and replace. Something like:
Dim c As Range
Columns("F:M").Select
Set c = Selection.Find(What:=",", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False)
If Not c Is Nothing Then
Do
c.Replace What:=",", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells(c.Row, 1).Value = Date
Set c = Selection.FindNext(c)
Loop While Not c Is Nothing
End If