Change the value of a column with vba code rather than loop - excel

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.

Related

Excel VBA problems with combined cells [duplicate]

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

How to check if Cell values balance each other

I am trying to Create a Formula that checks 4 Cells next to each other if they have the same number once positive and once negative see in the example:
If the formula sees there is a Plus 50 and a Minus 50 its has to colour the cell on the right side or the cells with the numbers blue.
The list is a inventory of multiple stores if one store sells alot of that product and may run out they ask another store to restock the product. Sometimes they forget to send a note. This List is supposed to make the control as easy as possible.
I expect the formula to color the cell on the right side of the list to be colored blue if 2 of the cells have the same value in plus and minus.
I tried to use cell formatting rules but its not possible to do it with that.
Another Example since people seem to have trouble understanding what the formula should do:
I marked every cell blue like the formula should and yellow colored value is the reason.
You can do this, using conditional formatting, using this formula (just for the first row):
=OR(A1+B1=0;B1+C1=0;C1+D1=0)
This formula checks if the sum of two adjacent cells equals zero, which is another way of saying that they should have the same value, but opposite signs.
Obviously, you might consider changing this formula, e.g.:
Instead of:
A1+B1=0
you put:
AND(A1+B1=0;A1<>0)
When the sum of two values equals zero and at least one of them is not zero, then both are not zero.
All this together in one formula yields the following:
=OR(AND(A1+B1=0;A1<>0);AND(B1+C1=0;B1<>0);AND(C1+D1=0;C1<>0))
Use such a formula in the conditional formatting of cell E1, and apply this for all cells in E column.
Try:
Option Explicit
Sub test()
Dim Row As Long, Column As Long
Dim rng As Range
'Let us assume that we use Sheet1 & columns A to F
With ThisWorkbook.Worksheets("Sheet1")
For Row = 2 To 100 ' <- Let us assume that data starts in row 2 and ends in row 100
Set rng = .Range("B" & Row & ":E" & Row)
For Column = 2 To 6
If .Cells(Row, Column).Value <> 0 Then
If Application.WorksheetFunction.CountIf(rng, (-1 * .Cells(Row, Column).Value)) > 0 Then
.Range("F" & Row).Interior.Color = vbBlue
Exit For
End If
End If
Next Column
Next Row
End With
End Sub

VBA Conditional Formatting with changable conditions

I am trying to set conditional formatting in 18 cells in third column ("C"). I have merged each 6 cells in first column ("A"), and unmerged (normal) cells in second column ("B"). I am trying to check for each next cell in row of column "C" if there is a "yes" in first row of column "A" or whether there is a "no" in "A" column and "pass" in "B" column. The trick is, I want to check only first row of "A" column, seventh, thirteenth and nineteenth (so with the step = 6) and check every row in "B" column. I try something like this:
Sub try()
Dim i As Integer
Dim j As Integer
i = 1
For j = 1 To 12
With Range("C1:C18")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=OR(Cells(i, 1) = ""Yes""; AND(Cells(i, 1) = ""No""; Cells(j, 2) = ""Pass""))"
End With
If j Mod 6 = 0 Then
i = i + 6
Next j
End Sub
But it does not work like that, I saw examples with specific Cells like "A1" or "A3" but I want a number to increase with every loop (so I tried it with Cells(row,column)).
You can do it in one statement on the whole range by using relative addresses, so what applies to C1 relatively to A1 and B1 will follow automatically in the subsequent rows of the range.
The only trick is to retrieve the value in column A, since this value is only available in cells A1, A7, etc. This is achieved by the expression OFFSET(A1,-MOD(ROW(C1)-1,6),0).
Sub doIt()
With Sheet1.Range("C1:C30").FormatConditions
.Delete
.Add(xlExpression, , _
"=OR(OFFSET(A1,-MOD(ROW(C1)-1,6),0)=""yes"",AND(OFFSET(A1,-MOD(ROW(A1)-1,6),0)=""no"",B1=""pass""))") _
.Interior.ColorIndex = 6
End With
End Sub
You can also do it from the GUI using the same formula; select cell C1 then select the whole range C1:C30, and click
Conditional Fomatting -> New rule -> Use a formula... and enter the same formula.
BTW, the expression can be further simplified if you dont care to check for "no", meaning if column A is assured to be either "yes" or "no".

excel macro : Change cell value dependant on original cell value

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

Search and return text from column in Excel

I have an Excel worksheet and am trying to figure out the formula for the following:
With my formula, I want to search all 700 rows of Column A for cells that contain aa_product11.12
Column A's value may or may not contain those values (some will, some will not). And, this is only the partial value. All of the cells have more data, i.e.
Column A
Sept01_aa_product11.12;
Oct01_aa_product11.12;
and so forth.
I need the full values of those cells that match, to show up in B1. So, the formula in B1 will search all of column A for aa_product11.12 and then B1 will look like:
Cell B1
Sept01_aa_product11.12, Oct01_aa_product11.12, Jan02_aa_product11.12,
Aug08_aa_product11.12
Thank you in advance for your help!
My answer will not immediately give you the concatenated results, but they are the step(s) before this:
Without formula:
Insert a filter and filter on Text with the criteria 'Contains aa_product11.12'.
Copy the column after the filter and paste the results in a fresh sheet.
You can then remove the filter and transfer the results back to the original sheet.
With a formula:
In B1, put the formula:
=IFERROR(INDEX($A$1:$A$700,SMALL(IF(ISNUMBER(FIND("aa_product11.12",$A$1:$A$700)),ROW($A$1:$A$700),9.9E+208),ROW())),"")
But this is an array formula which will require using Ctrl+Shift+ to work properly. After this, fill down the formula until there are no more results returned to proceed to the last step.
After doing either of these, you can run a CONCATENATE on the cells:
=CONCATENATE(B1,", ",B2,", ", ... )
Okay, You need to open your Visual Basic Editor and create a module. Place the following code in your module:
Function Get_Data()
Dim Strg As String
Dim Boo As Boolean
Boo = False
Dim i
i = 1
For i = i To 65000
Strg = Cells(i, 1).Value
If InStr(1, Strg, "aa_product11.12") Then
If Boo = True Then
Get_Data = Get_Data & Cells(i, 1).Value & ", "
Else
Get_Data = Cells(i, 1).Value & ", "
Boo = True
End If
End If
Next i
End Function
Save file as a Macro-Enabled workbook. Then go to cell B1 and type in: =Get_Data()
Press enter and the function should work. Let me know if you have any questions.

Resources