Excel VBA merge two columns - excel

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.

Related

How to search a cell for a comma and perform one of two actions based on whether a comma was found or not? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an excel worksheet of about 7,000 rows, each of which has a column with names that can be in various forms ([firstname lastname], [lastname, firstname], etc). I need to take all of those names and separate them into two separate columns, a first name and a last name column.
So what I want to do is write some code that will …
search for a comma in the string
if there is a comma, take all of the characters leading up to the comma and put them in the last name column. Then take all the characters after the comma and put them in the first name column
if there is no comma, just put the first string (first name) in the first name column and the second string (the last name) in the last name column.
It's been a while since I have messed around with visual basic code but I am thinking I can use some sort of find() method for the first step but after that I am completely lost. Any help would be appreciated.
After many edits....
Here is where I am at so far.
Sub WhatsInAName()
Dim N As Long, i As Long, v As String, M As Long, X As Long
N = Cells(Rows.Count, "G").End(xlUp).Row
For i = 2 To N
v = Cells(i, "G").Value
M = InStr(1, v, ",")
X = InStr(1, v, "#")
If M > 0 Then
Cells(i, "H").Value = LTrim(Mid(v, M + 1))
Cells(i, "I").Value = Left(v, M - 1)
ElseIf X > 0 Then
Cells(i, "H").Value = "email"
Else
M = InStr(1, v, " ")
Cells(i, "H").Value = Left(v, M - 1)
Cells(i, "I").Value = Mid(v, M + 1)
End If
Next i
End Sub
Edit #?
Ok so this is what I've essentially ended with. After seeing the data parsed out, it turns out there were a bunch of rows with 3 names or extra jibberish next to their name. For those I will just have to use Text-To-Columns until I figure out how to make my code more advanced to handle those issue. Thanks guys! This was a fun learning experience
Well, not the best solution, but you can do this with normal formulas and then later on paste values.
You posted you have a column with names and you want to separate into 2 columns. I made something like this:
In cell B2 my formula is:
=IF(LEN(A2)-1=LEN(SUBSTITUTE(A2;" ";""));IF(IFERROR(SEARCH(",";A2);0)=0;TRIM(MID(A2;1;SEARCH(" ";A2)-1));TRIM(MID(A2;SEARCH(",";A2)+1;99)));"Manual fix")
In cell C2 my formula is:
=IF(LEN(A2)-1=LEN(SUBSTITUTE(A2;" ";""));IF(IFERROR (SEARCH(",";A2);0)=0;TRIM(MID(A2;SEARCH(" ";A2)+1;99));TRIM(MID(A2;1;SEARCH(",";A2)-1)));"Manual fix")
Some observations:
The formula will return Manual Fix if there are 2 or more spaces in the name (composed names)
The formula only works with 2 cases: case lastname, firstname and firstname lastname. Any other case will cause an error/unexpected result
If there are extra spaces at end or start of names, it will be counted as 2 or more spaces, so the formula will return Manual Fix
As you can see in the image, Michael Jackson and Jackson, Michael is splitted correctly. Jean Claude Van Dame returns Manual Fix due to being a composed name.
Try to adapt this to your needs.
UPDATE: Uploaded a file sample to GDrive in case you want to check the formulas on live. https://drive.google.com/file/d/17Agd57sclLlNuUbhdyaHmQgKUHcRd7hO/view?usp=sharing
Consider:
Sub WhatsInAName()
Dim N As Long, i As Long, v As String, M As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To N
v = Cells(i, "A").Value
M = InStr(1, v, ",")
If M > 0 Then
Cells(i, "B").Value = LTrim(Mid(v, M + 1))
Cells(i, "C").Value = Left(v, M - 1)
Else
M = InStr(1, v, " ")
Cells(i, "B").Value = Left(v, M - 1)
Cells(i, "C").Value = Mid(v, M + 1)
End If
Next i
End Sub
For example:
This assumes that each cell contains either a comma or a space and the parsing is based on the first occurrence of the separator. Also assumes:
input data in column A
no header row (data starts in row #1)

delete cell in Excel

I have a excel table and I want to delete if three column have same data delete second and third. I can write 4. column which datas same or different but I cannot set first 1. 2. or 3. column data
=IF((AND(A1=B1;B1=C1));"Same";"Different")
a b c
a b b
a a a
to
a b c
a b b
a
You can put this into a new module, but the macro will run on the Active Sheet:
Sub DeleteCells()
Dim intResponse, i As Integer
i = 1
intResponse = MsgBox("Are you sure you want " & _
"to delete the cells?", vbYesNo)
If intResponse = vbYes Then
Do While Cells(i, 1).Value <> "" 'does it one by one going down in the first column while the cell is not empty
If (Cells(i, 1) = Cells(i, 2)) And (Cells(i, 1) = Cells(i, 3)) Then
Cells(i, 2).Value = ""
Cells(i, 3).Value = ""
End If
i = i + 1
Loop
End If
End Sub
With a formula is possible but a little more tedious than VBA. For example in E1 copied across to G1 and then E1:G1 copied down to suit:
=IF(COUNTIF($A1:$C1,A1)<>3,A1,IF(COLUMN()=5,A1,""))
Then Copy, Paste Special, Values over the top and delete Columns A:D.
Alternatively flag the rows where the three values are the same (say with =COUNTIF(A1:C1,A1) copied down to suit) then filter that column to select 3 and blank out the values in ColumnsB:C (other than the header row).

Replacing strings in a column sequentially

I am naive to macros. I have data in column A with 500 rows of data. Note some rows are blank in between. The data are image files names like (X0011#00.jpg). I need to rename them sequentially with user input. The user input must be only 400500. The new name must be like (400500_Image-1.jpg). The _Image-1, _Image-2 and so on must be generated automatically in sequence omitting blank rows.
See below how my data is displayed in column A and how I want it in column B.
I appreciate if anyone can provide macro for this.
Col A Col B
X0011#00.jpg 400500_Image-1.jpg
X0021#00.jpg 400500_Image-2.jpg
X0041#00.jpg 400500_Image-3.jpg
X0071#00.jpg 400500_Image-4.jpg
X0051#00.jpg 400500_Image-5.jpg
X0031#00.jpg 400500_Image-6.jpg
X0061#00.jpg 400500_Image-7.jpg
X0091#00.jpg 400500_Image-8.jpg
Thanks
Sub naming()
RowsToProcess = Range("A" & Rows.Count).End(xlUp).Row
j = 1
userInput = InputBox("Give me text")
For i = 1 To RowsToProcess
If Not Cells(i, 1).Value = "" Then
Cells(i, 2).Value = userInput & "_Image-" & j & ".jpg"
j = j + 1
End If
Next
End Sub
This macro creates column B as desired

VBA loop one cell upward until cell not equal "" and a cell incorporate two IF functions

I'm totally new to VBA, am learning it by myself. Here is the problem:
enter image description here
So for Column E
If B = "S" Then
E = "" // null
If B = "F" Then
E = (Reverse Rank x PREVIOUS Adjusted Rank + no of failure + 1)/(Reverse Rank + 1)
or in formula excel format
E5 = ((D5*E4)+($K$1+1))/(D5+1)
So the problem lies on the PREVIOUS adjusted rank in the formula. Say, to get value of E5 it needs value of E4 BUT if E4 = "" so it has to go one cell upward. If one cell upward is also ="", it has to go one cell upward again until is not equal <> "".
The problem is I'm not sure what function is right to use. I think it would be IF and LOOP but I don't understand how to write the condition. Like I said I'm totally new and time constraints cause me anxiety. Also, if you notice, for Column E there are two IFs function I suppose? One is E is depended on Column B.
If Range("B2:B" & lastRow) = "S" or "F" and one is if E="" or <> ""
How I incorporated with that?
I think you can do this without using VBA. You can use the following to get the previous non-blank value in column E:
E5=INDEX($E:$E,MAX(ROW($E$1:$E4)*(LEN($E$1:$E4)>0)))
This formula looks in column E above cell E5 (i.e. E1:E4) and returns the one cell with the largest row value that is also not blank (length > 0).
From there, your whole formula becomes:
E5=IF($B5="S","",IF($B5="F",($D5*INDEX($E:$E,MAX(ROW($E$1:$E4)*(LEN($E$1:$E4)>0)))+($K$1+1))/($D5+1)))
Note that these are array formulas, so they must be validated using Ctrl+Shift+Enter. Also, the top non-blank value in column E has to be input manually or with a different formula.
What's the first value in column E when you meet the first "F" in
column B? I mean, there is nothing in column E, what is the "PREVIOUS Adjusted Rank". In this case, I just set it 0.
What's "no of failure"?
Here is my solution for you and I set "no of failure" as zero.
Sub test()
Dim i, j As Integer
Dim previous As Single
Dim NoOfFailure As Single
For i = 2 To Cells.CurrentRegion.Rows.Count
If Cells(i, 2).Value = "S" Then
Cells(i, 5).Value = ""
ElseIf Cells(i, 2).Value = "F" Then
previous = 0
NoOfFailure = 0
For j = i To 2 Step -1
If Cells(j, 5).Value <> "" Then
previous = Cells(j, 5).Value
Exit For
End If
Next
Cells(i, 5).Value = (Cells(i, 4).Value * previous + NoOfFailure + 1) / (Cells(i, 4).Value + 1)
End If
Next
End Sub

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.

Resources