VBA on data source update I want to update a different sheets sort order - vba

I have this working thus far for the datasource sheet. But on another sheet called WatchList I want to update the sort order for 1 of the columns which does a look up when the data source updates. I tried several items and cant get it working.
As far as my range it starts at at the top of the sheet WatchList and goes to column L. And the sort I want to do is on column H.
Private Sub Worksheet_Activate()
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Sheet As Worksheet, Pivot As PivotTable
For Each Sheet In ThisWorkbook.Worksheets
For Each Pivot In Sheet.PivotTables
Pivot.RefreshTable
Pivot.Update
Next
Next
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("WatchList")
Dim rng As Range
Set rng = Sheet2.Range("A2:L69")
ws.Range(rng).Sort Key1:=Range("H2:H69"), Order1:=xlAscending, Header:=xlNo
End Sub
I am getting run-time error '1004';
Method 'Range' of object'_Worksheet' failed
Any help is much appreciated

Related

Copy/Paste Macro Pasting Randomly

I am having an issue copying and pasting a drop-down data validation menu from one sheet into all selected sheets. The drop-down menu seems to paste randomly instead of pasting into sheet "B22" of the selected sheets.
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
Selection.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = False
End Sub
Any suggestions on how to tackle this? I am having some difficulty finding the error in my code.
In case you want to work with the workbook and avoid using copy/paste..
Option Explicit
Sub Test()
Dim excel_sheet As Worksheet
Dim sht As Worksheet
Dim drop_down_value As Range
Set sht = ThisWorkbook.Sheets("Sheet2")
Set drop_down_value = sht.Range("B22")
'Sheets selection should be done before running macro
For Each excel_sheet In ThisWorkbook.Windows(1).SelectedSheets
excel_sheet.Range("B22").Value = drop_down_value.Value
Next
End Sub
Try the following, you need to loop through all selected Sheets instead of using Selection.Range:
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
For Each sht In ActiveWindow.SelectedSheets
sht.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = True
Next
End Sub

Private Sub User-Defined Type Not Defined Range Sheet

First post. I have the relatively simple code below and am getting a
User-defined type not defined
error. I know that the stand alone code works when I place it into one Sub but for various reasons I want to split it out so that in my larger workbook I can just call on the second sub rather than having to copy and paste the whole loop multiple times. The purpose of the code is to autosize the specified range in excel.
Sub letsGo()
Dim rng As Range
Dim sht As Worksheet
Set rng = ThisWorkbook.Sheets("Sheet1").Range("Range1")
Set sht = ThisWorkbook.Sheets("Sheet1")
Call whyDoesntThisWork(sht, rng)
End Sub
Private Sub whyDoesntThisWork(rangeSheet As Sheet, rangeTable As Range)
Dim Col As Range
Dim reSize As Range
For Each Col In rangeTable.Columns
If Col.Hidden = False Then
Set reSize = rangeSheet.Range(rangeSheet.Cells(rangeTable.Row, Col.Column), rangeSheet.Cells(rangeTable.Rows.Count, Col.Column)) reSize.Columns.autoFit
End If
Next Col
End Sub
You have two different data types:
Private Sub whyDoesntThisWork(rangeSheet As Sheet, rangeTable As Range)
rangeSheet is a Sheet, but when you call it, you pass:
Call whyDoesntThisWork(sht, rng)
sht is of type WorkSheet
That's your inconsistency. I recommend you change your definition to:
Private Sub whyDoesntThisWork(rangeSheet As WorkSheet, rangeTable As Range)
Change rangeSheet As Sheet to rangeSheet As Worksheet

VBA - Calling from another workbook

Trying to delete sheets based on data from another workbook sheet - By comparing and accessing data from another workbook sheet, however its not working. I was able to do it if the sheet was in the same workbook, however i do not want to import the worksheet every time.
Code so far, My problem is calling from another workbook sheet.
sub delete()
Dim wb As Workbook
Dim wks As Worksheet
Dim MyRange As Range
Dim Cell As Range
Set wb = Workbooks("name.xlsx")
Set wks = wb.Worksheets("allnames")
With wks
Set MyRange = wks.Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
End With
On Error Resume Next
Application.DisplayAlerts = False
For Each Cell In MyRange
Sheets(Cell.Value).Delete
Next Cell
Application.DisplayAlerts = True
On Error GoTo 0
End Sub
Thanks in advance
maybe you're after something like this:
Option Explicit
Sub delete()
Dim toDeleteSheetsWb As Workbook
Dim Cell As Range
Set toDeleteSheetsWb = Workbooks("WorkbookWithSheetsToDelete.xlsx") '<-- set the workbook whose sheets will be deleted (change "WorkbookWithSheetsToDelete.xlsx" to its actual name)
With Workbooks("name.xlsx").Worksheets("allnames") '<-- reference the worksheet from which to read worksheets names to be deleted in "WorkbookWithSheetsToDelete.xlsx" workbook
Application.DisplayAlerts = False
For Each Cell In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
toDeleteSheetsWb.Sheets(Cell.Value).delete
Next Cell
Application.DisplayAlerts = True
End With
End Sub

Excel VBA: Counting Data in Column from Another Workbook and Inputting Counter in Master Workbook

I need to create a macro in my CountResults.xlsm (Master Workbook) that solves the following problem. I have a column of data in another worksheet with either YES or NO. I need to come up with a macro that counts the amount of "YES" in the column. The column is located in Sheet2 of the workbook Test01.xlsx. Then take that count and put it in one cell in my CountResults.xlsm file. Like so:
I have a code that displays a count for a column in the same sheet. But this code does not count when there are 'breaks' in the column (empty spaces) like I have in my picture. This is that code:
Private Sub CommandButton1_Click()
MsgBox Range("A1").End(xlDown).Row
Range("A1").End(xlDown).Offset(1, 0).Select
End Sub
I have another code that helps with accessing another workbook and defining values for each workbook and worksheet:
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim shSource As Worksheet
Dim shTarget As Worksheet
Set wbSource = Workbooks.Open(Filename:="C:\Users\khanr1\Desktop\Test_Excel\Test03.xlsm", ReadOnly:=True)
Set wbTarget = ThisWorkbook
Set shSource = wbSource.Worksheets("Sheet2")
Set shTarget = wbTarget.Worksheets("Sheet1")
Use COUNTIF. It will give you the total even if the range is in another workbook. i.e. =COUNTIF([Book2.xlsx]Sheet2!$D$2:$D$9, "Yes"). Problem with having COUNTIF within your sheet as a formula is that you will need to open the other workbook if you want the count to be update. Below VBA code will perform an update for you. Assign the sub to a button in your CountResults.xlsm workbook
EDIT: Added row count as per OP's requirement
Sub UpdateResults()
Dim oWBWithColumn As Workbook: Set oWBWithColumn = Application.Workbooks.Open("<your Test01.xlsx address here>")
Dim oWS As Worksheet: Set oWS = oWBWithColumn.Worksheets("Sheet2")
Dim intLastRow as Integer: intLastRow = oWS.Cells(Rows.Count, "B").End(xlUp).Row
ThisWorkbook.Worksheets("<name of the sheet in your CountResults.xlsm workbook>").Range("<cell address>").Value = Application.WorksheetFunction.CountIf(oWS.Range("B2:B" & intLastRow), "yes")
oWBWithColumn.Close False
Set oWS = Nothing
Set oWBWithColumn = Nothing
End Sub

Repeating macro code

I recorded this macro:
Sheets("Sheet1").Select
Range("D4:E4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("ALB3").Select
Range("C1").Select
ActiveSheet.Paste
I want to make a loop to repeat the process.
From range D4:E4 to D200:E200 when do select
To paste that on respective sheet name from ALB3 to ALB196.
My data in sheet 1.
Column a is sheets name, column d4 and e4, is the data that I want to paste on every sheet already created.
If you're trying to copy a range from one sheet to another, you don't need a loop and you don't need to select. You can use copy syntax that doesn't use your clipboard.
Try this:
Sub CopyRangeToAnotherSheet()
Dim source As Worksheet
Dim target As Worksheet
Set source = ActiveWorkbook.Sheets("Sheet1")
Set target = ActiveWorkbook.Sheets("Sheet2")
source.Range("D4:E200").Copy target.Range("ALB3")
End Sub
To copy the source range to all sheets in the workbook except the source worksheet, try this:
Sub CopyToAllSheets()
Dim ws As Worksheet
For Each ws In Worksheets
CopyRangeToAnotherSheet (ws.Name)
Next
End Sub
Sub CopyRangeToAnotherSheet(targetName As String)
Dim source As Worksheet
Dim target As Worksheet
Set source = ActiveWorkbook.Sheets("Sheet1")
Set target = ActiveWorkbook.Sheets(targetName)
If target.Name <> source.Name Then
source.Range("D4:E200").Copy target.Range("ALB3")
End If
End Sub