What I have:
Range("A" & i+1).value=Range("B" & i).value
What I Need:
"'" & Range("A" & i+1).value=Range("B" & i).value
The output I Need:
'1234
How do I add that type of text using VBA I'm trying to implement with other functions but it is giving me an error
You've got the order mixed up:
Range("A" & i+1).value = "'" & Range("B" & i).value
If you need to format given cell as a text, then this is a working solution:
Sub TestMe()
Range("A1").NumberFormat = "#"
Range("A1") = 1234
End Sub
Related
I need to create a formula that is going to concatenate 3 different cells into one date. The formula is going to be a part of a loop function so I need the cell reference to change as the loop function runs.
I am having trouble with the syntax such as the "&" and the """" that are necessary to distinguish parts of the formula from the cell references.
For now I am just trying to get the formula to paste into a single cell without the loop. The 3 cells that I am combining are in columns: N,O & P. I am trying to paste the formula into column M.
I tried creating the formula on a separate "Data" tab and then simply copy and pasting it into each cell using VBA but the row number does not update according to the row that the formula is pasted.
I tried rearranging the & and "" for a while and could not figure out the winning combination.
FormulaRow = Cells(Rows.Count, "M").End(xlUp).Offset(1).Row
M_Formula = "=N" & FormulaRow & "" / "" & "O" & FormulaRow & "" / "" & "P" & FormulaRow
Range("M" & FormulaRow).Value = M_Formula
I am expecting to get the following result: =N5&"/"&O5&"/"&P5 with the row number corresponding to the row that the formula is pasted.
When I tried the copy and paste method I got this message: "Object Doesn't Support this Property or Method"
Any help would be appreciated. Thank you!
Maybe:
Sub sub1()
' If you have 12 in N2 and 34 in O2 and 5678 in P2:
Dim FormulaRow&, M_Formula$
FormulaRow = 2
M_Formula = "=N" & FormulaRow & "&" & """" & "/" & """" & "&" & _
"O" & FormulaRow & "&" & """" & "/" & """" & "&" & _
"P" & FormulaRow
Cells(FormulaRow, ColNum("M")) = M_Formula ' gives the formula you want 12/34/5678
Cells(FormulaRow, 13) = M_Formula ' also gives the formula you want 12/34/5678
End Sub
Function ColNum&(col$)
ColNum = Range(col & 1).Column
End Function
Excel is smart enough to increment the row reference when you do a range in one go:
Range("M5:M" & Range("N" & rows.count).end(xlup).row).formula = "=N5 & ""/"" & O5 & ""/"" & P5"
That will do what you want in 1 line.
Then you can copy the result, paste as values then format to date with something like this:
Sub EnterDate()
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = "=N5 & ""/"" & O5 & ""/"" & P5"
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Copy
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValues
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).NumberFormat = "DD/MM/YYYY"
'Force a reevaluate to make it see actual dates
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Value
End Sub
Using this method I just tested on over 12,000 rows and it took under a second.
With regards to the comments about using the date function, using the date function is a much better method, I wanted to show you how to do it using your own method but you can get rid of the formatting code if you use Date like so:
Sub EnterDate()
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = "=DATE(P5,O5,N5)"
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Copy
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValues
End Sub
If you want to leave the formulas then simply delete the last 2 lines in there.
I'm stuck with a problem and can't find a solution. I want to concatenate cells with two different formats. One cells has the format "tt:mm" and the other cell has format "general" how would I concatenate the cells? I have seen online that this can be done in Exel with something like
=A1 & TEXT(B1,"tt:mm")
or
=CONCATENATE(TEXT(A1;"tt:mm"); " ";B1)
But how is this done in VBA via a loop?
I have tried to just add the cells together but I the date is not returned in the format that I want.
Sub Merge()
Dim i As Long
Dim time As String
LastRow = Range("A" & StartRow).End(xlDown).Row
For i = StartRow To LastRow
Range("B" & i).Value = Range("B" & i).Value & " " & Range("A" & i).Value
Range("B" & i).NumberFormat = "tt:mm General"
End Sub
So if I run this code for xyz and 02:30 then I get 0,15625 xyz but I want to get 02:30 xyz.
Many thanks for any help.
Make this line:
Range("B" & i).Value = Range("B" & i).Value & " " & Range("A" & i).Value
Into:
Range("B" & i).Value = format(Range("B" & i).Value, "hh:mm") & " " & Range("A" & i).Value
This link has the different format strings: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/format-function-visual-basic-for-applications
I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"
I have a whole bunch of data on a whole bunch of sheets and for each of them, I want to find the count of the unique values in a given column.
When I use the following command in a sheeet, it works perfectly
=SUM(IF(FREQUENCY(MATCH(REST!D2:D2225,REST!D2:D2225,0),MATCH(REST!D2:D2225,REST!D2:D2225,0))>0,1))
But when I use an equivalent command inside VBA, I am getting errors
range1 = Cell(2,j).Address & ":" & Cells(k,j).Address
= Application.WorksheetFunction.SUM( Application.WorksheetFunction.IF( Application.WorksheetFunction.FREQUENCY( Application.WorksheetFunction.MATCH(Range(range1),Range(range1),0), Application.WorksheetFunction.MATCH(Range(range1),Range(range1),0))>0,1))
I have tried other combinations, like using
Application.Match
and
Application.Frequency
I've also got "type mismatch errors.
On the other hand, the following function works perfectly
Application.Worksheetfunction.Sum(Range(range2))
The big difference between range1 and range2 is that range2 data is strictly numeric while range1 data is both numeric and string.
EDIT: TO implement BX201's solution
range_TradeID_total_FL = .Cells(2, TradeId_column).Address & ":" & Cells(Finalrow, TradeId_column).Address
doubleQ = Chr(34) & Chr(34)
fStr = "=SUMPRODUCT((range_TradeID_total_FL & " <> " & " & doubleQ & ")/COUNTIF(range_TradeID_total_FL , range_TradeID_total_FL & " & " & " & doubleQ & "))"
var_TOTAL_TradeId_count_FL = Evaluate(fStr)
MsgBox var_TOTAL_TradeId_count_FL
I don't want to store the values in a cell but store it in a variable. But when I do that, the MsgBox gives me the value "True" instead of a number.
#SiddharthRout I am not aware of evaluate. Can you please tell me how it would work in my context, especially when I use variables for range.
Try this (UNTESTED)
This is an example where row is a variable.
Sub Sample()
Dim r1 As Long, r2 As Long
Dim formulaString As String
r1 = 2
r2 = 2225
'=SUM(IF(FREQUENCY(MATCH(REST!D2:D2225,REST!D2:D2225,0),MATCH(REST!D2:D2225,REST!D2:D2225,0))>0,1))
formulaString = "=SUM(IF(FREQUENCY(MATCH(REST!D" & r1 & _
":D" & r2 & _
",REST!D" & r1 & _
":D" & r2 & _
",0),MATCH(REST!D" & r1 & _
":D" & r2 & _
",REST!D" & r1 & _
":D" & r2 & _
",0))>0,1))"
Debug.Print Application.Evaluate(formulaString)
End Sub
A much simpler formula for getting count of unique values in a column is:
=SUMPRODUCT((REST!$D$2:$D$2225<>"")/COUNTIF(REST!$D$2:$D$2225,REST!$D$2:$D$2225&""))
Normal formula so no need to CSE. This works best if there are no conditions attached to your values, like maybe a simple list of names or values.
I think evaluating this should give you the result you want. You can also assign it as a formula to a cell. Either of the following two approaches works, in that regard.
Insert formula into cell
Sub UniqueCount1()
doubleQ = Chr(34) & Chr(34)
fStr = "=SUMPRODUCT((REST!D2:D2225<>" & doubleQ & ")/COUNTIF(REST!D2:D2225,REST!D2:D2225&" & doubleQ & "))"
Range("C1").Formula = fStr
End Sub
Evaluate the formula and insert result into cell
Sub UniqueCount2()
doubleQ = Chr(34) & Chr(34)
fStr = "=SUMPRODUCT((REST!D2:D2225<>" & doubleQ & ")/COUNTIF(REST!D2:D2225,REST!D2:D2225&" & doubleQ & "))"
Range("C1").Value = Evaluate(fStr)
End Sub
A third one approach is to use a scripting dictionary. This is a bit more complicated, but it's pretty fast and can be used in a myriad of ways.
Sub UniqueDict()
Dim oDict As Object
Dim sElem As Variant, sList As Variant
sList = ThisWorkbook.Sheets("REST").Range("D2:D2225").Value
Set oDict = CreateObject("Scripting.Dictionary")
With oDict
For Each sElem in sList
If Not .Exists(sElem) And Not IsEmpty(sElem) Then
.Add sElem, Empty
End If
Next sElem
End With
MsgBox oDict.Count
End Sub
Hope this helps.
EDIT:
Here's an approach using your variables.
startRow = 2 'Or whatever it is based on your other code.
finalRow = 2225 'Or whatever it is based on your other code.
rngStr = "REST!$D$" & Startrow & ":$D$" & Finalrow '$D$2:$D$2225
dQ = Chr(34) & Chr(34) 'Double quote string.
fStr1 = "=SUMPRODUCT((" & rngStr & "<>" & dQ & ")" '=SUMPRODUCT((REST!$D$2:$D$2225<>"")
fStr2 = "/COUNTIF(" & rngStr & "," & rngStr & "&" & dQ & "))" '/COUNTIF($D$2:$D$2225,$D$2:$D$2225&""))
fStr = fStr1 & fStr2 '=SUMPRODUCT((REST!$D$2:$D$2225<>"")/COUNTIF($D$2:$D$2225,$D$2:$D$2225&""))
var_Total = Application.Evaluate(fStr)
MsgBox var_Total
Hope this helps.
I really appreciate if anyone could help; I've been working on this for a while...
I just want to define the formula of countif in a cell, here is the code:
Range("E" & PLrowstart).Formula = "= CountIf($B$PLrowstart:$B$PLrowend" & ",B2)"
PLrowstart and PLrowend are integer variables I set before the line. The range for count if is range("B" & PLrowstart & ":B" & PLrowend). I've also tried other ways, none worked...
TIA.
Range("E" & PLrowstart).Formula = "= CountIf($B$" & PLrowstart & ":$B$" & PLrowend & ",B2)"
Try this
Sub SetFormula()
PLrowstart = 2
PLrowend = 4
Range("E" & PLrowstart).Formula = "=CountIf($B$" & PLrowstart & ":$B$" & PLrowend & ",B2)"
End Sub