Pablo Espressobar
New Member
- Joined
- Nov 15, 2017
- Messages
- 6
Hello Everyone,
I'm trying to write an excel macro that sets the Orientation of the 2nd Row of a column to "90" where i have a selected cell and back to "0" when i'm selecting a cell in another column.
So for example:
The orientation in Row2 is "90" in all Rows. Then i'm clicking into "C8". This triggers my macro and it changes the Orientation of "C2" to "0". Then when i'm clicking into "E6", the Orientation of "C2" changes back to "90" and the Orientation of "E2" changes to "0".
My code gives the following Error Message: Run-time error '1004': Method 'Range' of object'_Worksheet' failed
Sorry for the long introduction, but i want to make sure you know everything.
Here is my current code: (Error line in red)
Private lastColumn As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim column As String
Dim finalColumn As String
Dim isNum As Boolean
Dim length As Integer
Dim i As Integer
column = Target.address
column = Replace(column, "$", "")
length = Len(column)
For i = 1 To length
If Not (IsNumeric(Mid(column, i, 1))) Then
finalColumn = finalColumn & Mid(column, i, 1)
End If
Next i
If finalColumn <> lastColumn Then
Range(finalColumn & "2").Orientation = 0
Range(lastColumn & "2").Orientation = 90
End If
lastColumn = finalColumn
End Sub
Thanksfully,
Pablo
I'm trying to write an excel macro that sets the Orientation of the 2nd Row of a column to "90" where i have a selected cell and back to "0" when i'm selecting a cell in another column.
So for example:
The orientation in Row2 is "90" in all Rows. Then i'm clicking into "C8". This triggers my macro and it changes the Orientation of "C2" to "0". Then when i'm clicking into "E6", the Orientation of "C2" changes back to "90" and the Orientation of "E2" changes to "0".
My code gives the following Error Message: Run-time error '1004': Method 'Range' of object'_Worksheet' failed
Sorry for the long introduction, but i want to make sure you know everything.
Here is my current code: (Error line in red)
Private lastColumn As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim column As String
Dim finalColumn As String
Dim isNum As Boolean
Dim length As Integer
Dim i As Integer
column = Target.address
column = Replace(column, "$", "")
length = Len(column)
For i = 1 To length
If Not (IsNumeric(Mid(column, i, 1))) Then
finalColumn = finalColumn & Mid(column, i, 1)
End If
Next i
If finalColumn <> lastColumn Then
Range(finalColumn & "2").Orientation = 0
Range(lastColumn & "2").Orientation = 90
End If
lastColumn = finalColumn
End Sub
Thanksfully,
Pablo