Select method of the Range class
Solved
Hello everyone,
I have a VBA code that allows me to create a chart based on the number of occurrences of each entry.
Some entries have a zero occurrence, and I would like them not to appear in my chart.
To do this, before creating the chart, I added the following lines of code that allow me to hide the rows for which the occurrence is zero.
This code works perfectly when it's in a separate module, but when I add it to my main code that is activated by clicking a button, it no longer works...
I get a runtime error 1004 that says "the Select method of the Range class failed" ... The debugger indicates that this line is causing an issue:
I tried replacing
with
but that doesn't work either...
Any idea why it works in a module and not in the global code?
Thanks in advance!
Configuration: Windows 7 / Chrome 53.0.2785.116
I have a VBA code that allows me to create a chart based on the number of occurrences of each entry.
Some entries have a zero occurrence, and I would like them not to appear in my chart.
To do this, before creating the chart, I added the following lines of code that allow me to hide the rows for which the occurrence is zero.
''''''''''''''''''''''Hiding zero occurrences'''''''''''''''''''''''''''' line2 = 27 Do Until IsEmpty(Cells(line2, "B")) If Cells(line2, "B").Value = 0 Then Rows(line2).Select Selection.EntireRow.Hidden = True line2 = line2 + 1 Else line2 = line2 + 1 End If Loop
This code works perfectly when it's in a separate module, but when I add it to my main code that is activated by clicking a button, it no longer works...
I get a runtime error 1004 that says "the Select method of the Range class failed" ... The debugger indicates that this line is causing an issue:
Rows(line2).Select
I tried replacing
Rows(line2).Select
with
Range(line2:line2).Select
but that doesn't work either...
Any idea why it works in a module and not in the global code?
Thanks in advance!
Configuration: Windows 7 / Chrome 53.0.2785.116
7 answers
-
Hello thread, hello forum,
Not much time to look at all the codes... But just one remark. In module 5, procedure graph_def_risque, you have, if I counted correctly, 2 sets of 66 variables. I recommend replacing those 66 variables with an array of variables with 66 entries. I used an array with 2 rows and 66 columns because I wasn't sure if you would need the variable names later.
In the first row are stored the names of the variables and in the second the values. I named TBCCO the array of cco variables and TBR the array of R variables. It looks like this:Dim TBOCC(1, 1 To 66) As Variant Dim TBR(1, 1 To 66) As Variant TBOCC(0, 1) = "occ_101" TBOCC(1, 1) = Application.CountIf(Worksheets("High risks").Range("G:G"), "101") TBOCC(0, 2) = "occ_102" TBOCC(1, 1) = Application.CountIf(Worksheets("High risks").Range("G:G"), "102") '... TBOCC(0, 66) = occ_1605 TBOCC(1, 66) = Application.CountIf(Worksheets("High risks").Range("G:G"), "1605") TBR(0, 1) = "r101" TBR(1, 1) = "slippery ground" TBR(0, 2) = "r102" TBR(1, 1) = "uneven ground" '... TBR(0, 66) = "r605" TBR(1, 66) = "coactivity"
For now, you're not gaining anything, but later you will remove all the lines:Cells(27, "A") = r101 '... Cells(92, "B") = occ_1605
with a single loop:For X = 1 To 66 Cells(26 + X, "A") = TBR(1, X) Cells(26 + X, "B") = TBOCC(1, X) Next X
if you don't need the variable names, it simplifies like this:Dim TBOCC(1 to 66) As Variant Dim TBR(1 To 66) As Variant TBOCC(1) = Application.CountIf(Worksheets("High risks").Range("G:G"), "101") TBR(1) = "slippery ground" '... TBOCC(66) = Application.CountIf(Worksheets("High risks").Range("G:G"), "1605") TBR(66) = "coactivity"
the loop becomes:For X = 1 To 66 Cells(26 + X, "A") = TBR(X) Cells(26 + X, "B") = TBOCC(X) Next X
See you,
ThauTheme
--
See you,
ThauTheme-
-
[UPDATE]
I've pushed the idea of tables even further to avoid having too many lines to write, this is what it looks like, and it considerably reduces (to say the least) the length of the code !!
Dim TB_R(1 To 66) As Variant Dim TB_CODE(1 To 66) As Variant Dim TB_OCC(1 To 66) As Variant '''Recording the names of risks in the TB_R array''' For i = 1 To 66 TB_R(i) = Sheets("Risks").Range("E" & i + 2) Next '''Recording all the risk codes in the TB_CODE array''' For i = 1 To 66 TB_CODE(i) = Sheets("Risks").Range("D" & i + 2) 'from cell D3 of the "Risks" sheet Next '''Counting occurrences of risk codes in "High Risks" and recording in the TB_OCC array''' For i = 1 To 66 TB_OCC(i) = Application.CountIf(Worksheets("High Risks").Range("G:G"), TB_CODE(i)) Next '''Assigning risks and occurrences to cells for chart creation''' Sheets("Graph Summary").Activate For X = 1 To 66 Cells(26 + X, "A") = TB_R(X) Cells(26 + X, "B") = TB_OCC(X) Next X
Thanks for the tip !! -
Hello everyone!
I have a little problem with my document that compiles just fine (!!) but when I try to reopen it, I get an error message that says:
"Excel has found unreadable content in "Document_Name". Would you like to recover the contents of this workbook? If the source of this workbook is trustworthy, click Yes."
I click Yes, and a "Repairs" window opens telling me:
"Deleted records: Sort in part /xl/tables/table17.xml (Table)".
Except that I can't figure out which table corresponds to "table17", and I can't spot any deleted sort anywhere...
So question: is this due to my save format, or does it happen on your computers too? And how can I change that?
The file HERE: http://www.cjoint.com/c/FIEm3UNFs14
Thanks for your responses!
-