Do While Type Mismatch - vba

I am getting a type mismatch error on my IF statement. I can't seem to resolve it. Your help is appreciated!
Sub practice()
Dim i As Long
Dim myrange As Range
Set myrange = Worksheets("test").Range("A1:O26")
i = 1
Do While i <= 9999
If Worksheets("test").Range("A1:O26").Offset(0, i - 1).Value = "" Then
Exit Do
End If
Worksheets("test_table").Range("rest_out").Offset((i - 1) * 24, 0) =_
Worksheets("test").Range("A1:O26").Offset(0, i - 1)
Worksheets("test_table").Range("rest_out").Offset((i - 1) * 24, 0).Font.Bold = True
i = i + 1
Loop
End Sub

Related

Excel VBA run time error 1004 method range of object _worksheet failed

I'm really in need of help to pinpoint what's wrong on my excel vba. Totally beginner here.
Basically the the vba code will do "filter" for the worksheet. The weird things is that when filter on other categories, the vba works. But on 1 category, it just keep giving me run-time error 1004 (method range of object _worksheet failed).
Debug mode will always pinpoint to the line of code:
Range(Mid(rangeToHide, 1, 199)).Select
Here is the code:
Private Sub cboPopulateDept_Change()
Dim sh As Worksheet
Dim rw As Range
Dim RowCount As Integer
Dim rangeToHide As String
Dim emptyRow As Integer
unHide
If cboPopulateDept.Value = "ALL" Or cboPopulateDept.Value = "" Then
Exit Sub
End If
RowCount = 1
Set sh = ActiveSheet
For Each rw In sh.Rows
If RowCount >= 6 Then
If sh.Cells(RowCount, 1).Value Like "TOP Innovation Projects - Vision 2020 - Participating?" Then
Exit For
End If
If sh.Cells(RowCount, 3).Value <> cboPopulateDept.Value And sh.Cells(RowCount, 3).Value <> "" Then
'sh.Cells(RowCount, 3).EntireRow.Hidden = True
'sh.Cells(RowCount + 1, 3).EntireRow.Hidden = True
rangeToHide = rangeToHide & RowCount & ":" & RowCount + 1 & ","
RowCount = RowCount + 2
Else
RowCount = RowCount + 1
End If
Else
RowCount = RowCount + 1
End If
Next rw
rangeToHide = Mid(rangeToHide, 1, Len(rangeToHide) - 1)
If Len(rangeToHide) <= 201 Then
Range(rangeToHide).Select
Selection.EntireRow.Hidden = True
Else
Range(Mid(rangeToHide, 1, 199)).Select
Selection.EntireRow.Hidden = True
Range(Mid(rangeToHide, 201, Len(rangeToHide))).Select
Selection.EntireRow.Hidden = True
End If
'Range(rangeToHide).Select
'Selection.EntireRow.Hidden = True
Range("A8:A9").Select
End Sub
Thanks
Cheers,
RH
You are probably not selecting a valid range with:
Range(Mid(rangeToHide, 1, 199)).Select
As rangeToHide is declared as a string, unless Mid(rangeToHide, 1, 199) resolves to a valid range address such as "A3" then you won't be able to use it within Range(myexpression).Select. Put a watch on this expression and have it stop on change of value. Then run the code and check the value of the expression at the point it changes when using the filter in question.
You also should be using VBNullString instead of ' ' and Long instead of integers. Also, use option explicit at the top to check you have declared all your variables and that they are used. And explicitly reference the ActiveWorkbook rather than implicitly.

How to correct a userform when error 13 is displayed in VBA?

I'm currently on a project that search in a product database all non-referenced product (blank fields). When I click on the button that opens a userform, error 13 is displayed, here is the code:
Dim i As Integer
Dim j As Integer
Dim t As Integer
Dim r As Integer
t = 1
While Feuil3.Cells(t, 1) <> ""
t = t + 1
Wend
t = t - 1
For r = 2 To t
If Feuil3.Cells(r, 3) = "" Then
i = 1
While Feuil2.Cells(i, 1) <> ""
i = i + 1
Wend
Feuil2.Cells(i, 1) = Feuil3.Cells(r, 2)
End If
Next
i = 1
While Feuil2.Cells(i, 1) <> ""
i = i + 1
Wend
For j = 2 To i
If Feuil2.Cells(j, 2) = "" Then
list51.AddItem Feuil2.Cells(j, 1)
End If
Next
End Sub
It appears that the error comes from this line:If Feuil3.Cells(r, 3) = "" Then
My skills in VBA are limited, do you have any idea on how to fix this problem?
Thanks,
Have a look at this. Should do the same just a lot less iteratively
Dim Feuil2Rng As Range, Feuil3Rng As Range
Dim c
With Feuil3
Set Feuil3Rng = .Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1))
End With
For Each c In Feuil3Rng
If c.Offset(0, 2) = vbNullString Then
With Feuil2
.Cells(.Cells(.Rows.Count, 1).End(xlUp).Row + 1, 1) = c.Offset(0,1)
End With
End If
Next
With Feuil2
Set Feuil2Rng = .Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1))
End With
For Each c In Feuil2Rng
If c.Offset(0, 1) = vbNullString Then
list51.AddItem c.Value2
End If
Next

Table editing with excel vba causing crashing and cell lockup

I have made a userform that allows the user to select a table and add rows to it and fill those rows with various information, all from the userform. I have run into a few problems with this.
First after adding or during adding the items (after hitting submit) excel would crash. It occurs randomly and is hard to reproduce.
Second after running the macro there is a good chance that all the cells in the workbook and every other object except the userform button will stop working, meaning you can't edit interact or even select anything. Then when I close the workbook excel crashes after saving. This is my major offender and I think causes the other problem.
What causes this freezing and why does it occur? How do I fix it? I have looked around and haven't found anything circumstantial. One post said that I should try editing the table with no formatting on it and I did that and it didn't work.
I can provide the excel workbook at a request basis via pm.
The code:
On Activate -
Public Sub UserForm_Activate()
Set cBook = ThisWorkbook
Set dsheet = cBook.Sheets("DATA")
End Sub
Help Checkbox -
Private Sub cbHelp_Click()
If Me.cbHelp.Value = True Then
Me.lbHelp.Visible = True
Else
Me.lbHelp.Visible = False
End If
End Sub
Brand combobox -
Public Sub cmbBrand_Change()
brandTableName = cmbBrand.Value
brandTableName = CleanBrandTableName(brandTableName)
'if brand_edit is not = to a table name then error is thrown
On Error Resume Next
If Err = 380 Then
Exit Sub
Else
cmbItemID.RowSource = brandTableName
End If
On Error GoTo 0
'Set cmbItemID's text to nothing after changing to a new brand
cmbItemID.Text = ""
End Sub
CleanBrandTableName(brandTableName) function -
Option Explicit
Public Function CleanBrandTableName(ByVal brandTableName As String) As String
Dim s As Integer
Dim cleanResult As String
For s = 1 To Len(brandTableName)
Select Case Asc(Mid(brandTableName, s, 1))
Case 32, 48 To 57, 65 To 90, 97 To 122:
cleanResult = cleanResult & Mid(brandTableName, s, 1)
Case 95
cleanResult = cleanResult & " "
Case 38
cleanResult = cleanResult & "and"
End Select
Next s
CleanBrandTableName = Replace(WorksheetFunction.Trim(cleanResult), " ", "_")
End Function
Public Function CleanSpecHyperlink(ByVal specLink As String) As String
Dim cleanLink As Variant
cleanLink = specLink
cleanLink = Replace(cleanLink, "=HYPERLINK(", "")
cleanLink = Replace(cleanLink, ")", "")
cleanLink = Replace(cleanLink, ",", "")
cleanLink = Replace(cleanLink, """", "")
cleanLink = Replace(cleanLink, "Specs", "")
CleanSpecHyperlink = cleanLink
End Function
Browse button -
Public Sub cbBrowse_Click()
Dim rPos As Long
Dim lPos As Long
Dim dPos As Long
specLinkFileName = bFile
rPos = InStrRev(specLinkFileName, "\PDFS\")
lPos = Len(specLinkFileName)
dPos = lPos - rPos
specLinkFileName = Right(specLinkFileName, dPos)
Me.tbSpecLink.Text = specLinkFileName
End Sub
bFile function -
Option Explicit
Public Function bFile() As String
bFile = Application.GetOpenFilename(Title:="Please choose a file to open")
If bFile = "" Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Function
End If
End Function
Preview button -
Private Sub cbSpecs_Click()
If specLinkFileName = "" Then Exit Sub
cBook.FollowHyperlink (specLinkFileName)
End Sub
Add Item button -
Private Sub cbAddItem_Click()
Dim brand As String
Dim description As String
Dim listPrice As Currency
Dim cost As Currency
Dim Notes As String
Dim other As Variant
itemID = Me.tbNewItem.Text
brand = Me.tbBrandName.Text
description = Me.tbDescription.Text
specLink = Replace(specLinkFileName, specLinkFileName, "=HYPERLINK(""" & specLinkFileName & """,""Specs"")")
If Me.tbListPrice.Text = "" Then
listPrice = 0
Else
listPrice = Me.tbListPrice.Text
End If
If Me.tbCost.Text = "" Then
cost = 0
Else
cost = Me.tbCost.Text
End If
Notes = Me.tbNotes.Text
other = Me.tbOther.Text
If Me.lbItemList.listCount = 0 Then
x = 0
End If
With Me.lbItemList
Me.lbItemList.ColumnCount = 8
.AddItem
.List(x, 0) = itemID
.List(x, 1) = brand
.List(x, 2) = description
.List(x, 3) = specLink
.List(x, 4) = listPrice
.List(x, 5) = cost
.List(x, 6) = Notes
.List(x, 7) = other
x = x + 1
End With
End Sub
Submit button -
Private Sub cbSubmit_Click()
Dim n As Long
Dim v As Long
Dim vTable() As Variant
Dim r As Long
Dim o As Long
Dim c As Long
Dim w As Variant
Set brandTable = dsheet.ListObjects(brandTableName)
o = 1
listAmount = lbItemList.listCount
v = brandTable.ListRows.Count
w = 0
For c = 1 To listAmount
If brandTable.ListRows(v).Range(, 1).Value <> "" Then
brandTable.ListRows.Add alwaysinsert:=True
brandTable.ListRows.Add alwaysinsert:=True
Else
brandTable.ListRows.Add alwaysinsert:=True
End If
Next
ReDim vTable(1000, 1 To 10)
For n = 0 To listAmount - 1
vTable(n + 1, 1) = lbItemList.List(n, 0)
vTable(n + 1, 2) = lbItemList.List(n, 1)
vTable(n + 1, 3) = lbItemList.List(n, 2)
vTable(n + 1, 5) = lbItemList.List(n, 4)
vTable(n + 1, 6) = lbItemList.List(n, 5)
vTable(n + 1, 7) = lbItemList.List(n, 6)
vTable(n + 1, 8) = lbItemList.List(n, 7)
If lbItemList.List(n, 3) = "" Then
ElseIf lbItemList.List(n, 3) <> "" Then
vTable(n + 1, 4) = lbItemList.List(n, 3)
End If
If n = 0 And brandTable.DataBodyRange(1, 1) <> "" Then
For r = 1 To brandTable.ListRows.Count
If brandTable.DataBodyRange(r, 1) <> "" Then
o = r + 1
' brandTable.ListRows.Add alwaysinsert:=True
End If
Next
End If
brandTable.ListColumns(1).DataBodyRange(n + o).Value = vTable(n + 1, 1)
brandTable.ListColumns(2).DataBodyRange(n + o).Value = vTable(n + 1, 2)
brandTable.ListColumns(3).DataBodyRange(n + o).Value = vTable(n + 1, 3)
brandTable.ListColumns(4).DataBodyRange(n + o).Value = vTable(n + 1, 4)
brandTable.ListColumns(5).DataBodyRange(n + o).Value = vTable(n + 1, 5)
brandTable.ListColumns(6).DataBodyRange(n + o).Value = vTable(n + 1, 6)
brandTable.ListColumns(7).DataBodyRange(n + o).Value = vTable(n + 1, 7)
brandTable.ListColumns(8).DataBodyRange(n + o).Value = vTable(n + 1, 8)
Next
brandTable.DataBodyRange.Select
Selection.Font.Bold = True
Selection.WrapText = True
brandTable.ListColumns(5).DataBodyRange.Select
Selection.NumberFormat = "$#,##0.00"
brandTable.ListColumns(6).DataBodyRange.Select
Selection.NumberFormat = "$#,##0.00"
Unload Me
End Sub
Remove Items button -
Private Sub cbRemoveItems_Click()
Dim intCount As Long
For intCount = lbItemList.listCount - 1 To 0 Step -1
If lbItemList.Selected(intCount) Then
lbItemList.RemoveItem (intCount)
x = x - 1
End If
Next intCount
End Sub
There is other code that does things for the other tabs but they don't interact with this tabs code.

Highlighting intersection cell of row and column VBA

I'm trying to record a macro in which if the text in a column header is the same as the text in a row the intersection cell of the row and the column gets highlighted.
For example:
A11: "description"
Y1: "description"
->Y11 should be highlighted
Your answer doesn't seem to intuitively answer the question at hand: How to highlight an intersecting row and column on found match?
A naive approach would be to iterate through the columns and rows to find matches:
Private Sub ColorIntersection()
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim cols As Range, rws As Range
Dim lastRow As Integer: lastRow = ws.UsedRange.Rows.Count
Dim lastColumn As Integer: lastColumn = ws.UsedRange.Columns.Count
For Each cols In ws.Range(ws.Cells(1, 1), ws.Cells(1, lastColumn))
If (Not (cols.Value = vbNullString)) Then
For Each rws In ws.Range("A1:A" & lastRow)
If (rws.Value = cols.Value) Then ws.Cells(rws.Row, cols.Column).Interior.Color = 5296210
Next
End If
Next
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
So this is what it is. Works perfectly with what I need (it also highlights a number of cells ahead of the one on the intersection)
Sub BorderForNonEmpty2()
Dim wb As Workbook
Dim wsCurrent As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb = ActiveWorkbook
Set wsCurrent = wb.ActiveSheet
Dim atLastCompareDate As Boolean
Dim atLastMPDate As Boolean
Dim mPDateCounter As Integer
Dim compareDateCounter As Integer
mPDateCounter = 3
'loop over each row where the value in column c is not empty, starting at row 3
Do While Not atLastMPDate
Dim mPDate As String
mPDate = wsCurrent.Range("C" + CStr(mPDateCounter)).Value
atLastCompareDate = False
If (mPDate = Null Or mPDate = "") Then
atLastMPDate = True
Else
'loop over each column where the value in row 1 is not empty, starting at column e
compareDateCounter = 5
Do While (Not atLastCompareDate)
Dim compareDate As String
Dim currentCellColumn As String
If (compareDateCounter <= 26) Then
currentCellColumn = Chr((compareDateCounter) + 96)
Else
If (compareDateCounter > 26) And (compareDateCounter Mod 26 = 0) Then
currentCellColumn = Chr(Int(compareDateCounter / 26) - 1 + 96) + Chr(122)
Else
currentCellColumn = Chr(Int(compareDateCounter / 26) + 96) + Chr((compareDateCounter Mod 26) + 96)
End If
End If
compareDate = wsCurrent.Range(currentCellColumn + CStr(1)).Value
If (compareDate = Null Or compareDate = "") Then
atLastCompareDate = True
Else
If (compareDate = mPDate) Then
Dim cellLocation As String
If (compareDateCounter <= 26) Then
cellLocation = Chr((compareDateCounter) + 96)
Else
If (compareDateCounter > 26) And (compareDateCounter Mod 26 = 0) Then
cellLocation = Chr(Int(compareDateCounter / 26) - 1 + 96) + Chr(122)
Else
cellLocation = Chr(Int(compareDateCounter / 26) + 96) + Chr((compareDateCounter Mod 26) + 96)
End If
End If
wsCurrent.Range(cellLocation + CStr(mPDateCounter)).Interior.ColorIndex = 11
'Loop backwards to mark the 6 dates before
Dim i As Integer
i = compareDateCounter - 1
Do While (i > compareDateCounter - 7)
If (i <= 26) Then
cellLocation = Chr((i) + 96)
Else
If (i > 26) And (i Mod 26 = 0) Then
cellLocation = Chr(Int(i / 26) - 1 + 96) + Chr(122)
Else
cellLocation = Chr(Int(i / 26) + 96) + Chr((i Mod 26) + 96)
End If
End If
wsCurrent.Range(cellLocation + CStr(mPDateCounter)).Interior.ColorIndex = 43
wsCurrent.Range(cellLocation + CStr(mPDateCounter)).Borders.LineStyle = xlContinuous
wsCurrent.Range(cellLocation + CStr(mPDateCounter)).Borders.ColorIndex = 11
i = i - 1
Loop
atLastCompareDate = True
End If
End If
compareDateCounter = compareDateCounter + 1
Loop
End If
mPDateCounter = mPDateCounter + 1
Loop
End Sub

How to check the cell is valid or not in VBA?

If
r = 1, and
c = 1
the intended code below is invalid (it tries to return a cell two columns to the left of Column A)
Cells(r, c).Offset(0, -2)
How do I check whether the intended cell is valid or not in vba?
Use a Range object to test whether it is valid (preferred for versatility)
Test whether the column is valid (assumes hard-code of your OFFSET as (0,2)
(1) code
Sub Test1()
Dim r As Long
Dim c As Long
r = 1
c = 1
Dim rng1 As Range
On Error Resume Next
Set rng1 = Cells(r, c).Offset(0, -2)
On Error GoTo 0
If Not rng1 Is Nothing Then
'proceed with your code - range exists
Else
MsgBox "Range Error", vbCritical
End If
End Sub
(2) code
Sub Test2()
Dim rng1 As Range
Dim r As Long
Dim c As Long
c = 3
r = 1
If c - 2 <= 0 Then
MsgBox "Error", vbCritical
Else
Set rng1 = Cells(r, c).Offset(0, -2)
End If
End Sub
Here is example for you.
Sub sample()
Dim r As Integer
Dim c As Integer
r = 1
c = 1
Dim validRng As Boolean
validRng = isValidRng(r, c, 0, -2)
Debug.Print validRng
validRng = isValidRng(r, c + 5, 0, 2)
Debug.Print validRng
validRng = isValidRng(r, c, -1, 0)
Debug.Print validRng
validRng = isValidRng(r, c + 2, 0, -1)
Debug.Print validRng
End Sub
Function isValidRng(row As Integer, col As Integer, offsetrow As Integer, offsetcol As Integer) As Boolean
'Returns if its valid range
If ((row + offsetrow) > 0) And ((col + offsetcol) > 0) Then
isValidRng = True
Else
isValidRng = False
End If
End Function