VBA assistance - wh...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] VBA assistance - why doens't this work??

7 Posts
4 Users
0 Reactions
51 Views
Posts: 11884
Full Member
Topic starter
 

I'm writing a workbook with an input sheet, which transfers text and other data over to the first blank row of a log sheet. That bit works fine. I then want to put borders around the cells of the new data, but the selecting the cells to format fails every time despite using the same technique that I've used to copy data from one sheet to another - any ideas?

The bold row fails, reporting a Run-time error 1004, Select method of range class failed. Ultimately I want to select cells A to O on the 'num' row, but even a single selection fails.

ActiveWorkbook.Sheets("Local Action Log").Range("L" & num).Value = ActiveWorkbook.Sheets("New Action").Range("O10").Value
ActiveWorkbook.Sheets("Local Action Log").Range("M" & num).Formula = "=(K" & num & "*L" & num & ")"

'Formatting in new action line. Borders, wrap text and cell shading.
[b] ActiveWorkbook.Sheets("Local Action Log").Range("A" & num).Select
[/b] Selection.Borders(xlDiagonalDown).LineStyle = xlNone

Flummoxed. I've tried all sorts of combinations


 
Posted : 06/12/2017 10:34 am
Posts: 10761
Full Member
 

Why not just set the border directly without doing the select first?


 
Posted : 06/12/2017 10:44 am
Posts: 11884
Full Member
Topic starter
 

Yep, tried that. The first step in my VBA is to count previous used rows, which generates the 'num' function to define which blank row on the local action log sheet to populate.

Any kind of formatting applied first, even borders makes the VBA count that row as used. In that case if I have 300 bordered rows, even with no text in them the data goes into row 301.


 
Posted : 06/12/2017 11:09 am
Posts: 10315
Full Member
 

I'm not sure why you can't set it directly. You have already calculated 'num' so it should already be pointing at the correct line. You then just set the border directly

With ActiveWorkbook.Sheets("Local Action Log").Range("A" & num)
.Borders(xlDiagonalDown).LineStyle = xlThin
'Whatever other formatting you want to do

End With

I suspect we are missing an actual step taken in your efforts to describe the problem succinctly.


 
Posted : 06/12/2017 12:13 pm
Posts: 13594
Free Member
 

Yep, tried that. The first step in my VBA is to count previous used rows, which generates the 'num' function to define which blank row on the local action log sheet to populate.

Any kind of formatting applied first, even borders makes the VBA count that row as used. In that case if I have 300 bordered rows, even with no text in them the data goes into row 301.

easy, fix, just loop through rows looking for last non blank one...


 
Posted : 06/12/2017 12:28 pm
Posts: 11884
Full Member
Topic starter
 

leffeboy - with a bit of manipulation, that worked! Marvellous. Cheers.


 
Posted : 06/12/2017 1:09 pm
Posts: 10315
Full Member
 

Excellent 🙂


 
Posted : 06/12/2017 1:13 pm
Posts: 10761
Full Member
 

For info using Select is generally a great way of slowing code down and making it longer. It's rarely needed.


 
Posted : 06/12/2017 1:22 pm

6 DAYS LEFT
We are currently at 95% of our target!