When recording a macro, i want to change the current value of a cell to a new value dependant on the original value. not set it to a "recorded" value.
Example : value in cell needs to change from .1234 (text) to 0,1234 (number). And
.56789 to 0,56789
My work method is:
record macro
"F2" : to change value,
"home" : go to beginning,
"del",
"del",
"0",
",",
"return",
stop record macro
but when i use the macro on other fields, the value is changed to 0,1234 even when original value is .5678 for example.
This can be done in VBA but the most simple solution (if all your values begin with ".") is to use the built in "Text to Columns" Excel option (by simply selecting your entire column then ALT+A, +E, +F). If this has to be done with VBA please tell. Hope this alone helps.
Edit I've wrote this code to solve your problem (based on the rule of the cell values are starting with "." and then switching to numbers is made by adding a "0" to the initial text). Then "number" format is picked up by Excel as implicit format.
Sub ChangeFormat()
For i = 1 To 2 'This is the row index (it's now set to the 1st 2 rows.)
For j = 1 To 2 'This is the column Index (i.e. column 2 is B, 1 is A, etc.) (it's now set to the A and B columns)
If Left(ActiveSheet.Cells(i, j), 1) = "." Then
ActiveSheet.Cells(i, j).Value = "0" & ActiveSheet.Cells(i, j).Value
End If
Next j
Next i
End Sub
Related
This question already has answers here:
Get value of a merged cell of an excel from its cell address in vba
(4 answers)
Closed 1 year ago.
I am writing a Excel VBA program to generate text.
I am using IF statements to find out if some specific text is in in a cell and concatenate a string based on that.
I have a problem with combined cells:
IF Cells(1,1) Like "A" and Cells(1,2) Like "---" Then
concatenate something
end if
The above works
IF Cells(2,1) Like "B" and Cells(2,2) Like "---" Then
concatenate something
end if
This does not work, because the Cell(2,2) is empty according to
MsgBox (Cells(2, 2).Value) instead of the "---".
Is it possible to get the code to work, without splitting the cells again?
I would like to go row by row concatenating text, the VBA code should recognize the cellvalue one cell right to "B" as "---".
Cells(1,1).Value is "A". Cells(1,2).Value is "---".
Cells(2,1).Value is empty. Cells(3,2).Value is empty.
Cells(3,1).Value is "B". Cells(3,2).Value is empty.
Just because you merge the cells above, doesn't change that the cell is in Row 3. Only the top-left cell in a Merged Range has a value. (Although, there are ways around that.)
You can check if a cell is merged using Range.MergeCells. You can get the value of a Merged Cell using Range.MergeArea
For example:
Cells(1,1).MergeArea.Cells(1,1) is "A". Cells(2,2).MergeArea.Cells(1,1) is "---". Cells(1,1).MergeCells is True.
Cells(2,1).MergeArea.Cells(1,1) is "A". Cells(2,2).MergeArea.Cells(1,1) is "---". Cells(2,1).MergeCells is True.
Cells(3,1).MergeArea.Cells(1,1) is "B". Cells(3,2).MergeArea.Cells(1,1) is "---". Cells(3,1).MergeCells is False.
If you are using a loop to run through the cells, consider using a While or Do instead of a For, since you can increment by different numbers for each iteration:
Dim rowCurrent AS Long
rowCurrent = 1
While Cells(rowCurrent,1).Value <> ""
If Cells(rowCurrent,2).MergeArea.Cells(1,1).Value = "---" Then
Select Case Cells(rowCurrent,1).MergeArea.Cells(1,1).Value
Case "A"
'concatenate something
Case "B"
'concatenate something else
Case "C"
'concatenate a third thing
Case Else
'throw an error?
End Select
End If
'Skip the rest of the merged cell
rowCurrent = rowCurrent + Cells(rowCurrent,1).MergeArea.Rows.Count
Wend
I would like to change the range in the counfif-formula by using vba. By clicking a button the Range A3:A3 changes to A3:A4, then clicking the button again A3:A4 changes to A3:A5, and so on... I managed to create a constant vba-formula, but I do not know how to proceed. Anybody? :)
Excel
VBA
I think you need a dummy cell/value to achieve, The dummy cell will "count" how many times you have clicked the button. My solution will require a dummy cell.
Sub Countif_function()
Dim start_value As Long
start_value = Cells(1, "D").Value 'Take the value from cell D1
If start_value <= 3 Or Cells(1, "D").Value = "" Then start_value = 3 ' If the value is less than 3 (since you start at row 3) or if the value is empty then set the minimum start value to 3 (otherwise countif will fail).
Cells(3, "D").Value = Application.WorksheetFunction.countif(Range(Cells(3, "A"), Cells(start_value, "A")), "A") 'Using the inbuild function in VBA to retrieve the countif
Cells(3, "E").Value = "=COUNTIF(A3:A" & start_value & ",""A"")" 'Just for view purpose of the final formula result, can be ignored in the code
Cells(1, "D").Value = Cells(1, "D").Value + 1 'Add +1 row. This will be used as the start value for next time the code is executed.
End Sub
I used "A" as criteria since you used it in your example code, but could be change to "C3" or some other cell.
Result:
The alternate if you don't want a dummy cell to count as per the previous answer is to read the formula in the cell into a string variable via range.formula, split it on the comma, read the last digit of the first entry in the split array, increment by 1. (If the digit is a 9, then check whether the preceding character is a number or letter, if letter, make the 9 a 10, if number, increment that by one and make the 9 a 0, if the preceding character is also a 9, recursive call). So, yeah, unless you really cannot have the dummy cell, use that solution, it's easier.
I want to add "#" before each cell in a column (have more than 13000 cells), and add "000" to another column through vba excel code. I can achieve this with loop but it takes a long time when running the macro. Can anyone help me with more effective solutions?
for each cell in column
cell = "#"& cell
next cell
for each cell in column
select case len(cell)
case 2: cell = "'000" & cell
case 3: cell = "'00" & cell
end select
next cell
Desired result:
456 => #456
10 => 00010
however it runs very slowly.
Use display format:
Range("A1:A1300").NumberFormat = "\##"
Range("B1:B1300").NumberFormat = "00000"
This does not change the underlying value, but it will display correctly.
To read the value out use Cell.Text to retrieve a string with the leading zeros.
I have created a userform to track service requests and I am having trouble with this last part.
There are 2 columns in question the first "A" is service ID the second is "B" Date. I want "B" to be today's date and "A" to start at 1 and count up, when I add a new request tomorrow I want "A" to start at 1 again.
Here is the code for column "A", when a new record is created "A" goes up but when the date changes it does not start back at 0. Any ideas?
'Service ID
'check row above
'if "Date" then out put "1"
'if yesterday then out put "1"
'if today then ouput + 1
If (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) = "Date" Then
serviceorder = "1"
ElseIf (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) < Date Then
serviceorder = "1"
ElseIf (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) = Date Then
serviceorder = (Sheet1.Cells(Rows.Count, "A").End(xlUp).Value) + 1
Else
End If
Thanks in advance.
Why not just make column A a COUNTIF formula to show the number of times the date in the corresponding column B cell shows up in all of column B? That way when the date changes, it will auto reset to 1 and each new time a request is entered the same day, it the ID will increment by 1. The VBA formula would be:
With Sheet1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
.Formula = "=COUNTIF(B:B,B" & .Row & ")"
'Uncomment this next line if you want column A to contain values instead of formulas
'If you do uncomment it, make sure you have already put the date in the B column for this entry
'.Value = .Value
End With
I think the general problem in your functions is the Sheet1.Cells(Rows.Count, "B").End(xlUp). Rows.Count means - as it says - the count of rows in your sheet and is 1048576. In my opinion you take the very last cell of the column and End(xlUp) won't make it better. I think Sheet1.Cells(1, "B").End(xlUp) should work better. Sorry if I'm wrong for I can't reproduce your scenario here.
I have very big problem and i can't find answer for it. Maybe You can halp me.
In Sheet "Intro" I have row (5), in wich are formulas:
(i) C5(='Sheet1'!$A$1);
(ii) I5(=OFFSET('Sheet1'!$A$1;17;1;1;));
(iii) In this row also is number of my tables line, i. e B5(=1). This number "1" is also a hyperlink to Sheet1.enter code here
I have Macro, which every time automaticaly insert new row after last filled row, i. e. when i have filled 5 row and run my Macro, it inserts line 6. Therefore necessary to automatically fill this row with my formulas, that I mentioned above.
More specifically, the new row should be filled in like this:
When new row is 6:
(i) C6(='Sheet2'!$A$1);
(ii) I6(=OFFSET('Sheet2'!$A$1;17;1;1;));
(iii) B6(=2). This number "2" is also a hyperlink to Sheet2.
When new row is 7:
(i) C7(='Sheet3'!$A$1);
(ii) I7(=OFFSET('Sheet3'!$A$1;17;1;1;));
(iii) B7(=3). This number "3" is also a hyperlink to Sheet3.
When new row is 8:
(i) C8(='Sheet4'!$A$1);
(ii) I8(=OFFSET('Sheet4'!$A$1;17;1;1;));
(iii) B8(=4). This number "4" is also a hyperlink to Sheet4.
And......
Maybe You can help me?
In advance thank you sincerely.
As you already have a macro in place, I would extend the macro with some additional functionality to take care of this.
Throug VBA it is simple to set the Formula of a cell.
Sub createRow()
Dim i As Integer
i = 5
Dim sheetOffset As Integer
sheetOffset = -4
FillInRow i, sheetOffset
End Sub
Sub FillInRow(i As Integer, sheetOffset As Integer)
Sheets("Intro").Range("C" & i).Formula = "=Sheet" & CStr(i + sheetOffset) & "A1"
Sheets("Intro").Range("I" & i).Formula = "=OFFSET(Sheet " & CStr(i + sheetOffset) & "3!$A$1;17;1;1;"
End Sub
You should set the offset carefully.
I could not understand what you needed in the "B" column. but seeing this example I am sure you can expand this.
If this is too vague, please say so and I can ellaborate.