I haven't used formulas in Excel before. Currently, the values of my cells are misaligned. What I would like to do is move the values of column F to column G if they are numerical values. For instance, in the 7th row that is displayed in my image, I'd like to move 48 over to the adjacent cell to the right. Same with 3054, 5770, and 32. However, I DON'T want to move IsCallOnly because it is an alphanumerical value. How would I go about doing this? Thanks!
In cell G2, try:
=IF(ISNUMBER(F2),F2,"")
This will tell is to copy the adjacent cell if it is a number, but leave the cell blank if it is not.
I suggest, in H1 and copied down:
=IF(G1="",F1,G1)
this looks to see if G1 is empty and if it is, take F1, otherwise take G1.
However, the number values in ColumnF are copied rather than moved, though it could be arranged for 'move' rather than 'copy, if required, with a bit of fiddling.
Neither requires VBA but if that is obligatory then it would help if you would post the code you have tried so far.
I think you are asking to move only the numbers in column F to column G?, if so maybe the following will help.
Sub MoveMyNumbers()
MoveNumbers "XXX", "F"
End Sub
.
Function MoveNumbers(ShtName As String, ColLetter As String)
Dim ws As Excel.Worksheet
Dim ColNumber As Integer
Dim lRow As Long
Dim i As Long
Set ws = ThisWorkbook.Sheets(ShtName)
lRow = ws.Range(ColLetter & ws.Rows.Count).End(xlUp).Row
'Get Column Number from column letter
ColNumber = ws.Range(ColLetter & "1").Column
For i = 1 To lRow
If IsNumeric(Cells(i, ColNumber).Value) Then
Cells(i, (ColNumber + 1)).Value = Cells(i, ColNumber).Value
Cells(i, ColNumber).Value = " "
End IF
Next i
End Function
Related
I have a function that inserts an array formula into a spreadsheet using R1C1 style notation. When the code inserts the formula into the spreadsheet, all the references get messed up. This confuses me because from what I saw online the .FormulaArray property of a range accepts both A1 and R1C1 style. This is the formula I want to insert:
AddArrayFunction "HUB_START", "=IFERROR(IF(INDEX(PACE!C10,MATCH(1,(PACE!C4=""New Hub"")*(PACE!C9=HubSummaryLTE!RC1),0))="""",""NO_DATE"",INDEX(PACE!C10,MATCH(1,(PACE!C4=""New Hub"")*(PACE!C9=HubSummaryLTE!RC1),0))),""NO_DATE"")", HubWS(Key) ' Make this an array formula
The double quotes are only there because of the way I pass the string to the function that actually inserts the formula down an entire column.
When the code writes to the Range.FormulaArray property, and I then go to the spreadsheet to look at the cell, it gives me this instead:
=IFERROR(IF(INDEX(PACE!R[8]C[-4],MATCH(1,(PACE!R[2]C[-4]="New Hub")*(PACE!R[7]C[-4]=HubSummaryLTE!R[-1]C[464]),0))="","NO_DATE",INDEX(PACE!R[8]C[-4],MATCH(1,(PACE!R[2]C[-4]="New Hub")*(PACE!R[7]C[-4]=HubSummaryLTE!R[-1]C[464]),0))),"NO_DATE")
I don't know why the references are getting messed up and I don't want to use A1 notation, any ideas?
Here's the calling code:
' HUB_START
AddArrayFunction "HUB_START", "=IFERROR(IF(INDEX(PACE!C10,MATCH(1,(PACE!C4=""New Hub"")*(PACE!C9=HubSummaryLTE!RC1),0))="""",""NO_DATE"",INDEX(PACE!C10,MATCH(1,(PACE!C4=""New Hub"")*(PACE!C9=HubSummaryLTE!RC1),0))),""NO_DATE"")", HubWS(Key) ' Make this an array formula
And here is the function definition:
Sub AddArrayFunction(FormulaTitle As String, FormulaText As String, WS As Worksheet, Optional Row As Long = 1, Optional Column As Integer = -1)
Dim LastRow As Long
LastRow = FindLastUsedRow(WS) ' WS.Cells(WS.Rows.Count, 1).End(xlUp).Row
Dim PasteCol As Integer
If Column < 0 Then
PasteCol = 1
Do Until WS.Cells(Row, PasteCol) = ""
PasteCol = PasteCol + 1
Loop
Else
PasteCol = Column
End If
If WS.Cells(Row, PasteCol) <> "" Then WS.Columns(PasteCol).Insert Shift:=xlShiftToRight
WS.Cells(Row, PasteCol) = FormulaTitle
WS.Range(WS.Cells(Row + 1, PasteCol), WS.Cells(LastRow, PasteCol)).FormulaArray = FormulaText
End Sub
The column with the header "HUB_START" has the formula that the code automatically puts in. The column next to it with no header, has the same exact formula, I just copied and pasted, then I did control shift enter just to test and it clearly works, so I don't know where the problem is coming from.
I want to refer to the last cell that contains a text by its address (ex, C800) inside the MIN function. Can you help please?
Sub Set_Formula()
' -----------------------------
Dim lastRow As Long
Dim Lastcell As Range
Dim LC As String
Set Lastcell = Range("C:C").Find("*", Range("C1"), SearchDirection:=xlPrevious)
Set LC = Lastcell.Address()
'find last cell in the row
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim r As Long
For r = 2 To lastRow
If (Cells(r, 3).Value <> "") Then
Range("E8") _
= "=MIN(C2:LC)" 'The MIN function
End If
Next
End Sub
It's not how I would do this, but :
Range("E8") _
= "=MIN(C2:" & LC & ")" 'The MIN function
End If
and, in your code, since LC is a string and not an object, be sure to remove the Set word.
That, of course, puts a static formula into E8.
So, if column C changes (more data added below LC, for example), you'll need to rewrite the formula to the worksheet.
If you prefer a formula that automagically adjusts to changing numbers of rows in column C, you can use:
=MIN(INDEX($C$2:INDEX($C:$C,LOOKUP(9.9E+307,$C:$C,ROW($C:$C))),0,1))
Edit: (Explanation of the formula)
The formula uses a feature of the LOOKUP function to find the last row. When lookup_value is greater than any value in lookup_array, LOOKUP will match the last value in lookup_array. Since we are using the optional result_vector argument, LOOKUP returns the value in the matching position which is that row number.
By using a very large number as lookup_value, one that is close to the maximum number allowed in Excel, we assure that there will not be a larger number in lookup_array
We then use the INDEX function to create an array that begins with C2 (or whatever you put in for a starting cell) and ends and Cn where n is the row number returned by the LOOKUP function.
Use the value for lastRow: Range("E8") = "=MIN(C2:C" & lastRow & ")"
I have been trying to find something that can help me online but no luck. I am trying to compare a value in column A with a value in Cell E1 and if match I want to put an X in column B next to the match in Column A.
here is my code I go so far:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim a As Integer
Dim i As Integer
Dim x As Range
Dim y As Range
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
i = Worksheets("Sheet1").Range("E1")
x = Worksheets("Sheet1").Range("B1:a")
y = Worksheets("Sheet1").Range("A1:a")
'For Each cell In y
'if y = i then
'print "X" in column B next to the value
'MsgBox (i)
End Sub
thanks for your help in advance
Dan
There are a few things here that are worth mentioning. When you want to specify a range using .Range you have to specify the columns on both sides of the : ; furthermore, it takes a string. This means that what you're passing is "B1:a" which doesn't make sense to the computer because it doesn't know you want it to use the value of a instead of the letter. You need to pass "B1:B" & a to the .Range. What this does is concatenate the value you found in the variable a to the string so it appears as one string to the computer.
I personally think it's easier to take all of the values as a column vector instead of dimming the x's as a range because it makes the iteration a little easier. Instead of keeping track of what row I'm on, Counter will always tell me where I am since I'm just moving down a single column. As an added bonus, this reduces the times you access the worksheet which helps speed up your macro.
Although it's commented out, it's worth noting that the loop at the bottom of your sub wouldn't work because you haven't properly closed off the if or the for.
I'm not sure what you intended this for, but it's never a bad idea to use meaningful names so you can look back on your code and figure it out without too much effort. For example, I've renamed your a variable to lastrow which at a glance describes what value it stores.
Below your code that I've altered
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lastrow As Long
Dim Criteria As Long
Dim x() As Variant
Dim Counter As Long
lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Criteria = Worksheets("Sheet1").Range("E1").Value
x = Worksheets("Sheet1").Range("B1:B" & lastrow).value
For Counter = 1 To UBound(x)
If x(Counter,1) = Criteria Then
Worksheets("Sheet1").Cells(Counter, "B").Value = "X"
End If
Next Counter
MsgBox (Criteria)
End Sub
I little bit different approach. This find the last row in column A.
I also included if you want to match by wildcard, i.e. you want to find 45 in 645.
Sub Worksheet_SelectionChange()
Dim lrow As Integer
Dim a As Integer
Dim i As String
Dim Val As String
lrow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row 'Find last row
i = Worksheets("Sheet1").Range("E1") 'Set cell where compare value is
For a = 1 To lrow 'Loop from row 1 to last row in column A
Val = Cells(a, "A").Value 'Set value to compare in Column A
'If Val Like "*" & i & "*" Then 'Use this if you want to find 45 in 645, so wildcard
If Val = i Then 'Exact match
Cells(a, "B").Value = "X" 'Put X in column B
End If
Next a
MsgBox "Match Criteria: " & (i)
End Sub
I have excel cell having multiple rows of data with image url.
Now I want to select all images having 1500 value. So basically I want to select row starting with http and ending 1500.jpg.
Please not that in my single cell values are also other than 1500.jpg.Sample data is given below
colorImages': { 'initial': [{"hiRes":"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UL1500_.jpg","variant":"MAIN","lowRes":null},{"hiRes":"https://images-na.ssl-images-amazon.com/images/I/716mECZ9JDL._UL1500_.jpg","thumb":"https://images-na.ssl-images-amazon.com/images/I/313QD20m4WL._SR38,50_.jpg","large""thumb":"https://images-na.ssl-images-amazon.com/images/I/313QD20m4WL._SR38,50_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/313QD20m4WL.jpg","main":{"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY445_.jpg":[445,117],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY500_.jpg":[500,132],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY550_.jpg":[550,145],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY606_.jpg":[606,160],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY679_.jpg":[679,179],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY741_.jpg":[741,195],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY879_.jpg":[879,231]},
Assuming data is in Column A starting from Cell A2 and all the URLs ending with 1500.jpg needs to be displayed in adjacent columns i.e. same row Column B, Column C, Column D,.... then following might help.
Sub Demo()
Dim ws As Worksheet
Dim lastRow As Long, colIndex As Long
Dim rng As Range, cel
Dim X As Long, DotCount As Long
Dim Pat As String, EndPat As String, Parts() As String
Set ws = ThisWorkbook.Worksheets("Sheet3") 'change Sheet3 to your data sheet
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'get last row with data in Column A
For Each cel In .Range(.Cells(2, 1), .Cells(lastRow, 1)) 'loop through A2 to last cell with data in Column A
colIndex = 1
Pat = "*[!&-;?-[_a-z~=!" & Chr$(1) & "]."
EndPat = "[!&-;?-[_a-z~=!" & Chr$(1) & "]*"
Parts = Split(cel.Value, """") 'split cell value into an array
For X = 0 To UBound(Parts)
If Parts(X) Like "*?.?*" Then
DotCount = Len(Parts(X)) - Len(Replace(Parts(X), ".", ""))
If """" & Replace(Parts(X), "]", Chr$(1)) & """" Like Application.Rept(Pat, DotCount) & EndPat Then
Parts(X) = ""
ElseIf Right(Parts(X), 8) <> "1500.jpg" Then
Parts(X) = ""
Else
cel.Offset(0, colIndex) = Parts(X) 'display URL
colIndex = colIndex + 1
End If
Else
Parts(X) = ""
End If
Next X
Next cel
End With
End Sub
Derived this solution using Function URLs from here.
This is done easily via VBA, but am not expert in that. So I have done some thing for you,just follow the instruction , still it is apply to get only single search entry i.e. means in a cell its find only one 1500.jpg entry.
To get second entry in the same cell you need some effort via change or get substring from the G1 Cell string and repeat the step as explained again.
In A1 Cell, put 1500.jpg
In B1 Cell, put your actual string as you have above
In C1 cell, put formula "=SEARCH(A1,B1)", which find the search 1500 in string
In D1 cell, put formula "=MID(B1,1,C1)", which extract the substring
For E1 we need reverse the string via VBA code - Add Reversestr function (To add this function, see this link)
In F1 cell, put formula "=SEARCH(CHAR(34),E1)", which search " in above reverse string
In G1 cell, put formula "=MID(B1,C1-F1+1,C1)"
Finally you get the string in G1 Cell as "https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._1500.jpg"
For VBa formula, check this links
http://analystcave.com/excel-substring-vba-substring/
I need a macro that put the max number of same transactions in column J.
For exapmle:
You can do so by using a custom function like this one:
Function MaxIf(ByVal rngCol As Range, ByVal rngCol2 As Range, ByVal rngCompare As Range) As Long
MaxIf = 0
For Each c In Intersect(rngCol.Cells, rngCol.Parent.UsedRange).Cells
If c.Value = rngCompare.Value Then
If Cells(c.Row, rngCol2.Column).Value > MaxIf Then MaxIf = Cells(c.Row, rngCol2.Column).Value
End If
Next c
Use =MaxIf(first column, second column, cell) and it should give you the value you've been looking for.
Here are three worksheet formulas that will do what you want.
The first and third formulas are entered normally.
Note that the second formula is surrounded by braces {...}. This is an array formula and you do not type the braces yourself. Rather you enter the formula and then confirm it by holding down ctrl + shift while hitting enter
Formulas
Results
Using formula will be a better approach but if you particularly want VBA solution you could try something like this.
Sub Max_demo()
Dim lastRow As Long
Dim c As Range
lastRow = Cells(Rows.Count, "F").End(xlUp).Row 'gives last row with data in column F
For Each c In Range("J2:J" & lastRow)
c.Value = Evaluate("=MAX(INDEX(($F$2:$F$" & lastRow & "=F" & c.Row & ")*($H$2:$H$" & lastRow & "),0))")
Next c
End Sub