Capital letters to cells in column - excel

I'm trying to change all letters into capital letters in all cells holding data in column B2 and down. Got this VBA.
Sub CapitalLettersColumnB()
'Capital Letters to names in column B (from B2 and down)
'Column B holds headers!
With Range("B2", Cells(Rows.Count, "B").End(xlUp))
.Value = Evaluate("INDEX(UPPER(" & .Address(External:=True) & "),)")
End With
End Sub
When my sheet holds 2 rows of data (and more), not counting headers, the vba works just fine.
But when my sheet only holds 1 row of data, not counting headers, name in column B (cell B2) is replaced with #VALUE!.
Anyone that can help spot what's wrong, and how to correct my vba?

The problem is INDEX. The funtion returns a position in a Range or Array, not a single value. If you would evaluate the formula it would come down to this:
=INDEX("VALUE",)
Instead of a correct:
=INDEX({"VALUE1","VALUE2"},)
Not feeding the INDEX a range or array will then return the error
To correct your code you need to make sure there is always a range object larger than one cell, so for example:
Sub CapitalLettersColumnB()
With Range("B1", Cells(Rows.Count, "B").End(xlUp))
.Value = Evaluate("IF(ROW(" & .Address & ")>1,INDEX(UPPER(" & .Address & "),)," & .Address & ")")
End With
End Sub
This will now work from B1 onwards, but will leave that cell intact through the ROW test in our IF statement.
Another small note, I would definately use an explicit cell reference (including at least a worksheet)

Honestly not sure what is wrong. (I think it's the same reason as range("B2:B2"). You can't set a range spanning the same cell)
Your Cells(Rows.Count, "B").End(xlUp) returns the value of the last cell which I'm not sure how that works.
But you could change the code to do a with the range to lastrow.
But if the lastrow is 2 the code will fail, so the if goes to else and it only changes the Ucase of cell B2.
If Cells(Rows.Count, "B").End(xlUp).Row > 2 Then
' more than one row after B2
With Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)
.Value = Evaluate("INDEX(UPPER(" & .Address(external:=True) & "),)")
End With
Else
' no rows past B2
Range("B2").Value = UCase(Range("B2").Value)
End If

Related

Sum a range of cells above the active cell, the range of cells is spaced over 3 columns

After I clear all data below the last row with data, I offset to an active cell.
The active cell will be in column I, could be anywhere in column I.
I want to sum all the data above the active cell in the range from columns G, H & I.
Then put the total sum in the active cell. Not sure how to loop through this if anyone can help that would be great! Thanks.
Range("I" & cells(Rows.Count, "I").End(xlUp).row).Select
ActiveCell.Offset(3, 0).Select
Sub Test()
Dim lCurRow As Long
'Locates the last FILLED Cell in I and saves row number!
lCurRow = Range("I" & Rows.Count).End(xlUp).Row
'Place formula one row down Formula Creation
Cells(lCurRow + 1, "I").Formula = "=Sum(I" & lCurRow & ":G1)"
End Sub
All done w/o selects.
HTH

VBA to change formula depending on the row the iteration is in

I have a range of values in column G. I want to create a vba that changes the formula for each cell number depending on the row that is used in the iteration.
Sub the()
Dim c As Range
'//loop it
For Each c In Range(Range("G1"), Range("G1").End(xlDown))
c.Formula = "=IF(C" & c & ">0,2,B" & c & "))"
Next
End Sub
I know I got thumbs down, but I found the answer to my own question by myself. What I was trying to find out was how to put variables from other columns into a formula. Fo example, below I have 2*(E2) the formula entered into D2. The same iterations occur in D3, D4 etc.
It is hard to word this question properly.
Sub goth()
Dim c As Range
Dim a As Integer
'//loop it
For Each c In Range(Range("D2"), Range("D2").End(xlDown))
a = c.Offset(0, 1)
c.Formula = "=2*" & a
Next
End Sub

Copy paste a fixed row based on some condition else skip to next column in that row

Have data set in I11:X11 and I want to copy formulas seating I12:I12 into I13:X20 based on data contained in I11:X11.
Starting with I11, if that contains certain value lets say TEST, then want to increment row for that range to next column that is J11 and if J11 <> TEST, then copy J12:X12 to J13:X20.
Further want to skip pasting this entire logic based on flag seating in column H13:H20, for example if H13 = Y, then skip to next row.
Adding a screenshot to further explaining the issue.
Condition should start with first member in range I11:X11, if it encounters first member <> TEST till T11 , then it should start copying from that range. In this case it encountered first <> TEST member at L11, then it should copy from L12:T12 to L13:T24 and V12:X12 to V13:X13. Further this logic should work on the flag contained in column H. If this column H Contains Y,then above logic should not paste in that row, this pasting activity should go on until last value in column H starting from H13.
The condition value from I11:T11 can change between TEST and any other values, not further.
Want to achieve this on a button click using a VBA code.
Adding Code, but it limits to the fixed column H values and Fixed row values.
Sub CopyOnCondition1()
Dim sh1 As Worksheet, c As Range
Set sh1 = Worksheets("SheetNameHere") 'change the sheetname
For Each cel In sh1.Range("I11:T11")
If Not cel.Value = "TEST" Then
sh1.Range(Cells(12, cel.Column), Cells(12, 20)).Copy
sh1.Range(Cells(13, cel.Column), Cells(24, 20)).PasteSpecial xlPasteFormulas
End If
Next
For Each cel In sh1.Range("H13:H24")
If cel.Value = "Y" Then sh1.Range("I" & cel.row & ":T" & cel.row).ClearContents
Next
End Sub
enter image description here
As I could understand from the Question I think you are looking for something like this:
Sub CopyOnCondition1()
Dim sh1 As Worksheet, c As Range
Set sh1 = Worksheets("SheetNameHere") 'change the sheetname
For Each cel In sh1.Range("I11:T11")
If Not cel.Value = "TEST" Then
sh1.Range(Cells(12, cel.Column), Cells(12, 20)).Copy
sh1.Range(Cells(13, cel.Column), Cells(24, 20)).PasteSpecial xlPasteFormulas
End If
Next
For Each cel In sh1.Range("H13:H24")
If cel.Value = "Y" Then sh1.Range("I" & cel.row & ":T" & cel.row).ClearContents
Next
End Sub
First It will paste in the complete Range. Then it wo go and check if H have Y, if yes, then it will delete the formula from that row.

If statement with a loop - large data set

Currently I am using the following code to add a formula to cells in the column for a predefined range of cells. The problem is that the number of cells I need the formula in fluctuates based on how big the data set is.
Range("R9").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]<0,""False"",""True"")"
Selection.AutoFill Destination:=Range("R9:R12000")
Range("R9:R62053").Select
What I want to do is for every cell that has a value in say column B, I want the macro to insert the formula in the corresponding cell in column C, and stop once it reaches a point where the cell in column b has no value.
The code below is based off the OP's comments. Where as, his code seems to be targeting R9:R12000"
Dim cell As Range, Target As Range
With Worksheets("Sheet1")
Set Target = .Range("B9", .Range("B" & .Rows.Count).End(xlUp))
For Each cell In Target
If cell.Value <> "" Then cell.Offset(0, -1).Formula = "=IF(RC[-1]<0,""False"",""True"")"
Next
End With

Autofill from cell above if adjacent cell is not empty + match

I'm not that familiar with VBA, so please bear with me.
My question is related to this link: Double-click autofill - dynamic based on adjacent cell
enter image description here
However, the attributes are located in another column (for example Column E). I've tried tweaking the code, but to no avail.
Does this work for you?
Sub FillBlanks()
Dim lr As Long
lr = Cells(Rows.Count, "E").End(xlUp).Row 'Assuming Attributes are in Column E
On Error Resume Next
'Assuming you want to fill blanks in column A
With Range("A1:A" & lr).SpecialCells(xlCellTypeBlanks)
.Formula = "=R[-1]C"
.Value = .Value
End With
End Sub

Resources