I'm working on a macro that will change the default style of a worksheet back to "Normal", but I'm having trouble figuring out how to do so. I've tried a variety of different methods to accomplish this, and have also looked at the Microsoft documentation, but I can't see a specific way to change this setting programmatically. Can someone please assist? Thank you!
This is the format I'm working with:
' Set style for Worksheet
Sub SetSheetStyle(ws As Worksheet)
With ws
Selection.Style = "Normal"
End With
End Sub
Style is a Range property, so try something like:
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Cells.Style = "Normal"
I was able to get it with this syntax:
' Set style for Worksheet
Sub EWMBranding_SetSheetStyle(ws As Worksheet)
With ws
.Cells.Style = "Bad"
End With
End Sub
Related
I'm trying to ensure the UsedRange is accurate, having experienced it being substantially different to what is actually used in a worksheet. Having .UsedRange is meant to reset the used range to what is in use. I'm finding it doesn't unless structured in a very particular way.
ActiveSheet.UsedRange ' Works OK
Dim My_Sheet as Worksheet
Set My_Sheet = ActiveSheet
My_Sheet.UsedRange ' VBA compiler doesn't like this
#Geoff_B I don't have any issues. Do you mind sharing the screenshot? I tried different variations everything seems fine. unlikely the compiler confused with the expression.
Sub test()
Debug.Print ActiveSheet.UsedRange.Address ' Works OK
Dim My_Sheet As Worksheet
Set My_Sheet = Sheets(ActiveSheet.Name)
Debug.Print My_Sheet.UsedRange.Address
'=====================and=====================
Dim My_Sheet As Object
Set My_Sheet = ActiveSheet
Debug.Print My_Sheet.UsedRange.Address
End Sub
Thanks Tim Williams, dimming as Object has resolved it. Looks like a bug to me.
I currently have this code :
Sub FilterRows()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Sheets(ws).Range("A1").AutoFilter
Next
End Sub
I am trying to add a data filter to each sheet but I get type mismatch error.
I am new to this so any help would be great :)
When you are using the below code
For Each ws In ActiveWorkbook.Worksheets
Sheets(ws).Range("A1").AutoFilter
Next
here you are looping through each worksheet object, so you don't have to use
Sheets(ws).Range("A1").AutoFilter
this is the wrong approach. sheets(ws) is wrong instead use the below method
ws.Range("A1").AutoFilter 'more correct one
or
Sheets(ws.Name).Range("A1").AutoFilter 'less correct one
NB: make sure all of your worksheets have data on them.
I need my code to automatically change the font of my newly created hyperlink name and change it's other font properties so I need my code to detect when a new hyperlink is added to a worksheet.
"Worksheet_Change" and "Worksheet_FollowHyperlink" didn't help.
Update: Here is my code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WB As Workbook
Dim WS As Worksheet
Dim TA As ListObject
Dim TA_DateCol As ListColumn
Set WB = ThisWorkbook
Set WS = WB.Sheets(1)
Set TA = WS.ListObjects(1)
If Not Intersect(Target, TA.Range) Is Nothing Then
'This is the part which I want my code to detect if a new hyperlink
'is added but nothing happens my guess is it's because 'Target' type
'in this event handler is a 'Range' not a 'Hyperlink'
End If
End Sub
Any help and advise would be appreciated.The Picture of my code so far
Adding a hyperlink to a range does not trigger the worksheet_change event.
Therefore it is not able to detect this via code.
The solution is to update the hyperlink style of the workbook to your requirements.
There are quite a lot of tutorials that explain how to do that, e.g. https://support.microsoft.com/en-us/office/change-the-font-format-for-hyperlinks-672c2905-ad3e-40be-b281-811e68386243
I would like to use a CountIfs-function with two criterias. My programm should go through two different columns and compare the cells.
I used this code:
WorksheetFunction.CountIfs(Data!E:E;"=Open";Daten!Q:Q;"=company")
Could you please tell me if it is possible to use the CountIfs-function like that? Because my compiler is dropping a syntax-error.
Thanks a lot!
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Date")
Dim wksFn As WorksheetFunction
Set wksFn = Application.WorksheetFunction
Debug.Print wksFn.CountIfs(ws.Columns(5), "Open", ws.Columns(17), "company")
End Sub
it is possible to connect worksheet like so? const ws = me.textbox
I would like to make a drop down list with possibility to chose worksheets in my user form.
I know that method won't work i pasted it below.
Is there any other possibility to make it work?
I use select casebut it means that i write my whole code X times
Sub Populate()
Dim ws As Worksheet
Set ws = me.ChoseSheet
First assign text box value to string and then pass it to set
strSheetName = Me.ChoseSheet
Set ws = ThisWorkbook.Worksheets(strSheetName)
You could use a For each loop to populate a ComboBox on a UserForm
For example:
Public Sub UserForm_Initialize()
Dim ws as WorkSheet
For each ws in ActiveWorkbook.Worksheets
Combobox1.AddItem(ws.Name)
next ws
End sub