Concatenation error with equal sign VBA excel - vba

I have this code where ttlMtrs is an integer < 7000
diffFormula = "=1-(" & ttlMtrs & "/7000)*1200"
Debug.Print diffForumla
I am trying to build a string that will evaluate once printed to a cell in excel.
When I Debug.print to check the string I only get blank lines. One time I got long string of garbage characters.
I am trying to figure out what I am misunderstanding in building this string. Any ideas are appreciated.

you have mispelled the variable name in the debug statement
Debug.Print diffForumla
should be
Debug.Print diffFormula
note the changed position of the u

Public Sub Test()
Dim ttlMtrs As String
Dim diffFormula As String
diffFormula = "=1-(" & ttlMtrs & "/7000)*1200)"
Debug.Print diffFormula
End Sub
This should be the result:
=1-(/7000)*1200)

Related

Formula error "The formula you typed contains an error"

I use excel 2010.
I have the below formula that returns me an error
The formula you typed contains an error
. Is there any way to resolve this? Thanks.
=loopThroughFilesCount2("C:\Users\yalinbah\Desktop\20180131_120549\New folder\ayakkabı\","A2")
Here is my VBA formula
Function loopThroughFilesCount2(dirFolder As String, strToFind As String) As Double
Dim filePath As Variant
filePath = Dir(dirFolder)
While (filePath <> "")
If InStr(filePath, strToFind) > 0 Then
filesCount = filesCount + 1
End If
filePath = Dir
Wend
loopThroughFilesCount2 = filesCount
End Function
Problem is solved. Correct formula is below.
=loopThroughFilesCount2("C:\Users\yalinbah\Desktop\20180131_120549\New folder\ayakkabı\";A2)
Problem is solved. Correct formula is below.
=loopThroughFilesCount2("C:\Users\yalinbah\Desktop\20180131_120549\New folder\ayakkabı\";A2)

reverse some text in cell selected - VBA

I'm rooky for VBA. I have some problem about reversing my data on VBA-Excel. My data is "3>8 , 6>15 , 26>41 (each data on difference cells)" that i could reverse "3>8" to "8>3" follow my requirement by using function reverse. But i couldn't reverse "6>15" and "26>41" to "15>6" and "41>26". It will be "51>6" and "14>62" that failure, I want to be "15>6" and "41>26".
Reverse = StrReverse(Trim(str))
Help me for solve my issue please and thank for comment.
You first need to find the position of the ">" in the cell. you do this by taking the contents of the cell and treating it as a String and finding the ">"
This is done in the line beginning arrowPosition. This is the integer value of the position of the ">" in you original string
Next use Left to extract the text up to the ">" and Right to extract the text after the ">"
Then build a new String of rightstr & ">" & leftStr.
Note I input my data from Sheet1 B5 but you can just use any source as long as it is a String in the correct format.
Sub Test()
Dim myString As String
myString = Sheets("Sheet1").Range("B5")
Debug.Print myString
Debug.Print reverseString(myString)
End Sub
Function reverseString(inputString As String) As String
Dim leftStr As String
Dim rightStr As String
Dim arrowPosition As Integer
arrowPosition = InStr(1, inputString, ">")
leftStr = Left(inputString, arrowPosition - 1)
rightStr = Right(inputString, Len(inputString) - arrowPosition)
reverseString = rightStr & ">" & leftStr
End Function
just because you look for a VBA, you can add this function into your code:
Function rev(t As String) As String
s = Split(t, ">", 2)
rev = s(1) & ">" & s(0)
End Function
of course only if you have to reverse 2 number, otherwise you'll loop the "s", but the function would lose its usefulness

How can I convert a double to a Hex string?

I'm working on editing a project in visual basic, and don't have a whole lot of experience with vb.
I have a text box where a user can enter a number. The number should be stored as a double. Then, I need to convert the number to it's equivalent 16-byte hexadecimal representation. Any tips on how to do this?
I have just had this kind of problem. My idea was to take the time from the function Now() and to split it by the comma. Then to convert it to Hex. After some time of researching, I came up with the idea that the input should be rounded. to 8, otherwise we may get an overflow error. This is my code:
Public Function codify_time() As String
If [set_in_production] Then On Error GoTo codify_Error
Dim dbl_01 As Variant
Dim dbl_02 As Variant
Dim dbl_now As Double
dbl_now = Round(Now(), 8)
dbl_01 = Split(CStr(dbl_now), ",")(0)
dbl_02 = Split(CStr(dbl_now), ",")(1)
codify_time = Hex(dbl_01) & "_" & Hex(dbl_02)
On Error GoTo 0
Exit Function
codify_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure codify of Function TDD_Export"
End Function

string in an array not appearing as entered

So i am trying to create a simple string, the code is pretty simple
Sub Macro1()
Dim yr As Variant
Dim site As Variant
yr = Array("2013", "2014")
site = Array("SHR", "SHW", "SHH")
q = 0
a = 0
Range("D3").Formula = "\\shared\Export\STA30\" & site(q) & "\" & Year(a) & "\XL\"
End Sub
pretty basic, and it works, but the output is:
\\shared\Export\STA30\SHR\1899\XL\
I'm presuming something has happened to my excel settings? I have used arrays like this in the past and have never encountered this problem. Does anyone know why this might be happening?
You have to use yr(a) insted of Year(a).
Year(int) return the converted int into year.
You have declared an array as yr and when you are printing in the output string you are using year(a).
Just change the year(a) to yr(a) and you would get the desired output.

for loop : string & number without keep adding &

I'm learning for loop and I cannot get this problem fixed.
The problems are in the following codes.
dim rt as integer = 2
dim i As Integer = 0
dim currentpg as string = "http://homepg.com/"
For i = 0 To rt
currentpg = currentpg & "?pg=" & i
messagebox.show(currentpg)
next
'I hoped to get the following results
http://homepg.com/?pg=0
http://homepg.com/?pg=1
http://homepg.com/?pg=2
'but instead I'm getting this
http://homepg.com/?pg=0
http://homepg.com/?pg=0?pg=0
http://homepg.com/?pg=0?pg=0?pg=0
Please help me
Thank you.
You probably need something like this:
Dim basepg as string = "http://homepg.com/"
For i = 0 To rt
Dim currentpg As String = basepg & "?pg=" & i
messagebox.show(currentpg)
Next
Although a proper approach would be to accumulate results into a List(Of String), and then display in a messagebox once (or a textbox/file, if too many results). You don't want to bug user for every URL (what if there are 100 of them?). They would get tired of clicking OK.
First of all, you went wrong while copying the output of the buggy code. Here is the real one.
http://homepg.com/?pg=0
http://homepg.com/?pg=0?pg=1
http://homepg.com/?pg=0?pg=1?pg=2
It does not work because currentpg should be a constant but it is changed on each iteration.
Do not set, just get.
MessageBox.Show(currentpg & "?pg=" & i)
Or you can use another variable to make it more readable.
Dim newpg As String = currentpg & "?pg=" & i
MessageBox.Show(newpg)
Also, your code is inefficient. I suggest you to change it like this.
Dim iterations As Integer = 2
Dim prefix As String = "http://homepg.com/?pg="
For index As Integer = 0 To iterations
MessageBox.Show(prefix & index)
Next