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 7 years ago.
Improve this question
I'm trying the following code to find the location of several values:
Dim destino as String
Dim origen as String
destino="A"
origen="B"
Dim fila as Integer
fila = Application.WorksheetFunction.Match(1, (Range("F:F") = origen) * (Range("G:G") = destino), 0)
But it doesn't work. Does anybody know why?
Sub Tester()
Dim f As Integer
Dim destino as String
Dim origen as String
destino = "B"
origen = "R"
f = ActiveSheet.Evaluate("=MATCH(1,(C:C=""" & destino & """) * (D:D=""" & origen & """),0)")
Debug.Print f '>> 6
End Sub
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
For an example if column value is "ABC 123 981" need to extract only 123... like so if its "456_wert" need to extract only 456 using access VBA code. Can somebody please help on this.
Parse First Consecutive Digits
Sub StrFirstDigitsTEST()
Const pString As String = "a123.456b"
Dim rString As String: rString = StrFirstDigits(pString)
Debug.Print rString, Len(rString)
' Result:
' 123 3
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Returns a string's ('ParseString') first consecutive digits
' in a string.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function StrFirstDigits(ByVal ParseString As String) As String
Dim ResultString As String
Dim Char As String
Dim FoundDigit As Boolean
Dim n As Long
For n = 1 To Len(ParseString)
Char = Mid(ParseString, n, 1)
If Char Like "#" Then
If Not FoundDigit Then FoundDigit = True
ResultString = ResultString & Char
Else
If FoundDigit Then Exit For
End If
Next n
StrFirstDigits = ResultString
End Function
Parsing strings is fairly simple if data has a consistent structure. Does not seem to be the case here so gets complicated. Your second example could be accomplished with Val("456_wert") but because the first example does not follow same pattern, will require more complex code. Probably have to test each character until a number is encountered. Based on samples provided, something like:
Function GetNumber(varS As Variant) As Variant
Dim x As Integer
GetNumber = Null
If varS & "" Like "*#*" Then
For x = 1 To Len(varS)
If IsNumeric(Mid(varS, x, 1)) Then
GetNumber = Val(Mid(Replace(varS, " ", "|"), x))
Exit For
End If
Next
End If
End Function
Place the procedure in a general module and call it from query or textbox.
SELECT table.*, GetNumber([source field]) AS Nbr FROM table;
=GetNumber([sourcefield])
Shouldn't really be necessary to populate a field in table with this extract, however, the SQL would be:
UPDATE tablename SET fieldname = GetNumber([source field])
This question already has an answer here:
i want to make a new variable that says shot1 shot2 shot3 so on and forth how do i do this?
(1 answer)
Closed 1 year ago.
i want there to be a new variable name for every new shot, the shot name should be shot1 , shot2 up to 1000
Dim shotlist(1000) As Boolean
Sub Main()
For i As Integer = 0 To 1000
Dim istring As String = i.ToString
shotlist(i) = "shot" & istring
Next
End Sub
Any help would be great thanks
You just assigned String ( i.e., istring ) to a Boolean ( i.e., shotlist(i) ), i hope this helps now.
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 5 years ago.
Improve this question
I wanted to design a vb.net program which finds the longest word from a string. With the help of other users i managed to do that and i added other string operations. Overall the program now counts the number of symbols, has word count, interval count, finds longest and shortest word and the average word size. I thought the code could help others who have the same issues with those operations like me so i posted it below.
Here is a image of the program: https://i.stack.imgur.com/KcPNa.png
This is how the final code looks:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As String = TextBox1.Text
Dim a1 As Integer = Len(a) ' string size
Dim a3 As String = a.Split(" ").Length - 1 ' interval count
Dim a4 As String = a.Split(" ").OrderByDescending(Function(j) j.Length).FirstOrDefault 'longest word
Dim a5 As String = a.Split(" ").OrderByDescending(Function(j) j.Length).LastOrDefault 'shortest word
Dim a6 As String = a.Split(" ").Average(Function(j) j.Length) 'average word count
TextBox2.Text = a1
TextBox4.Text = a3
TextBox3.Text = a3 + 1 'word count is just 1 more than interval count
TextBox5.Text = a4
TextBox6.Text = a5
TextBox7.Text = a6
End Sub
I can not provide an solution in vb but maybe my c# implementation will give you a hint.
string source = "some string";
string longest = string.empty;
foreach(string s in source.Split(' ')) {
if (s.Length > longest.Length) {
longest = s;
}
}
Console.WriteLine(longest);
Tip: Next Time it would be helpfull to ask more specific questions.
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 5 years ago.
Improve this question
Basically, I have list of credits in a spreadsheet. Each credit heading on the sheet is displayed as Credit(5) and it displays below that 5 credits i.e:
Credit(5)
Cre1
Cre2
Cre3
Cre4
Cre5
Then I have another heading with Credit(3) and that displays 3 credits below it i.e:
Credit(3)
Cre1
Cre2
Cre3
Now my question is how to do this in VB and relay on the numbers (3) and (5) and display below the heading list according to the number in the heading? so in other words have 5 columns below the heading if heading has (5) and 3 for the other one.
Something like this? Credit to paxdiablo here. Edited.
Private Sub CommandButton1_Click()
Dim sTitle As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As Integer
Dim i As Integer
Dim k As Integer
For k = 1 To 50
sTitle = Worksheets("Sheet1").Cells(1, k)
If sTitle <> "" Then
openPos = InStr(sTitle, "(")
closePos = InStr(sTitle, ")")
midBit = Mid(sTitle, openPos + 1, closePos - openPos - 1)
For i = 1 To midBit
Worksheets("Sheet1").Cells(i + 1, k) = "cre" & i
Next i
End If
Next k
End Sub
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to convert this little code from Vba to Vb.net without success.
Would like some help please.
Dim str As String, i As Long
For i = 0 To lstPages.ListCount - 1
If lstPages.Selected(i) Then
If str <> vbNullString Then str = str & "-"
str = str & lstPages.List(i)
End If
Next
So you want the third column, from all selected rows of the ListView, in one string separated by "-"?
Yes that's right.
Then do:
Dim values As New List(Of String)
For Each lvi As ListViewItem In lstPages.SelectedItems
values.Add(lvi.SubItems(2).Text)
Next
Dim str As String = String.Join("-", values)
Debug.Print(str)
I think you could do something like this:
For i as integer = 0 To lstPages.ListCount - 1
If lstPages.Selected(i) Then
If Not String.IsNullOrEmpty(str) Then
str &= "-"
str &= lstPages.List(i)
End If
Next