question

Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 asked Tirrazo-5829 answered

Problem with VBA in Excel version 2203 & 2204 64bit

Hi,

I have an issue with a VBA code in Excel version 2203 & 2204.
On another PC I have version 2102 of Excel, the code works fine.

What do I do, I need it to work where I have the latest versions of Excel.
Can I go back from version 2203 to 2102 on that Excel, MS Office 365 account?

Shall I post the code here?


Regards

office-vba-dev
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You'll need to provide us more information before we can start to help you. "an issue" and "problem" don't tell us anything about what you're actually experiencing. Is it crashing? It is generating an error message? Is it just not running? Please provide us as much information as you can and perhaps someone can then help you.

0 Votes 0 ·

I thought maybe someone had experienced that VBA codes have certain bugs in the beta versions and would reply with that.

But the problem I am experiencing is that the code does not work. It is not crashing, not giving any error message.
Its not running as it should and I know how it looks like when it's running since it works on a computer with a former Excel version.

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered

Is this VBA code inside a macro? If so then security comes to mind as files must be in a trusted location before they can be run. See here.

The next thing to confirm is if the macro is being called at all. Open the macro and set a breakpoint on the first line. Then do whatever you need to do in your spreadsheet to trigger the code to run. If it doesn't run then either something has change to run it or security is getting in the way. If the breakpoint is hit then you can step through the code to try to determine what is failing.

If you cannot figure it out then please provide the steps we need to replicate the issue including how to set up the VBA code to run (clicking a button, etc) and the code itself.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered cooldadtx commented

Hi,


Thank you very much for your reply.
I am not able to create these codes myself so I would appreciate if you could test it. The function is also combined with 2 conditional formatting rules.

The solution I was after was to highlight the row and column from the cell I am standing at. This help me to see through large pages of similar numbers when you go through certain types of spreadsheets.

This is the VBA inserted in 'Thisworkbook' module:

Private Sub Workbook_Open()

End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim TargetRange As String
Dim TargetRow As String
Dim TargetCol As String

 If Not Intersect(Target, Range("D9:DV512")) Is Nothing Then
     Range("D9:DV512").FormatConditions.Delete
     TargetRow = Cells(Target.Row, "D").Address & ":" & Cells(Target.Row, "DV").Address
     TargetCol = Cells(9, Target.Column).Address & ":" & Cells(300, Target.Column).Address
     TargetRange = TargetRow & "," & TargetCol
     With Range(TargetRange)
         .FormatConditions.Add _
             Type:=xlExpression, _
             Formula1:="TRUE"
         With .FormatConditions(1)
             .StopIfTrue = False
             .Interior.ColorIndex = 15 'change to suit from dropdown
         End With
         .FormatConditions.Add _
         Type:=xlExpression, _
         Formula1:="TRUE"
     End With
 End If

End Sub



Regards Kristian198024-false.png197987-true.png



false.png (12.0 KiB)
true.png (12.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your script works for me but there are caveats.

1) For security reasons you need to ensure you save your Excel file with macros enabled. That is a different file extension. Save the file with the file type Excel Macro-Enabled Workbook otherwise macros are not going to run.
2) The script itself only highlights the rows D9+ because of the If statement at the top of it. So if you select any column from D on and row 9 on then it highlights the column and the row.

0 Votes 0 ·
Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered

Hi,

Thank you for testing it out.

I am aware of the different type of workbook for macro enabled files. I have other macroes that work on the same computer, different workbooks.
That is the weird thing.

I have 3 different computers with 3 different versions of Excel. The two beta versions will not run this macro (the row and column will not highlight) but the PC without the beta version will run it.

Regards

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered cooldadtx commented

Can I ask what version of Excel you have tried it on?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm on 2203. Switch the file to enable macros and running your code works correctly for me for the selection change function. The formatting I cannot answer to as I don't know what you're using but the function handles the selection of the rows/columns so I don't know that the formatting would matter.

0 Votes 0 ·
Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered cooldadtx commented

So you use the same version as me.

What would you recommend me to do?

I have another macro that is supposed to do the same without conditional formatting.
That works, the only problem is that the VBA removes all the colors of the spreadsheet.

Could you see if you can do anything with that code?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The macro does that because of this line: Range("D9:DV512").FormatConditions.Delete. That line is removing all conditional formatting from the given range. This includes the formatting it adds and any existing formatting. You cannot remove that otherwise every selection adds more format conditions.

I think you're going to have to come up with a conditional format rule that solves this issue a different way. One approach that comes to mind is to use just conditional formatting (no macro) and if the currently selected column = the column being formatted OR the currently selected row = the row being formatted then highlight the row.

Create a conditional formatting rule for the worksheet (or subset of data you want to select). Set the formula to =OR(CELL("col")=COLUMN(), CELL("row")=ROW()). Set the formatting the way you want. The formula says to format the cell if the cell's column/row matches the current column/row that is selected. This is equivalent to what you were doing before.

Unfortunately Excel won't recalculate stuff unless there is a change so keep the macro you had but change it to force a recalculation when selection changes.

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  If Application.CutCopyMode = False Then
     Application.Calculate
  End If
End Sub


Credit here for calculate - https://trumpexcel.com/highlight-active-row-column-excel/



0 Votes 0 ·
Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered

Hi,

thank you very much for this.

This is actually the same conditional formatting rule + VBA I have been using the last 2 years.
I got it from The Excel Forum.

The only problem with it was that it started to make the workbook slow and moving around became almost impossible without the sheet lagging.

That's why I tried to find another way to solve it.


This VBA without formatting is working, but this one is removing all colour. That is a problem.

VBA:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim rRow As Range, rCol As Ranges
If Range("A1") = "" Then
Range("A1") = Target.Address
End If

 With Range(Range("A1"))
     .EntireColumn.Interior.ColorIndex = 0
     .EntireRow.Interior.ColorIndex = 0
 End With

 Target.EntireRow.Interior.ColorIndex = 15
 Target.EntireColumn.Interior.ColorIndex = 15
 Range("A1").Value = Target.Address

End Sub

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered cooldadtx edited

Hi again,


What could be the reason that the highlighting VBA is not highlighting in one computer with the latest Excel.
But with an earlier version it does.


Regards
Kristian

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As you mentioned ealier, it was working right? You said it worked but it was getting slow. That is because you're telling Excel to refresh every time you change selection. The more cells in use the slower the process gets. I suspect that if you moved this code to a small spreadsheet it would work again. The Application.Calculate which is necessary to get Excel to refresh everything is a perf issue.

I don't know a way around this. You could limit the refresh to just the visible cells on the screen which would restore the performance but then you'd need to refresh every time the visible cells change. That could be a lot more work. You could also limit the calculate to just the current worksheet but, again, you'd then need to rerun the calculation whenever the worksheet changed.

Another alternative is to return to the code you had where you were deleting the format conditions and adding new ones but this time only delete format conditions for the previously selected row/column (not sure how to figure this out) and then only the formula that matches the one you're inserting (the one I posted earlier). You would therefore leave the existing formatting in place.

0 Votes 0 ·
Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered cooldadtx commented

The first one I had was working for a long time. And I built my workbook bigger and that's probably why it got slower.

You are correct. I have the same code in a small spreadsheet for another thing I am working on and that is working smooth.

I am not able to configure these myself. Got help from the Excel forum, but they have no good solution either for this.
Lastly someone came up with the VBA that doesn't work on my computer and that's annoying.

Would it be possible to instead of highlighting the row, underline + right line from where you are? Like a cross that's following the marked cell.
Since the sheet is frozen both ways, that's why it's important that the lines go all the way from the beginning of the sheet and from the top, not just around the cell.
This is for me to better navigate when I move out from a part of the sheet that is frozen to work with numbers far on the right side.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Anything involving formatting is going to have the same issue. The problem isn't the formatting but that call to Application.Calculate which is necessary to trigger the painting of the other rows. The only way to optimize this is to eliminate the need for that call. That's why I said you could try to figure out how to repaint just the visible rows but I don't know how you'd do that.

0 Votes 0 ·
Tirrazo-5829 avatar image
0 Votes"
Tirrazo-5829 answered

Where do you see this: Application.Calculate ?

I have got another code that will not start. Can you see anything wrong?
It is supposed to draw underline and right line.

When I paste it in my book it won't draw any lines.


1) To ThisWorkbook code module

Copy to clipboard
Private Sub Workbook_Open()
If ActiveSheet Is Sheets("ark1") Then Run "sheet1.worksheet_activate"
End Sub


2) To Ark1 sheet code module

Copy to clipboard
Option Explicit

Private myCell As Range

Private Sub Worksheet_Activate()
Set myCell = ActiveCell
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not myCell Is Nothing Then
myCell.EntireRow.FormatConditions.Delete
myCell.EntireColumn.FormatConditions.Delete
End If
With ActiveCell
With .EntireRow.Resize(, .Column)
.FormatConditions.Delete
.FormatConditions.Add 2, , "=row()=" & .Row
With .FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = 2
End With
End With
With .EntireColumn.Resize(.Row)
.FormatConditions.Delete
.FormatConditions.Add 2, , "=column()=" & ActiveCell.Column
With .FormatConditions(1).Borders(xlRight)
.LineStyle = xlContinuous
.Weight = 2
End With
End With
Set myCell = .Cells
End With
End Sub

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.