Replacing Column with an Identical Column Inside a Formula - excel

Is there any way in Excel to 'paste' a formula over a column so that each cell receiving the formula is taken as input in the formula? For example, I would like to write =Text() over a range without first putting Text(A1) over an empty column, dragging it down, and pasting the results into the original column. Is this possible? Thanks!

Here is an example that replaces a column of values with a column of formulas:
Sub Formulate()
Dim N As Long, i As Long, dq As String
N = Cells(Rows.Count, "A").End(xlUp).Row
dq = Chr(34)
For i = 1 To N
v = Cells(i, 1).Value
Cells(i, 1).Formula = "=Text(" & v & "," & dq & "mm/dd/yyyy" & dq & ")"
Next i
End Sub

Related

VBA apply Formula to an entire column

I want to apply an Formula into an entire Row (A)
My code:
Sub row()
Dim i As Long
Dim S1 As String
Dim S2 As String
S1 = "=+LINKS(C"
S2 = ";1)"
i = 2
Range("A1").EntireColumn.Insert
For i = 2 To 90000
Range("A" & i).Value = "=+Left(C "" & i & "" ;1)"
Next i
End Sub
The error is occuring on: Range("A" & i).Value = "=+Left(C "" & i & "" ;1)"
Error code: '1004'
i can't find a way to put the variable "i" into the Formula...
I want to put into the column "A" the Formula, which lets the Cells in "A" look into the Cell "C" of each individual row and take the first Number and write it in itself.
Thank you in advance.

'Range' of object'_Global' fail in Do Until

Basically I just found this little code on the web, I thought it might help me because I want to improve it. But on the
Do Until Range("A" & amp, R) = ""
line I got the mentioned error in the title.
Here is the code:
Sub Use_Instr()
R = 1
'loop to the last row
Do Until Range("A" & amp, R) = ""
'check each cell if contains 'apple' then..
'..place 'Contains Apple' on column B
If Range("A" & amp, R) Like "*apple*" Then
Range("B" & amp, R) = "Contains Apple"
End If
R = R + 1
Loop
End Sub
It does search the "apple" term in the sentences in A column and write "contains apple" in the B
column if it contains "apple"
Try this instead:
R = 1
'loop to the last row
Do Until Range("A" & R).Value = ""
'check each cell if contains 'apple' then..
'..place 'Contains Apple' on column B
If Range("A" & R).Value Like "*apple*" Then
Range("B" & R).Value = "Contains Apple"
End If
R = R + 1
Loop
Range("A" & amp, R) is not the right way to address the range. When you copied from the website, it copied the html encoding as well. In Html & is encoded as &. Simply replace & amp, with & in your code. So your code becomes
R = 1
'loop to the last row
Do Until Range("A" & R) = ""
'check each cell if contains 'apple' then..
'..place 'Contains Apple' on column B
If Range("A" & R) Like "*apple*" Then
Range("B" & R) = "Contains Apple"
End If
R = R + 1
Loop
Also to make the code case insensitive, as suggested by #VBasic2008 in comments below, you may want to change Range("A" & R) Like "*apple*" to If LCase(Range("A" & R).Value2) Like "*apple*" Then.
Having said that, I would use a different approach than using a loop which is a slightly slower approach.
It does search the "apple" term in the sentences in A column and write "contains apple" in the B column if it contains "apple"
If you were to do this in Excel, then you would use the formula =IF(ISNUMBER(SEARCH("apple",A1)),"Contains Apple","")
So what we will do is find the last row in Column B and then add this formula in the entire range in one go! Finally we will convert the formula to values.
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim sFormula As String
'~~> Change this to the relevant sheet
Set ws = Sheet1
'~~> This is your formula
' =IF(ISNUMBER(SEARCH("apple",A1)),"Contains Apple","")
sFormula = "=IF(ISNUMBER(SEARCH(""apple"",A1)),""Contains Apple"","""")"
With ws
'~~> Find the last row in column B
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Insert the formula in the entire range in 1 go
With .Range("B1:B" & lRow)
.Formula = sFormula
'~~> Convert formula to value
.Value = .Value
End With
End With
End Sub

Multiple Row Concatenate Macro in Excel VBA

I have a spreadsheet of x amount of names, with the last name being in column A, and the first name in column B. I am trying to concatenate them to get the First(B) and Last(A) name in the same Column for the entire list of names. Was wondering if someone has or could write a macro for this so it will read in one column ("lastname", "firstname") x amount of times. Thanks!
Give this a try:
Sub NameGenerator()
Dim N As Long, DQ As String
N = Cells(Rows.Count, 1).End(xlUp).Row
DQ = Chr(34)
For i = 1 To N
Cells(i, 3) = "(" & DQ & Cells(i, 2) & ", " & Cells(i, 1) & DQ & ")"
Next i
End Sub

Excel trick for this task

I've got a spreadsheet in excel with this rows:
COLUMN
Value1.Value2.Value3
Value4.Value5.Value6
Value7.Value8.Value9
In another spreadsheet I've got a simple list with names:
COLUMN
Name1
Name2
Name3
And,of course, this list is huge :).
So need to have the following spreasdsheet at the end:
COLUMN
Value1.Name1.Value2.Value3
Value4.Name1.Value5.Value6
Value7.Name1.Value8.Value9
Value1.Name2.Value2.Value3
Value4.Name2.Value5.Value6
Value7.Name2.Value8.Value9
Value1.Name3.Value2.Value3
Value4.Name4.Value5.Value6
Value7.Name4.Value8.Value9
I have to concatenate the names on the list with all the values on spreadsheet replicating them for ALL the names.
Is there a way of doing this process automatically? The manual process would take hours to be done and I think there's a smarter way of doing that although I don't know it! :)
Thanks in advance for your help.
And it is a good challenge to do it with formulas: :)
With this array formula in D1 and then copy down
=INDEX(LEFT($A$1:$A$4;FIND(".";$A$1:$A$4))&TRANSPOSE($C$1:$C$3)&RIGHT($A$1:$A$4;LEN($A$1:$A$4)-FIND(".";$A$1:$A$4)+1);1+INT((ROWS($D$1:D1)-1)/ROWS($C$1:$C$3));1+MOD(ROWS($D$1:D1)-1;ROWS($C$1:$C$3)))
Depending on your regional settings you may need to replace field separator ";" by ","
There is always a "." between the values.
Try this code. Using arrays would be much faster for huge list of names/values:
Sub test()
Dim arrVal As Variant
Dim arrNames As Variant
Dim arrRes As Variant
Dim v, n, k As Long
'change Sheet1 to suit
With ThisWorkbook.Worksheets("Sheet1")
'change A1:A3 to values address
arrVal = .Range("A1:A3")
'change B1:B3 to names address
arrNames = .Range("B1:B3")
ReDim arrRes(1 To UBound(arrVal) * UBound(arrNames), 1 To 1)
k = 1
For Each v In arrVal
For Each n In arrNames
arrRes(k, 1) = Left(v, InStr(1, v, ".")) & n & Mid(v, InStr(1, v, "."))
k = k + 1
Next
Next v
'change "c1" to start cell where to put new values
.Range("C1").Resize(UBound(arrRes, 1)) = arrRes
End With
End Sub
Note:
If you don't know exact addresses of "values" and "name" ranges, change this part
'change A1:A3 to values address
arrVal = .Range("A1:A3")
'change B1:B3 to names address
arrNames = .Range("B1:B3")
to
'change A1:A to "values" address
arrVal = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
'change B1:B to "names" address
arrNames = .Range("B1:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
In that case "values" and "name" ranges starts from A1 and B1 accordingly and ends in the last non empty row in coumns A and B accordingly.
Result:
I think that could work.
Const FIRST_TALBE = 4
Const SECOND_TABLE = 2
Sub makeTheJob()
For i = 1 To lastRow
l = Split(Cells(i, FIRST_TABLE), ".")
newvalue = l(0) & "." & Cells(i, SECOND_TABLE) & "." & l(1) & "." & l(2)
Debug.Print newvalue
Next i
End Sub

Excel Visual Basic Change Cell to hyperlink by adding portion of URL

In Column B I have partial links such as B003KIU14O
What I'm trying to achieve is to have it converted to hyperlink and stayed in the same Column B.
My static (first part of link) link is http://www.amazon.com/gp/product/
The complete link looks like this: http://www.amazon.com/gp/product/B003KIU14O
If possible I would love to have the Visual value in Column B to display B003KIU14O and have hyperlink
light the cells you wish to convert and run this tiny macro:
Sub dural()
Dim r As Range, s As String, DQ As String
DQ = Chr(34)
s = "http://www.amazon.com/gp/product/"
For Each r In Selection
v = r.Value
r.Formula = "=HYPERLINK(" & DQ & s & v & DQ & "," & DQ & v & DQ & ")"
Next r
End Sub
EDIT#1:
To avoid manual selection:
Sub dural()
Dim r As Range, s As String, DQ As String
DQ = Chr(34)
Dim rBig As Range
s = "http://www.amazon.com/gp/product/"
Dim N As Long
N = Cells(Rows.Count, "B").End(xlUp).Row
Set rBig = Range("B1:B" & N)
For Each r In rBig
v = r.Value
r.Formula = "=HYPERLINK(" & DQ & s & v & DQ & "," & DQ & v & DQ & ")"
Next r
End Sub
I don't think you need Visual Basic, if you don't mind the hyperlink appearing in an adjacent cell: Put the following in the cell where you want the hyperlink to appear (assuming your link appears in cell B1):
=HYPERLINK("http://www.amazon.com/gp/product/" & B1, B1)
Then copy down as necessary for other values in column B.
If you want the full hyperlink to appear, just drop the second parameter:
=HYPERLINK("http://www.amazon.com/gp/product/" & B1)

Resources