I have a set of data in which when there is value in column B then i need to fill corresponding column A with number series 1,2,3... Is this possible in VBA??
I filled the below formula in VBA but it will return 1 in all cases... while I need 1,2,3 etc.
Range("I22").Select
ActiveCell.FormulaR1C1 = "=IF((NOT(ISBLANK(RC27))),""1"","" "")"
This should also do the trick:
=IF(LEN($AA22),COUNTIF($AA$22:AA22,"<>"&""),"")
You can paste the following formula in Column A. This will work.
=IF(B2="","",MAX($A$1:A1)+1)
Try using count formula? You'd want a variable to store the location....
Copy and paste this:
Dim rng as range
rng = Range("I22")
rng.FormulaR1C1= "=IF(ISBLANK(RC27), """", COUNT(" & Range("A1").Address(True, True, xlR1C1) & ":RC27))"
Then replace A1 with the top cell of the column you want to count, and I22 with the cell that the formula goes in.
Try this code, please:
Range("I22").Formula = "=IF((NOT(ISBLANK($AA22))),IF(I21="""",1,I21+1),"""")"
It will increment the I21 value, if any. If nothing, it will write 1 (not "1"), like start of iteration...
Related
I'm trying to search on the specific column(E), and if matched with the first 4 digit, I would like to copy the number to a different column.
Column E is where i would like to paste all the random number(dynamic)
Column A/B/C is static where i would add 4 digits from time to time.
Column I/J/K is where is would like to paste the result.
PS:
I'm doing it manually and would really appreciate if someone can help me out with the automation hence no code is provided. :(
Having ExcelO365 means you may use FILTER(). Therefor try the below:
Formula in I2:
=FILTER($E:$E,ISNUMBER(MATCH(--LEFT($E:$E,4),A:A,0)))
Drag right to K2. Now, this is dynamic and will change accordingly upon data entry in column E:E, or changing values in A:C.
this is the code to execute on sheet 1, it goes through the entire column E and validates through the formula of counting if in each of the first three columns and assigns the value found in the corresponding columns.
Sub macro()
Dim Static_Data As String
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Hoja1")
Active_row = 2
Do While Sht.Range("E" & Active_row).Value <> ""
Static_Data = Sht.Range("E" & Active_row).Value
For i = 1 To 3
If Application.WorksheetFunction.CountIf(Sht.Columns(i), Mid(Static_Data, 1, 4)) > 0 Then
Sht.Cells(Sht.Cells(Rows.Count, i + 8).End(xlUp).Row + 1, i + 8).Value = Static_Data
End If
Next i
Active_row = Active_row + 1
Loop
End Sub
For Excel versions that don't support FILTER or as an alternative you can use standard formulas for this.
If you use columns F-H as helper columns (and these columns can be hidden) then the formula in F2 will be:
=IF(NOT(ISERROR(VLOOKUP(VALUE(LEFT($E2,4)),A$2:A$100,1,FALSE)))=TRUE,$E2,"")
The formula can then be copied across and down. This will find your matches.
In order to then remove the blanks from the data you can use the following formula in I2 and again copy across and down. Depending on how many numbers you want to add in, you may want to extend the range A$2:A$100 in the top formula and F$2:F$100 in the bottom formula
=IFERROR(INDEX(F$2:F$100,AGGREGATE(15,6,(ROW(F$2:F$100)-ROW(F$2)+1)/(F$2:F$100<>""),ROWS(I$2:I2))),"")
I know VBA is probably the way to go but I believe this can be done using a few basic formulas.
I need "E2" to be replaced (cut/copy) with the contents from "A3" but only if "D2" = "Status:Active"...and so on down the sheet
the yellow and blue color-coding are only for this example and do not represent the whole sheet
this is a 7,000 line spreadsheet that was a report generated off some old system and I'm trying by best to collate and format.
Try in Column F starting with cell F2
=if(AND(E2="",D2="Status: Active"), A2, E2)
This will test to see if D2 has "Status: Active" and if it does, it will pull the value form A2. If it isn't then it will use the address already in E2
As explained in your comments that you are looking for A to be blank when F accepts a value from it (Cut/Paste)... there is no way for a formula to affect another cell, but... You could add a new Column B inserted after A and put the following formula in there: =if(A2<>G2, "", A2). Then hide Column A. The new B column will then only show values of Column A when it's not already in Column G (formerly column F before the insertion of the new column).
You could also do all of this through VBA, but that seems like more effort than it's worth when some simple formulas will get you there.
Seeing as you want the column A to be blank you can try this macro out:
Option Explicit
Sub SwapCols()
Dim oWs As Worksheet
Dim lRowNum As Long
Dim i As Long
Set oWs = ActiveWorkbook.Worksheets("Sheet1")
lRowNum = oWs.Range("A2").End(xlDown).Row
For i = 2 To lRowNum
If oWs.Range("D" & CStr(i)).Value = "Status:ACTIVE" Then
oWs.Range("E" & CStr(i)).Value = oWs.Range("A" & CStr(i)).Value
oWs.Range("A" & CStr(i)).Value = Null
End If
Next i
End Sub
Make sure you replace "Sheet1" with the name of your sheet. the macro basically checks if a cell in column D has the value "Status:ACTIVE" and if it does it copies the corresponding cell in column A to column E.
Just make sure if you do run this and you do not like the results do not save.
Hello from an unexperienced vba user.. I'm having trouble with the code for autofill to the last row when trying to use named ranges for the column. Everything seems to work fine when I use a hard coding for the column, in this case Column CW, and what I need is to replace this column CW with a named range so that the macro will still work when adding or deleting columns in the worksheet.
I used the following named ranges:
First_Date: This is the header cell of one of the columns (in this case AP5)
Second_Row: This is the range of columns I want to copy the formulas from (AP7:CW7)
Second_Cell: The cell where I want to start to autofill (AP7)
Last_Column: This is column CW that I want to use in the code. Autofill would be up to this column and down to the last row.
After searching in different threads, I came up with the following code that seems to work fine. How can I change column CW to a named range? Or do I need to change the code?
Dim Lr As Integer
Lr = Range("First_Date").End(xlDown).Row 'Searching last row
Rows(Lr).Insert Shift:=xlDown 'Inserting new row
Range("Second_Row").AutoFill Destination:=Range(Range("Second_Cell"), Range("CW" & Lr))
Can anyone assist me here please?
This will get the job done :-)
Sub RangerFiller()
'The Cell that holds the formula B1
OriginalFormula = Cells(1, 2).Formula
'copies formula down to the last column next to B but use can use another column as
'a counting column....the column that hold the last value
Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = OriginalFormula
End Sub
Someone gave me the solution:
Change
Range("CW" & Lr)
To
Cells(Lr, Range("Last_Column").Column)
I faced a similar problem because I don't want to hard code the cell reference. I found this solution below to be useful, by using "& ______ &" to replace the cell number that can be calculated using input box or formula.
Eg.
cell1 = last row of column A
Range("CW " & cell1 &" :CW & Lr),
where cell1 = any number that can be added via input box/formula.
Hope this helps!
Working on a excel macros where I am trying to add the cell values from above cells to calculate total value. This is what my data looks like
Here I want to add above cell values for each column to calculate the sum. To accomplish this i have written a macro as follows.
For cl = 2 To 5
Worksheets(5).Cells(4, cl).Formula = "=SUM(B4:B6)"
Next cl
This should set the formula to each cell in a row till 5 columns.
But it sets the same formula on all cells in a row it should get change according to column. How to set sum formula for each cell for corresponding column ?
Not sure I quite understand your code. You seem to be writing into row 4, but you also want to sum from row 4 to row 6. That will create a circular reference.
Let's assume the formula is written into row 3 instead. You will want to use the R1C1 reference style to make the cells to sum relative to the current cells.
A trick to learn what reference to use is:
Get a new worksheet, enter =SUM(B4:B6) into cell B3 and copy to the right.
Then click File > Options > Formulas and select R1C1 Reference Style.
Now inspect the formulas in the sheet. You will see this: =SUM(R[1]C:R[3]C)
This is what you need in the macro.
For cl = 2 To 5
Worksheets(5).Cells(3, cl).FormulaR1C1 = "=SUM(R[1]C:R[3]C)"
Next cl
some more details to R1C1 and R[1]C[1] style in formulas.
As far as I know R[1]C[1] creates a relative reference and R1C1 creates a absolute reference. But keep in mind that the figures for R[x] and C[y] are an offset to the cell which contains the formula.
That means if you want to show the sum of A1:B4 in C5 the code has to be like this:
Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R[-4]C[-2]:R[-1]C[-1])"
If you want to the same but ending up with an absolute reference is aht to look like this.
Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R1C1:R4C2)"
You can use very simple formula like below:
Sub sum_month()
Sheets("13").Activate
Range("D2").Formula = "=SUM(A1+A2+A3)"
End Sub
And next just hold and drag the cell to fill up another rows automatically.
For your case, you can use this:
For cl = 2 To 5
ColName = Left(Cells(1, cl).Address(0, 0), (Cells(1, cl).Column < 27) + 2)
ValueSum = "=SUM(" & ColName & "4:" & ColName & "6)"
Worksheets(5).Cells(4, cl).Formula = ValueSum
Next
Try something like this.
For cl = 2 To 5
ColName = Left(Cells(1, cl).Address(False, False), 1 - (cl > 26))
Worksheets(5).Cells(4, cl).Formula = "=SUM(" & ColName & "4:" & ColName & "6)"
Next cl
I think you can just reference the whole formula range and Excel will be smart enough to adjust the columns.
With no loop:
Worksheets(5).Cells(4, cl).resize(1,4).Formula = "=SUM(B4:B6)"
The two columns look like on this image.
When I want to show only the cells which contain a letter 'b', I can no longer see the text "Title1" and "Title2" which is normally visible in the column B.
I guess although the cells in column B are merged, the text is still bound to A3, respectively to A7.
So how can I at the same time filter the visible content and preserve the merged text? In simple words, I want to filter content by letter 'b' and I still want to see the text "title 1/2" in the column B.
You tagged excel so here is a solution in excel:
You need to click on that column with the merged cells and unmerge all cells.
Then you need to put this formula at the top of your list and enter it with ctrl+shift+enter(this will enter it as an array formula):
=OFFSET(C3,MAX(IF(NOT(ISBLANK(C$3:C3)),ROW(C$3:C3),0))-ROW(C3),0)
Then you need to autofill that down.(this function seems a little verbose but I just got it online - there is probably a simpler way to do this - but it finds the last nonblank cell in a range).
I think openoffice has similar functions so you should be able do the same or something similar in openoffice.
Alternatively if you are using excel you could click on the column you want to unmerge and run this macro:
Sub UnMergeSelectedColumn()
Dim C As Range, CC As Range
Dim MA As Range, RepeatVal As Variant
For Each C In Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
If C.MergeCells = True Then
Set MA = C.MergeArea
If RepeatVal = "" Then RepeatVal = C.Value
MA.MergeCells = False
For Each CC In MA
CC.Value = RepeatVal
Next
End If
RepeatVal = ""
Next
End Sub
Good Luck.
EDIT:
I found a Non-VBA solution that will work in both excel and openoffice and doesn't require you to enter it as an array formula(with ctrl+shift+enter):
=INDEX(B:B,ROUND(SUMPRODUCT(MAX((B$1:B1<>"")*(ROW(B$1:B1)))),0),1)
In open office I think you want to enter it like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0),1)
or maybe like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0))
You just need to autofill that formula down:
Your main problem seems to be the one "blank row" that you have left after the filter fields.
Remove it, and it will work fine.