I've been working on a macro for a client. Need to insert columns based on cell values.
I use the same code for each value:
ws.range(getcolumn(currentColumn + 1) & ":" & getcolumn(currentcolumn + 3)).EntireColumn.Insert
That works fine until I hit column 50, and then I get a runtime Error of 1004 that says "Method Range of Object Worksheet Failed"
Why am I getting this error?
Here is the getColumn() function:
Function getColumn(columnNumber As Integer) As String
Dim alphaNumber As Integer
Dim iRemainder As Integer
alphaNumber = Int(columnNumber / 27)
iRemainder = columnNumber - (alphaNumber * 26)
If alphaNumber > 0 Then
getColumn = Chr(alphaNumber + 64)
End If
If iRemainder > 0 Then
getColumn = getColumn & Chr(iRemainder + 64)
End If
End Function
Looks like the problem is in your getColumn() function.
When currentcolumn = 50 the getColumn is returning "A[" because iRemainder is 27 so your looking for Chr(91) which is [
Taken from this post try this:
Function getColumn(columnNumber As Integer) As String
If columnNumber < 27 Then
getColumn = Chr(64 + columnNumber)
Else
getColumn = getColumn((columnNumber - 1) \ 26) & getColumn((columnNumber - 1) Mod 26 + 1)
End If
End Function
I did a basic test using this sub:
Sub insertCol()
Set ws = Sheets("Sheet1")
For currentcolumn = 1 To 60
ws.Range(getColumn(currentcolumn + 1) & ":" & getColumn(currentcolumn + 3)).EntireColumn.Insert
Next currentcolumn
End Sub
Related
I am trying to punch a formula into a cell, referencing a function ConvertToLetter to do so (seen in my code). However, running ConvertToLetter doesn't seem to work, returning the titular error when I try to run it with a highlight on "firstVideoRow". My thought is that this program is not returning a string, but that's just a guess.
I've already tried to initialize the ConvertToLetter variables elsewhere, but that doesn't work either.
Function ConvertToLetter(iCol As Long) As String
Dim iAlpha As Long
Dim iRemainder As Long
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function
...
Range(Cells(lastRow + 3, 4)).Formula = "=AVG(" & ConvertToLetter(firstVideoRow) & CStr(4) & ":" & ConvertToLetter(lastRow) & CStr(4) & ")"
ConvertToLetter wants a Long by reference. Apparently you did not declare firstVideoRow and/or lastRow as Long, so you get the error.
You could ask for a Long by value so that it is implicitly converted for you:
Function ConvertToLetter(ByVal iCol As Long) As String
But actually what you want to do is switch to the R1C1 notation:
Cells(lastRow + 3, 4).FormulaR1C1 = "=AVG(R4C" & firstVideoRow & ":R4C" & lastRow &")"
I am trying to copy the selected combobox values into the adjacent cell, when I code for the same I am getting
Run time error 5.
Private Sub CommandButton1_Click()
Dim projworkbook As Workbook
Dim page1 As Worksheet
Dim lColumn As Long
Dim CopiedColName as String
Set projworkbook = ActiveWorkbook
Set page1 = projworkbook.Worksheets("Project_Creation")
lColumn = page1.Cells(13, Columns.Count).End(xlToLeft).Column 'Getting the last used column number
If lColumn > 26 Then
CopiedColName = Chr(Int((lColumn - 1) / 26) + 64) & Chr(Int((lColumn - 1) Mod 26) + 65) 'Converting the col number to col name
Else
CopiedColName = Chr(lColumn + 64)
End If
Me.Cells("CopiedColName" & 4).Text = Me.ComboBox1.Text '-> I am getting run time error at this line
End Sub
use:
Me.Range(CopiedColName & 4).Value = Me.ComboBox1.Text '-> I am getting run time error at this line
Unfortunately my collaegue is currently on vacation and I'm sitting here with his VBA-Code, where I always trigger the run-time 13 Error. Any Help? The Error-Code is marked with **
Function checkForMapping(sheetName As String, checkColumn As Integer, displayColumn As Integer, firstRow As Integer, text As String) As Boolean
Worksheets(sheetName).Activate
Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Dim values As String
Dim currentRow As Long
Dim currentValue As String
For currentRow = firstRow To lastRow Step 1
**If Not Round(Cells(currentRow, checkColumn), 1) = Round(1, 10) Then**
currentValue = Cells(currentRow, displayColumn)
If Not InStr(values, currentValue) > 0 Then
If Len(values) > 0 Then
values = values & ", "
End If
values = values & currentValue
End If
End If
Next
checkForMapping = (Len(values) > 0)
If checkForMapping Then
result = MsgBox(text & " " & values, (vbOKOnly + vbInformation), "Info")
End If
Thanks for any help!
You could maybe add the following line before:
If Not IsError(Cells(currentrow, checkcolumn)) Then
and then remember to stick in another
End If
after
what if using:
If Left(Cells(currentRow, checkColumn).Value2, 1) <> "1" Then
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
I am using a function that I copied from Microsoft that takes a column number and reassigns the column letters. I need to do this to create a formula. I have researched all day and cannot pinpoint the cause of my errors (I also tried to accomplish this as a sub proc). The function is in its own module. I tested it and it works fine:
Function ConvertToLetter(ByRef iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function
The code that is giving me an error is:
For groups = 1 To i ' level 1 grouping
For iCol = 24 To 136
rCol = ConvertToLetter(iCol)
Cells(Start(groups) - 1, rCol).Formula = "=COUNTA(" & rCol & Start(groups) & ":" & rCol & Finish(groups) & ")"
Next
Next
I tried substituting the function into the formula itself:
Cells(Start(groups) - 1, ConvertToLetter(iCol)).Formula = "=COUNTA(" & ConvertToLetter(iCol) & Start(groups) & ":" & ConvertToLetter(iCol) & Finish(groups) & ")"
The debugger made it past the first function call, but not the second & third. The errors I receive are types of "expecting a variable or procedure, not module." With the second case, I get other errors, and my head is so fuzzy, I cannot recall them.
Any help is greatly appreciated. I have run out of ideas. Thanks so much!
You almost never need to convert columns to letters. First consider using FormulaR1C1
For groups = 1 To i ' level 1 grouping
For iCol = 24 To 136
lLast = Finish(groups) - Start(groups) + 1
Sheet1.Cells(Start(groups) - 1, iCol).FormulaR1C1 = _
"=COUNTA(R[1]C:R[" & lLast & "]C)"
Next iCol
Next groups
If you don't like R1C1, you can use Address more directly
For groups = 1 To i ' level 1 grouping
For iCol = 24 To 136
Set rStart = Sheet1.Cells(Start(groups), iCol)
Set rEnd = Sheet1.Cells(Finish(groups), iCol)
rStart.Offset(-1, 0).Formula = _
"=COUNTA(" & rStart.Address & ":" & rEnd.Address & ")"
Next iCol
Next groups
The function that you are using is faulty. Check this example
Sub Sample()
Dim iCol As Integer
For iCol = 131 To 134
Debug.Print iCol; ConvertToLetter(iCol)
Next iCol
End Sub
Function ConvertToLetter(ByRef iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function
Output
131 D[
132 D\
133 D]
134 D^
Use this code which I picked up from the link that I mentioned.
rcol = Split(Cells(, iCol).Address, "$")(1)
instead of
rCol = ConvertToLetter(iCol)
The following function does conversion of column number to letters: it takes advantage of the .Address method to simplify life a lot.
Function convertToLetter(colnum)
mycell = [A1].Offset(0, colnum - 1).Address
convertToLetter = Mid(mycell, 2, Len(mycell) - 3)
End Function
But now that I look at your code - when you use the Cells function, you should call it with numbers, not letters. So you have another problem!
Try the following:
For groups = 1 To i ' level 1 grouping
For iCol = 24 To 136
rCol = ConvertToLetter(iCol)
Cells(Start(groups) - 1, iCol).Formula = "=COUNTA(" & rCol & Start(groups) & ":" & rCol & Finish(groups) & ")"
Next
Next
Note I used iCol not rCol in the Cells(Start(groups)-1, iCol) = part of the statement. It may not be the only thing wrong...