I'm trying to unhide a range of rows. This is to be done within a loop, so I'm using variables; here's the code:
For i = 2 To lastrow
If Workbooks("Discrepancies1").Worksheets(1).Cells(i, 8) = "USRFLG02=T" Then
a = Workbooks("Discrepancies1").Worksheets(1).Cells(i, 46).Value
b = Application.WorksheetFunction.CountIf(c, a)
Workbooks("Discrepancies1").Worksheets(1).Rows("i: i + b - 1").Hidden = False
End If
Next
End If
However, running this gives me a type mismatch error on the last line of code. I've checked all the variables, and they are what they should be. It seems like VBA doesn't like the colon (denoting range) in conjunction with variables. I can run this without variables, or without the range, but I can't have both. Suggestions?
With Workbooks("Discrepancies1").Worksheets(1)
For i = 2 To lastrow
If .Cells(i, 8) = "USRFLG02=T" Then
a = .Cells(i, 46).Value
b = Application.CountIf(c, a)
.Rows(i & ":" & i + b - 1).Hidden = False
End If
Next
End With
Related
I want to start second for from next current first for counter I run this code and this error prevent to run code.
ERROR : type mismatch
This code should show shortest distance cells as when find next cell(short distance) this cell should remove from search
also I want to return the address(ROW NUMBER) of next cell(shortest distance)
`Sub distance()
Dim j, i As Integer, ws As Worksheet
Set ws = Worksheets("activesheet")
For i = 2 To 87
For j = i+1 To 87
If j <> i Then
Worksheets("activesheet").Cells(j, 11).Value = Sqr(((Worksheets("activesheet").Cells(i,
8).Value) - (Worksheets("activesheet").Cells(j, 8).Value)) ^ 2 +
((Worksheets("activesheet").Cells(i, 9).Value) - (Worksheets("activesheet").Cells(j,
9).Value)) ^ 2)
Next j
ws.Range("l" & i) = Application.WorksheetFunction.Small(ws.Range("k2:k87"), 1)
Next i
End Sub`
I have reformatted your code so I could read it, it would not compile because you were missing an End If (as indicated by a comment in the formatted code below). The code runs fine on a test workbook I have just created. However of course I don't know what source data you have.
Sub distance()
Dim j, i As Integer, ws As Worksheet
Set ws = Worksheets("activesheet")
For i = 2 To 87
For j = i + 1 To 87
If j <> i Then
Worksheets("activesheet").Cells(j, 11).Value = Sqr(((Worksheets("activesheet").Cells(i, 8).Value) - (Worksheets("activesheet").Cells(j, 8).Value)) ^ 2 + ((Worksheets("activesheet").Cells(i, 9).Value) - (Worksheets("activesheet").Cells(j, 9).Value)) ^ 2)
End If 'this was missing
Next j
ws.Range("l" & i) = Application.WorksheetFunction.Small(ws.Range("k2:k87"), 1)
Next i
End Sub
Given that this runs okay, I recommend checking the data being used from the worksheet on this line
Worksheets("activesheet").Cells(j, 11).Value = Sqr(((Worksheets("activesheet").Cells(i, 8).Value) - (Worksheets("activesheet").Cells(j, 8).Value)) ^ 2 + ((Worksheets("activesheet").Cells(i, 9).Value) - (Worksheets("activesheet").Cells(j, 9).Value)) ^ 2)
and this line
ws.Range("l" & i) = Application.WorksheetFunction.Small(ws.Range("k2:k87"), 1)
and make sure you are getting data of the correct type. Do you have any words or letters or non numeric data of any kind being used?
To demonstrate the problem, below I am trying to divide the variable A (a string) by the variable B (an integer) and get a type mismatch error as a result.
Sub test()
Dim a As String
a = "test"
Dim b As Integer
b = 1
Debug.Print a / b
End Sub
I am trying to extract a substring which has a random position from different strings. The substing is not a fixed value but a "T" and then four numberals e.g. T6000.
As you can see in this image there are a number of machines names where most of them contain a T number. The T number is also different in almost all of the cases. The column of the machines names is "E". First number (T6000) is in E16, last is in E25.
Using my code:
For Ipattern = 16 To NumofMachines + 15 Step 1
TNUMcell = Dsht.Range("E" & Ipattern).Value
'Verify if string contains a Tnum
TNUMLikeBoolean = TNUMcell Like "*T###*"
If TNUMLikeBoolean = True Then
Do Until TNUMdone = True
TNUMchar1 = InStr(TNUMcell, "T") + 1
TNUMcharV = Mid(TNUMcell, TNUMchar1)
TNUMchecknum = IsNumeric(TNUMcharV)
If TNUMchecknum = True Then
Dsht.Range("F" & Ipattern).Value = "T" & Mid(TNUMcell, TNUMchar1, 5)
TNUMdone = True
End If
Loop
Else
Dsht.Range("F" & Ipattern).Value = "NO T"
End If
Next Ipattern
It only fills in the first and the last cell of the 'export' range (F16:F25).
I have been searching for an answer quite some time. As I am (obviously) not a VBA expert.
What am I doing wrong? Why is not filling in the other values?
Thanks,
Wouter J
Try this code
Sub Test()
Dim r As Range, i As Long, c As Long
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "T\d{4}"
For Each r In Range("E16", Range("E" & Rows.Count).End(xlUp))
c = 6
If .Test(r.Value) Then
For i = 0 To .Execute(r.Value).Count - 1
Cells(r.Row, c).Value = .Execute(r.Value)(i)
c = c + 1
Next i
End If
Next r
End With
End Sub
The problem is with your variable TNUMdone.
This is set to True on the first iteration of the loop and then never again set to False, so this code after Do Until TNUMdone = True never runs again.
At the start of your loop, just set TNUMdone to False and it should work:
For Ipattern = 16 To NumofMachines + 15 Step 1
TNUMdone = False
TNUMcell = Dsht.Range("E" & Ipattern).Value
...
When I go to run the VBA code it gives me an error of "Compile Error: Type Mismatch". It doesn't give an error code like it previously has.
It is being used to work on this data. The data on the left is a table that I am running against the values in Row J and assigning the ID in Row M
[Image of Excel][1]
Sub Cat()
Dim i As String
Dim x As String
For i = 1 To 113
For x = 1 To 13
If Sheet1.Cells("B", i).Value = Sheet1.Cells("J", x) Then
Sheet1.Cells("A", i).Value = Sheet1.Cells("M", x)
End If
Next
Next
End Sub
I am expecting IDs of Row A to be inserted where B and M equal as per the for loops
Multiple Type Mismatch
Remember: You can use e.g. Cells(i, "B") or Range("B" & i) for a one-cell range.
The Code
Option Explicit
Sub Cat()
Dim i As Long
Dim x As Long
For i = 1 To 113
For x = 1 To 13
If Sheet1.Cells(i, "B").Value = Sheet1.Cells(x, "J") Then
Sheet1.Cells(i, "A").Value = Sheet1.Cells(x, "M")
End If
Next
Next
End Sub
Aside from the obvious i As String being illegally used as an integer value for loop-counting (should be declared As Long), you are inverting the RowIndex and ColumnIndex arguments of an implicit Range.[_Default] member call:
Sheet1.Cells("B", i).Value
"B" isn't a valid RowIndex value.
Rule of thumb, avoid Range.Cells when you have a string for a column; use it when you have a row index and a column index, i.e. numeric values for row & column.
When you have a column heading string such as "B", use the Range property and concatenate the cell address, e.g. Sheet1.Range("B" & i).
I have a code that searches an excel fill for the word distance, takes the value of the cell next to it, paste it into a new cell, then sums all the cells up. Which works great, but i now need to find a way to only sum ever even row number. Does that make sense?
Sub Distance_Check()
Dim DistanceCheck As String
Dim DistanceNumber As String
Dim DistanceSum As String
Dim DistanceTotal As String
DistanceCheck = MsgBox("Would you like to check the distance?", vbYesNo)
If DistanceCheck = vbYes Then
If IsArray(fname) Then Workbooks.OpenText fname(1)
i = 1
findStr = "Distance"
Set foundCel = Range("A:A").Find(what:=findStr)
If Not foundCel Is Nothing Then
firstAddress = foundCel.Address
Do
Range("J" & i).Value = foundCel.Offset(0, 1).Value
Set foundCel = Range("A:A").FindNext(foundCel)
i = i + 1
Loop While Not foundCel Is Nothing And foundCel.Address <> firstAddress
End If
Set wkbSourceBook = ActiveWorkbook
DistanceNumber = i - 2
DistanceSum = WorksheetFunction.Sum(Range(Cells(2, 15), (Cells(DistanceNumber + 1, 15))))
DistanceTotal = DistanceSum / DistanceNumber
If DistanceNumber = Cells(2, 12) Then
MsgBox ("No error found wihin distance")
Else
MsgBox ("Error found with distance")
End If
Else
End If
Call Save_Data
End Sub
Would the way youd go about this be using a for loop on the
cells(DistanceNumber(j,+1)
Where j = 0,
j = j +2 ,
Until j > DistanceNumber,
Would that work? If so how would you go about it?
Thanks
A quick way to step through a loop in the desired increments is to use the Mod operator which divides two numbers and returns any remainder (e.g. 7 mod 2 = 1, as two sixes fit into seven, leaving one).
You can use the row property of the range you identify with the Find method, and since you want to jump by two the modulo should be zero:
If foundcel.Row Mod 2 = 0 Then Range("J" & i).value = foundcel.Offset(0, 1).Value
That said, there is a 'built in' way to step through a loop if using a For loop like this
For x = 2 to 10 Step 2
' Do stuff
Next x
You can also use this method to step backwards, e.g.
For x = 100 to 0 Step -10
' Do stuff backwards!
Next x
I have an excel spreadsheet. In a column of the spreadsheet I have a list of codes (numbers).These codes (numbers) are sorted from highest to lowest values.(some of these codes has been repeated. For example I have three consecutive line with code 1001200).I want to insert new rows between each codes (in case of having repeated codes i just need one new row (for example i Just need one new row for 1001200 not 3 rows) .
I have written the following code but it does not work.
Sub addspace()
Dim space_1(5000), Space_2(5000)
For n = 1 To 5000
Debug.Print space_1(n) = Worksheets("sheet3").Cells(1 + n, 1).Value
Debug.Print Space_2(n) = Worksheets("sheet3").Cells(2 + n, 1).Value
Next
For n = 1 To 5000
If space_1(n) <> Space_2(n) Then
Range("space_1(n)").EntireRow.Insert
End If
Next
End Sub
How can I fix it? (From the code you can see that I am so beginner :)))
Cheers
To insert one empty row between each unique value try this:
Option Explicit
Public Sub addspace()
Dim i As Long
Application.ScreenUpdating = False
With Worksheets("sheet3")
For i = 5000 To 2 Step -1
While .Range("A" & i - 1) = .Range("A" & i)
i = i - 1
Wend
.Rows(i).Insert Shift:=xlDown
Next
End With
Application.ScreenUpdating = True
End Sub
It starts from the end row and moves up, skipping duplicates
The Range("space_1(n)") is invalid. Arg of range object should be a column name like "A1", you can use Range("A" & n).EntireRow.Insert in your code. But I recommend my code.
Please try,
Sub addspace()
Dim n As Integer
For n = 1 To 5000
If Worksheets("sheet3").Cells(n, 1).Value <> Worksheets("sheet3").Cells(n + 1, 1).Value Then
Worksheets("sheet3").Cells(n + 1, 1).EntireRow.Insert
n = n + 1
End If
Next
End Sub