Looping through columns then doing UCase - excel

I have some code that loops through columns to find a specific ending in a column (_END). If it finds that, then it will loop through the rows in that column, changing date formatting. This works as intended and I am having no issues with it. However, I need to UCase the rows as well. Right now, it would output a date like "01-Jan-2016". However, I need it to be "01-JAN-2016". I have code below that is giving me trouble.
lngColHeaders = Cells(5, Columns.Count).End(xlToLeft).Column
For X = 1 To lngColHeaders
If (Right(Cells(5, X), 4)) = "_END" Then
LastRowDates = Cells(Rows.Count, X).End(xlUp).Row
For ZZ = 6 To LastRowDates Step 1
Cells(ZZ, X).NumberFormat = "dd-MMM-YYYY"
UCase (Cells(ZZ, X))
Next ZZ
End If
Next X
Like I said, it seems to format them correctly, but UCase (Cells(ZZ,X)) seems to do nothing. Any help is much appreciated.

I think you'll need to format the cell as text, and then do your UCase, along with a Format.
Cells(ZZ, X).NumberFormat = "#"
Cells(ZZ, X).Value = UCase(Format(Cells(ZZ, X).Value, "dd-MMM-YYYY"))

Related

Excel function doesn't write anything

Goodmorning, i wanted to do a macro in Excel that multiply the element on the right for every element on the left (then put the result in another column), till a blank cell.
This is an example of the elements:
[enter image description here][1]
And this is what i try to write with no result...it seems like the cycle goes well, but it doesn't write anything ... could you please help me out? Anyway, sorry for my bad English, i hope i made it clear.
Thank you.
Sub test()
Range(A1).Select
x = 1
y = 1
Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))
If IsEmpty(Ax) = True Then
y = x + 1
End If
If IsEmpty(Ax) = False Then
Cells(E, x).Value = Cells(A, x).Value * Cells(D, y).Value
End If
x = x + 1
ActiveCell.Offset(1, 0).Select
Loop
End Sub
you have to put the cell references as strings.
VBA will interpret Range(A1) as whatever variable A1 is set to. It is NOT the "A1" cell in your spreadsheet.
Correct is
Range("A1").Select
Similarly if you have a variable x, and want to get the cell reference A1 then you need to do something like this:
x=1
if IsEmpty("A" & x) then ...
Also, the Cells(row,column) function uses row first, then column.
I'm assuming you have "On Error Resume Next" somewhere in your code, as what you have written should throw up a lot of errors.

Why does this loop not carry out all steps each time?

I’ve been writing a macro to format spreadsheets based on client preference. The part of the macro I’m having trouble with is getting percentage columns to format as percentages with one place after the decimal. In the code below, I determine where my % column will be (they’re headed with ‘Cov’ on the file I’m working in), and once that column is identified, the macro loops thru each row in the column and enters the % formula until the last row is reached.
Everything works mostly as intended, except the last two (2) columns that get the % formula do NOT update to show one place after the decimal. Can anyone provide insight to why the last two columns don't update the same as the others that are part of the loop?
Thank you!
For C = 24 To LastColumn + 2
If .Cells(12, C) = "Cov" Then
For i = 13 To LastRow
Set formatCell = .Range(.Cells(i, C), .Cells(i, C))
formatCell.Value = "=IFERROR(" & .Cells(i, C - 1).Address & "/" & .Cells(i, 14).Address & "*100,0)"
formatCell.NumberFormat = "0.0"
Next i
End If
Next C

Excel VBA merge two columns

So basically, I figured out the code to merge column A and B. This might seem silly, but no matter how many times I change the X value or the cell numbers I can't figure out for the life of me how to merge columns K and L.
here's what I did to merge A&B. Basically I want to merge first and last name but they're located in columns K and L. Feel free to tell me if there's an easier way to merge columns using VBA. I'm new to this..
Sub MergeColumns()
Dim myText As String, mySpace As String
mySpace = " "
Cells(1, 1) = "AuthorText"
Cells(1, 2) = ""
x = 2
Do While Cells(x, 1) <> ""
Cells(x, 1) = Cells(x, 1) & mySpace & Cells(x, 2)
Cells(x, 2) = ""
x = x + 1
Loop
End Sub
Cells has two arguments. The first is the row, the second is the column. If you want to combine the text from columns K and L, then use the appropriate numbers.
cells(2,11) will address cell K2.

Create new Excel rows based on column data

Good afternoon all,
I have an issue where I have users who have multiple bank account details. I need to try and create a new row for each employee who has more than one bank account, with the second bank account being allocated a new row.
Employee Number User ID BSB Account number
10000591 WOODSP0 306089,116879 343509,041145273
10000592 THOMSOS0 037125 317166
I need it to look something like this:
Employee Number User ID BSB Account number
10000591 WOODSP0 306089 343509
10000591 WOODSP0 116879 041145273
10000592 THOMSOS0 037125 317166
Any thoughts? Your input is greatly appreciated!
Screenshots are here to demonstrate:
Right click on the tab and choose "View Code"
Paste this code in:
Sub SplitOnAccount()
Dim X As Long, Y As Long, EmpNo As String, UserID As String, BSB As Variant, AccNo As Variant
Range("F1:I1") = Application.Transpose(Application.Transpose(Array(Range("A1:D1"))))
For X = 2 To Range("A" & Rows.Count).End(xlUp).Row
EmpNo = Range("A" & X).Text
UserID = Range("B" & X).Text
BSB = Split(Range("C" & X).Text, ",")
AccNo = Split(Range("D" & X).Text, ",")
For Y = LBound(AccNo) To UBound(AccNo)
Range("F" & Range("F" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = EmpNo
Range("G" & Range("G" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = UserID
Range("H" & Range("H" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = BSB(Y)
Range("I" & Range("I" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = AccNo(Y)
Next
Next
End Sub
Close the window to go back to excel
Press ALT-F8
Choose SplitOnAccount and click run.
Note, this is going to populate the split data to rows F to I, make sure there is nothing in there. If there is post back and we can change it.
Also format columns F - I as text before you run it or Excel will strip leading zeros off as it will interpret it as a number.
Here is another sub that appears to perform what you are looking for.
Sub stack_accounts()
Dim rw As Long, b As Long
Dim vVALs As Variant, vBSBs As Variant, vACTs As Variant
With ActiveSheet '<-define this worksheet properly!
For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
vVALs = .Cells(rw, 1).Resize(1, 4).Value
vBSBs = Split(vVALs(1, 3), Chr(44))
vACTs = Split(vVALs(1, 4), Chr(44))
If UBound(vBSBs) = UBound(vBSBs) Then
For b = UBound(vBSBs) To LBound(vBSBs) Step -1
If b > LBound(vBSBs) Then _
.Rows(rw + 1).Insert
.Cells(rw - (b > LBound(vBSBs)), 1).Resize(1, 4) = vVALs
.Cells(rw - (b > LBound(vBSBs)), 3).Resize(1, 2).NumberFormat = "#"
.Cells(rw - (b > LBound(vBSBs)), 3) = CStr(vBSBs(b))
.Cells(rw - (b > LBound(vBSBs)), 4) = CStr(vACTs(b))
Next b
End If
Next rw
End With
End Sub
I was originally only going to process the rows that had comma delimited values in columns C and D but I thought that processing all of them would allow the macro to set the Text number format and get rid of the Number as text error warnings and keep the leading zero in 041145273.
        
You Can definitely use Power Query to transform the data to generate new rows using split column option.
Check this article it explains the process in detail.
Load Data in Power Query section of excel.
Create an Index (Not required step)
Use Split column function with advance options and split them into new rows.
Save this result into new table for your use.
I did it myself and it worked like a charm.
A formula solution:
Delimiter: Can be a real delimiter or an absolute reference to a cell containing only the delimiter.
HelperCol: I have to use a helper column to make it work. You need to give the column letter.
StartCol: The column letter of the first column containing data.
SplitCol: The column letter of the column to be splitted.
Formula1: Used to generate the formula for the first column not to be splitted. You can fill this formula down and then fill to right.
Formula2: Used to generate the formula for the column to be splitted(only support split one column).
Formula3: Used to generate the formula for the Helper column.
(If the title of the column to be splitted contains the delimiter, you must change the first value of the helper column to 1 manually.)
Formula1:=SUBSTITUTE(SUBSTITUTE("=LOOKUP(ROW(1:1),$J:$J,A:A)&""""","$J:$J","$"&B2&":$"&B2),"A:A",B3&":"&B3)
Formula2:=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("=MID($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,FIND(""艹"",SUBSTITUTE($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,$M$1,"&"""艹"",ROW(A2)-LOOKUP(ROW(A1),$J:$J)))+1,FIND(""艹"",SUBSTITUTE($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,$M$1,""艹"",ROW(A2)-LOOKUP(ROW(A1),$J:$J)+1))-FIND(""艹"",SUBSTITUTE($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,$M$1,""艹"",ROW(A2)-LOOKUP(ROW(A1),$J:$J)))-1)&""""","$M$1",IF(ISERROR(INDIRECT(B1)),""""&B1&"""",B1)),"$J:$J","$"&B2&":$"&B2),"F:F",B4&":"&B4)
Formula3:=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("=SUM(E1,LEN(B1)-LEN(SUBSTITUTE(B1,$H$1,"""")))+1","B1",B4&1),"$H$1",IF(ISERROR(INDIRECT(B1)),""""&B1&"""",B1)),"E1",B2&1)
Helper must filled one row more than the data.
How to use:
Copy the formula generated by the above three formula.
Use Paste Special only paste the value.
Make the formula into effect.
Fill the formula.
Bug:
Numbers will be converted to Text. Of course you can remove the &"" at the end of the formula, but blank cells will be filled with 0.
ps. This method may by very hard to comprehend. But once you master it, it can be very useful to solve relative problems.

Excel - Split apart a list

I have a list of about 300 items, that I need spaced out every 8 cells as opposed to being one after the other. I'm sure there is an easy way to do this, however my brain is failing me. I have a feeling my terminology is hurting hence why I can't find an answer.
=IF(MOD(ROW()+7;8)=0;INDEX(A:A;INT(ROW()/8)+1);"")
Given that data begins at A1 and formula is used from row 1 (coulmn is not important).
Try below code
Sub Main()
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastRow
If i = 1 Then
Cells(i, 5).Value = Cells(i, 1)
Else
Cells((i - 1) * 9, 5).Value = Cells(i, 1)
End If
Next
End Sub
Output
Can you please post a screen shot or add some additional detail?
Two things come to my mind for possible solutions:
1) Use Text to columns (if that is where you're going with this) or
2) Use a formula like =LEFT(A1, 10) in the 8th column and fill down (10 can be changed to whatever the first part of the string is that needs to be separated).
Provide some additional info and I'll take another look!

Resources