Excel vba EntireRow.AutoFit does not work - excel

i have the following code in an add-in, which is called from the ribbon. It is meant to make all lines visible from the current line to the next visible line. The lines are hidden by an autofilter.
Sub Details4(control As IRibbonControl)
ActiveSheet.Unprotect Password:="xxx"
SendKeys "+{DOWN}+ ", True
ActiveCell.Rows("1:" & Selection.Rows.Count).EntireRow.EntireRow.AutoFit
End Sub
It does mark the lines as i wanted it to do, but the auto-fit does not work. When I double-click the line-height (which should do the same as the autp-fit) alle lines are visible as I wished this do to.
Can anyone see a problem? I know "sendkey" is not the best Thing in programming, but I had no other easy idea how to mark the range up to the next visible line...
Thanks
max

You have an extra .EntireRow call.
Rows("9:13").EntireRow.autofit
Works just fine.
For your sendkey issue, try something like this:
Range(Selection, Selection.End(xlDown)).Select

solved this by changing the code to:
For Each Row In ActiveCell.Rows("1:" & Selection.Rows.Count)
Row.EntireRow.AutoFit
Next
and deleting the line
ActiveCell.Rows("1:" & Selection.Rows.Count).EntireRow.EntireRow.AutoFit
of course...

Related

Error 438 for VBA Word when using .selectrow, does VBA not reconize open Word?

I am trying to manipulate Word tables from Excel. The general problem I am encountering has already been addressed by Microsoft here:
https://learn.microsoft.com/en-us/office/vba/word/concepts/customizing-word/error-accessing-a-table-row-or-column
tldr: Merged Cells in Word Tables require a work around to select entire rows to delete.
I used and customized the following code snippet:
Sub RemoveTableBorders()
ActiveDocument.Tables(1).Cell(1, 1).Select
With Selection
.SelectRow
.Cells.Borders.Enable = False
End With
End Sub
However I get an 438 Error, pointing to the .SelectRow command. VBA tells me that this is not a valid command for an object. It feels like when I use the With Selection command, Excel does not recognize that I am trying to write to word, as .selectrow is a Word command. Therefore I am trying to include another pointer in the selection like this:
Sub RemoveTableBorders()
ActiveDocument.Tables(1).Cell(1, 1).Select
With ActiveDocument.Selection
.SelectRow
.Cells.Borders.Enable = False
End With
End Sub
I can not provide reproducible code.
I think the answer to the following question also solves my problem: If I have two open instances of Office programs, how can I tell which selection (Excel or Word) should be manipulated?
The Mistake was indeed that VBA did not regonize the Word Selection. With the following adjustment in the With call the code works:
Sub RemoveTableBorders()
ActiveDocument.Tables(1).Cell(1, 1).Select
With Word.application.selection
.SelectRow
.Cells.Borders.Enable = False
End With
End Sub
Thanks for your help.

scroll at the left when first open excel-macro

I have a macro and it perfectly run. however, the result is always shown at the right (latest column).
I tried to add the macro:
select.range("A1")
or
Application.Goto Reference:="R1C1"
and it still is not working.
Is there anyone know how to make the scroll bar at the left (column A) when first open?
I solved this issue.
I add this line "ActiveWindow.LargeScroll ToRight:=-1"
thank you
You're missing Scroll:=True from your code, to scroll the worksheet to the activated range.
Try this:
Application.Goto Reference:=Sheet1.Range("A1"), Scroll:=True

How to deal with autofilter using vba

enter image description here
I am having trouble with below line codes at line ActiveSheet.ShowAllData because at times my worksheet has the auto filter on and at times off. Is there a way to cater for this ??
  
Consider:
Sub Framm()
With ActiveSheet
If (.AutoFilterMode And .FilterMode) Or .FilterMode Then
.ShowAllData
End If
End With
End Sub
Note that this does not actually remove autofilters, only removes any de-selections. It will also:
not barf if all data is already showing
not barf if filtering not is present.
It's so much easier if you paste the code straight into your question rather than display a picture - can't copy and paste a picture into the VBE.
You need to check if anything's filtered before clearing the filter:
If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData

Excel: Copy Row to New Tab if Cell Contains Value

I'm a complete novice when it comes to Excel, I've Googled it and it's come up with some responses that I don't understand in the slightest. Most of which are things I need to do in Visual Basic?
Essentially what I want is to set up several tabs for individual users, that generate from the main tab. All of which are set up already.
So IF Row R on Main Sheet has NO (Persons Initials)
Copy this row to the Nick tab (There will be 20+ rows for each user, not sure if that changes anything)
Any help would be incredible and massively appreciated!
Thanks,
Nick
You can probably get a start by recording a macro. Here's a link on how to do that: http://office.microsoft.com/en-us/excel-help/create-or-delete-a-macro-HP010342374.aspx#BMrecordmacro. After you click record, just start doing what you want done manually. That will show you some of the syntax. In this case, it will probably be easiest for you to format your summary data as a table, filter it by each person's initials and then copy the results to the individual tabs. Your recorded code is probably going to look something like this:
Sub CopyNO()
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"NO"
Range("Table1[#All]").Select
Selection.Copy
Sheets("NO").Select
Range("A2").Select
ActiveSheet.Paste
End Sub
To clean up the code you record, you'll want to try to change anything that says ActiveSheet to something like Sheets("SheetName") and, ideally, get rid of any .Select commands by instead using whatever is actually done with the selection. Here you might end up with something like:
Sub CopyNO()
Sheets("Summary").ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"NO"
Worksheets("Summary").Range("Table1").Copy _
Destination:=Worksheets("NO").Range("A5")
End Sub

VBA to click a button in a excel sheet

i need to click a button(FormControl) on an excel sheet and run the macro assigned to it, through VBA code.
I tried what was suggested here
https://stackoverflow.com/questions/130166/clicking-command-button-from-other-workbook
but it didn't work for me.
Is there any other way to do this ??
Fairly easy method for doing this, even if it's not really documented.
CommandButton1.Value = True
Did you actually try? Because you want exaclty what is suggested there.
Application.Run Activesheet.Shapes(1).OnAction
If in a worksheet. I used this in the past and it worked!
Sheet1.CommandButton1_Click
else
Sheets("Sheet Name").CommandButton1_Click
In my case, I needed to add a button, say on Sheet1, to call the code of CommandButton1 on, say, Sheet2. First I had to delete the 'Private' statement that was in front of CommandButton1_Click(). Then I had to call Worksheets("Sheet2").Activate before calling Worksheets("Sheet2").CommandButton1_Click. Then it worked. You can then add Worksheets("Sheet1").Activate to get you back to your original sheet. So:
Private Sub CommandButton1_Click()
Worksheets("Sheet2").Activate
Worksheets("Sheet2").CommandButton1_Click
Worksheets("Sheet1").Activate
End Sub

Resources