how to create macro to get multiple value? [closed] - excel

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 8 years ago.
Improve this question
i am beginner in excel vba i have huge list where the first name and last name in one column without any space or symbol and some name are in capital alphabet and some have no capital alphabet and they are the same column what would be the popossible macro to make space batween them...or any possible way....
thanks in advance....

Not reliable but will get you there with simple names. Select the range of names and run the macro.
Sub InsertSpacesInNames()
Dim pos As Long: pos = 0
Dim uc As Long: uc = 0
For Each cell In Selection
For i = 1 To Len(cell.Value)
If Asc((Mid(cell.Value, i, 1))) >= 65 And Asc((Mid(cell.Value, i, 1))) <= 90 Then
uc = uc + 1
pos = i
End If
Next i
If uc = 2 Then
cell.Value = Mid(cell.Value, 1, pos - 1) & " " & Mid(cell.Value, pos, Len(cell.Value))
End If
uc = 0
pos = 0
Next
End Sub
My output:
This macro will work fine if and only if:
you use it with simple names (no compound names like Jean-PierreLeCosteau which's desired output is Jean-Pierre LeCosteau)
the full name has two capital letters only.

Assuming you know the first name then simple example using formulas:
A B
1 AmandaWinslet = "Amanda" & " " & RIGHT(A1, LEN(A1) - LEN("Amanda"))) // result is Amanda Winslet
The VBA way of doing this would be:
Sub AddSpace()
Range("A1") = "Amanda" & " " & VBA.Right$(Range("A1"), Len(Range("A1")) - Len("Amanda"))
End Sub
You will need to loop over the list in VBA and make FirstName a variable. Your post assumes you have the first name from somewhere

Related

Copy a range of cells in a row and paste same row/range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
Thats my data. I want to copy B2:M9 and insert it to P2. Furher, I want to copy B13:M20 and paste it to P2. Then I want to copy B24:M31 and paste it to P2 and so on.
Does anyone have an idea how I can do it?
Thank you!
Sub copypasta()
Dim nb_iter as Integer 'Number of ranges you have to copy
nb_iter = 3 'for example
Dim step as Integer
step = 0
for k = 1 to nb_iter
Range("B" & step + 2, "M" & step + 9).Copy
Range("P2").PasteSpecial , Paste:=xlPasteValues
step = step + 11 'you are adding 11 to both row numbers for each iteration
Next k
End Sub
My first attempt:
Sub Copypasta()
Dim i As Long
For i = 0 To 31
Range("B" & 2 + i, "M" & 9 + i).Copy
Range("P2").PasteSpecial Paste:=xlPasteValues
Next i
End Sub

VBA code that adds responsive Shapes to the current macro [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 2 years ago.
Improve this question
I'm new at coding but I've been asked to create some macro using VBA to speed up our working process in my company.
i have this code that performs a comparison macro:
Sub Compare_numbers()
Dim i As Long, LastRow As Long
Dim L, M, txt As String
LastRow = Cells(Rows.Count, "L").End(xlUp).Row
For i = 2 To LastRow
L = Cells(i, "L").Value
M = Cells(i, "M").Value
If L = M Then
txt = "they are equal"
ElseIf L > M Then
txt = "L is greater than M " & _
ChrW(&H2B61)
Else
txt = "L is less than M " & _
ChrW(&H2B63)
End If
Cells(i, "N") = txt
Next i
End Sub
this is the output of the code:
I need to add to this macro another piece of code that would replace all the arrow characters with a proper Shape from "Insert" tab (and also it should be responsive to the changing of cell's size) like the one in this sample picture:
Any idea to do this?
If you use a helper column N with the formula
=IFERROR((L:L-M:M)/ABS(L:L-M:M),0)
it will show
-1 if L < M
0 if L = M
1 if L > M
You can then easily use conditional formatting to show the arrows:
You can even hide the values if necessary so only the arrows are shown in N (just edit the rule accordingly).
I highly recommend not to use shapes for that as this will become very cumbersome, can easily fail and will put a heavy load on your sheet if the arrows have to change with the values automatically.
Also you would need to ensure that the shapes cannot be moved accidentally, because this would "destroy" the entire sheet.

Macro VBA Excel sum by Group [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 know in Excel we can use pivot table, but I just want to make a button to sum a group by Vendor, this is the illustrate of my table in Excel:
I just want to sum Amount grouping by Vendor in new column and add 1 column to proof that column has a value bigger than 2,550,000.
Like this result when I hit a button "RUN":
I need using macro because we have dynamic data from pivot table (SSAS) and always change everyday, so I have to make sure the new column in this excel to provide the data
Following formula can be used to sum the D column group by A column.
=IF(A2=A1,"",SUMIF(A:A,A2,D:D))
Then you can put the condition in F column.
=IF(ISNUMBER(D2),IF(D2>255000,TRUE,FALSE),"")
And you can use the following VBA code to write the above formula:
Private Sub Test()
Dim i As Integer
Dim Condition As Variant
Dim AVal As Variant
For i = 1 To 10 'replace 10 with last row count
Condition = "A" & i & "=A" & i - 1
AVal = "A" & i
Worksheets("Sheet1").Range("E" & i).Formula = "=IF(" & Condition & ","""",SUMIF(A:A," & AVal & ",D:D))"
DVal = "D" & i
Worksheets("Sheet1").Range("F" & i).Formula = "=IF(ISNUMBER(" & DVal & "),IF(" & DVal & ">255000,TRUE,FALSE),"""")"
Next i
End Sub

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)

VBA Find a Row with two variables then time stamp a column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could anyone give me advice on how to look up a row in a spreadsheet which matches two criteria, then change the data in specific Cells.
The data will be like this..
Reference Version date1 date2 date3
ABC1 1 11/12/2013
ABC1 2 31/12/2013
ABC2 1 12/12/2013
ABC3 1 12/12/2013
ABC1 3 01/01/2014
In VBA I wish to be able to find the row that matches the reference and the version number, then datestamp column 4 of that 1 unique row.
Any help would be really appreciated.
Many Thanks
Solving for a position for two criteria can be solved by this array formula (in this sample looking up ABC3 and 1 in A2:B6
This formula can be used in VBA to provide the position for the timestamp:
VBA Equivalent
Sub OneWay()
Dim lngRow As Long
lngRow = Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")
Cells(lngRow + 1, 4) = Now()
End Sub
or just:
Sub OneWay2()
lngRow = Cells(Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")+ 1, 4) = Now()
End Sub
Try this code:
Sub test()
Dim refToFind As String
Dim versToFind As String
refToFind = "ABC1"
versToFind = "1"
'address of first reference
Set Rng = Sheet1.Range("B2")
lastrow = Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row - Rng.Row
For i = 0 To lastrow
If CStr(Rng.Offset(i, 0).Value) = refToFind _
And CStr(Rng.Offset(i, 1).Value) = versToFind Then
Rng.Offset(i, 4) = Now
End If
Next i
End Sub

Resources