VBA, For-Loops not initiating? - vba

I'm really confused as to why I have 2 For loops that aren't initiating. I have others in my macro that work fine, however. I even tried pulling the code sets out into different subs to see if the other code affected it, however it still didn't work! (This code worked fine yesterday... and the declarations are all correct (used elsewhere and reset fine).
One example:
strVal = regionArr(counti, 3)
For textSearch = 6 To lastrowVF
If UCase(lesTab.Range("C" & textSearch).Text) Like UCase(strVal) & "*" Then
inTot.Range("K" & 2 + county).Value = lesTab.Range("C" & textSearch).Offset(0, 1).Value
inTot.Range("L" & 2 + county).Value = lesTab.Range("C" & textSearch).Offset(0, 2).Value
county = county + 1
End If
Next textSearch

Related

VBA - Struggling with worksheet_change. Not working with no error given

I have a sheet in which our wholesale team are to enter L09 Part Codes and quickly see how much we have in stock of that item. The problem is that new starters may struggle to learn these part numbers as they don't follow a simple rule. What I did was create an easier code to remember which is simply: "Cable Type" & "Core Size" & "Cut Length", they also have the option to add "Colour" and "Brand" separated by spaces.
Their entered string may look like 6242y 2.5 100, or maybe 6242y 2.5 100 Grey, etc. and so where to look in my mapped table for what they've written depends on how many terms they put in. As you can see from the attached picture I need to select the correct column to look in for their code, and then offset back a few columns to suggest the correct L09 Part Number.
I hope the context makes a bit of sense and helps with the below code. The idea was for a new starter to enter something simple and it be replaced before their very eyes...
If anyone could help me to correct the following it would be greatly appreciated:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim P, Products, S, Search As Range
Dim Column As String
Dim Counter As Integer
Dim Spaces As Long
'On Error Resume Next
Counter = 0
'For top table only
If Target.Column = 1 And Target.Row < 100 Then
'Count spaces
Spaces = UBound(Split(Target, " "), 1)
Select Case Spaces
Case Is = 2
Column = "M"
Case Is = 3
Column = "O"
Case Is = 4
Column = "Q"
End Select
'When string has spaces
If Spaces <> 0 Then
'Set simple code range
Set Search = Sheets("Cherries").Range(Column & 1 & ":" & Column & 10000)
For Each S In Search
If S = Target Then
Target = S.Offset(0, 3 - 2 * Spaces)
End If
Next S
End If
Set Products = Sheets("Order Entry").Range("A3:A99")
For Each P In Products
If P.Value <> "" Then
Counter = Counter + 1
End If
Next P
Sheets("Order Entry").Rows("3:" & Counter + 11).Hidden = False
Sheets("Order Entry").Rows(Counter + 11 & ":99").Hidden = True
End If
End Sub
Unfortunately I'm not sure which line is erroring as no error message is given.
Thank you for your time.

add names of two variables and make a new dynamic variable

I am writing a code where I have a for loop in which I give a variable (named VType ) some value. For loop goes for a range of i variables. Now I want to make a new variable by concatenating names of both variables. for example if i = 1 then I want to make variable VType1. Here is my piece of code.
nrec = Split(Split(ie.document.body.innerHTML, "Found <strong>")(1), "</strong> records")(0)
If nrec = 1 Then
lnk.Click
Else
For j = 1 To nrec
link.Click
Do While ie.readyState <> 4: Wait 5: Loop
Application.Wait (Now + TimeValue("0:00:01"))
'VType , j = GetType
'Application.Wait (Now + TimeValue("0:00:01"))
IMO , j = GetValue("IMO:")
'MMSI = GetValue("MMSI:")
YBuilt , j = GetValue("Year Built:")
Flag , j = GetValue("Flag:")
DWT , j = GetValue("Deadweight:")
Next j
num = "1 - " & IMO1
For i = 2 To nrec
num = num & vbCrLf & i & "abc"
Next I
fin = InputBox(num, nrec & " records found for a. please select right one.")
Exit For
End If
There is not a way to directly do what you're specifically requesting. However, you can use arrays to get a similar outcome. Arrays are a not a topic that can be explained in a single posted answer, but if you do a little research you can probably figure out how the below might be useful...
Dim VTtyp(0 to i) as string
'while Looping...
Vtype(i) = "Whatever you want stored in this round of i"
When your code completes, you'll have all fields saved as variables that can be called from this array. An example is if you wanted to call the one that was tied to the number "2" you could type: Vtype(2) and it would call the text from the 2 iteration.
Again this example is extremely simplified and there are things to consider such as dim size, changing the dim, preserving the array, etc. and that is something you'll have to research further. However bottom line is, "there is not a way to do what you're specifically trying to do."
You can achieve this using Dictionary objects concept. Go through the below link to know more about dictionary objects.
https://www.tutorialspoint.com/vbscript/vbscript_dictionary_objects.htm

Double For Loop in VB.NET

Dim ssi(11) As String
For i = 0 To 10
If ssi(i) = "" Then ssi(i) = "0"
For j = 0 To Val(ssi(11)) + i
ssi(i) = xuh(Val(ssi(i)))
Next
Next
If ssi(11) = "2" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10)))
ElseIf ssi(11) = "3" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10))) * (-1)
End If
I am new here and new to VB as well.
I am trying to understand this double loop in vb code.
ssi(i) is defined as a String variable. and each element is assigned to a specific number in a String. Hope I told it clearly.
My problem with this loop is below.
Since i ranges from 0 to 10, what does this j mean? Does j mean the new ssi(1-10) or another whatever number?
I think the best way to answer your question about understanding a double loop is to try looking at something simpler.
The first program I always write in each new version of BASIC that comes along is a 12 times table.
I've modified it a bit below to be a 12 x 10 table for the purpose of illustrating for you how a double loop works ... hope it helps:
For x As Integer = 1 To 12
For y As Integer = 1 To 10
Console.Write(x * y)
Console.Write(vbTab)
Next
Console.WriteLine()
Next

If ElseIf And Or functions VBA

I have a really long IF AND OR formula that I'm trying to convert to VBA so it's quicker.
=IF(OR(H10<>"GL402",H10<>"GL412",H10<>"GL422",H10<>"GL432",H10<>"GL442",H10<>"GL452",H10<>"GL492",
H10<>"GL480",H10<>"GL370",H10<>"GL380",H10<>"GL710")*AND(OR(D10<>3,D10<>9,D10<>10),E10<>"ASX",
F10<>"AUD"),"F126",(IF(OR(H2="GL402",H2="GL412",H2="GL422",H2="GL432",H2="GL442",H2="GL452",H2="GL492")*
AND(OR(D2<>"3",D2<>"9",D2<>"10"),E2="ASX",F2="AUD"),"D111",.......))
I thought this should look like:
IF range("H10").Value <>""GL402"" or ""GL412"" or ""GL422"" or ""GL432"" or ""GL442"" _
or ""GL452"" or ""GL492"" or ""GL480"" or ""GL370"" or ""GL380"" or ""GL710"" AND _
range("D10").Value <>3 or 9 or 10 and range("E10").Value <>""ASX"" and _
range("F10").Value <>""AUD""
then
range("I10").Value = ""F126""
elseif
Range("H2").Value = ""GL402"" Or ""GL412"" Or ""GL422"" Or ""GL432"" Or ""GL442"" Or ""GL452"" Or ""GL492"" _
And Range("D2").Value <> 3 Or 9 Or 10 And Range("E2").Value = ""ASX"" And Range("F2").Value = ""AUD""
then
Range("I2").Value = ""D111""
elseif
another lengthy conditions with ANDs and ORs
plus I was hoping to loop this so it applies this whole IF formula until the value of cell A (whichever row) is blank.
I sort of know the loop should be
Do .........
next (with something like A1 + 1)
until A1 + 1 = ""
loop
any help appreciated!
The first rule of good code is that it should be clear - easy to read and debug. Only afterwards do you try to make it "fast". Converting your current expression to VBA may give a speed advantage but you still don't meet the first test...
You can make things cleaner with an expression like this (you can put this right in your spreadsheet):
=ISERROR(MATCH(H10,{"GL402","GL412","GL422","GL432","GL442","GL452","GL492","GL480","GL370","GL380","GL710"},0))
This will evaluate to "true" if the the value in H10 does not match any of the values in the array.
When you have a lot of or conditions in parallel, you can basically stop when the first condition is true.
An expression like that can be written in VBA as follows:
Sub test()
Dim matchStrings
Dim match1, match2
matchStrings = Array("GL402", "GL412", "GL422", "GL432", "GL442", "GL452", "GL492", "GL480", "GL370", "GL380", "GL710")
firstPart = Application.Match(Range("H10"), matchStrings, 0)
If IsError(firstPart) Then
MsgBox "no match found"
Else
match1 = true
MsgBox "match found at index " & firstPart
End If
End Sub
You can repeat similar code with other expressions, building match2, match3, etc - then combining with all the And and Or that you would like - for example
If match1 And (match2 Or match3) Then
... do something
Else
... do something else
End If
This won't work as expected:
If x = 1 Or 2 Or 3 Then
MsgBox "x is either 1, 2, or 3"
End If
because 2 and 3 aren't boolean (true/false) conditions (at least not the way you expect them to be).
The proper syntax is:
If x = 1 Or x = 2 Or x = 3 Then
MsgBox "x is either 1, 2, or 3"
End If
This is only a partial answer that nevertheless does address one of the many issues in your code.

sub declaration visual Basic

I am really new with vba, so please forgive my slaughtering of the code. I'm trying to write a macro for excel (my first one), and I get "statement invalid outside of type block" (pointing at the first line). Here is my code:
Sub MakeHTMLTable()
Worksheets("Sheet1").Activate
endRow As Integer
For Count = 1 To 200
For CountY = 1 To 200
If (!ActiveSheet.Cells(Count, CountY).Value.IsEmpty) Then
ActiveSheet.Cells(Count, CountY).Value = "<td>" + ActiveSheet.Cells(Count, CountY).Value + "</td>"
End If
Exit For
Exit For
For i = 1 To 200
If (!ActiveSheet.Cells(i, 1).Value.IsEmpty()) Then
ActiveSheet.Cells(i, 1).Value = "<tr>" + ActiveSheet.Cells(i, 1)
End If
Exit For
For x = 1 To 200
If (!ActiveSheet.Cells(x, 1).Value.IsEmpty()) Then
endRow = x
End If
Exit For
For countAgain = 1 To 200
If (!ActiveSheet.Cells(x, countAgain).Value.IsEmpty()) Then
ActiveSheet.Cells(x, countAgain).Value = ActiveSheet.Cells(x, countAgain).Value + "</tr>"
End If
Exit For
End Sub
I really don't understand, as the debugger fails on the line of computer generated code without even making it to mine. Have I missed ending an If" or For block?
I also realize that I'm probably reinventing the wheel. Any help on more appropriate built in functions would be appreciated.
It looks like you've got quite a few syntax errors in your code.
In VBA, local variables are declared with the Dim keyword.
So, the endRow declaration should look like this:
Dim endRow As Integer
For loops should end with the Next statement. So, your For loops should look like this:
For x = 1 To 200
If (!ActiveSheet.Cells(x, 1).Value.IsEmpty()) Then
endRow = x
End If
Next
VBA uses the keyword Not instead of !, so, your conditionals should look like this:
If (Not (ActiveSheet.Cells(i, 1).Value.IsEmpty())) ...
Try removing most of the code and adding it back in line-by-line until all of it works. The VBA syntax can be cumbersome for those who aren't used to it.