I would like my workbook to open on the cell of today's date. The dates of 2017 are listed in row 10. My macro is listed below but contains a syntax error on the line
'x = Format(Date, "Short Date")'
Private Sub Workbook_OpenDate()
Dim wb As Workbook, wb1 As Workbook
Dim LastRow As Long
Set wb = Workbooks("2017 Capacity Planner.xlsm")
Worksheets("Dashboard").Select
x = Format(Date, "Short Date")
On Error Resume Next
Worksheets(“Dashboard”).Row(10).Find(What:=x, LookIn:=xlValues).Activate
Application.Goto Selection, True
End Sub
You are still using magic quotes. Even if you remove that you will Error 438 on the line .Row(10). Then on the find another error is waiting when nothing is found.Change your code like this :
Sub test()
Dim rng As Range
Dim x As String
Dim wb As Workbook, wb1 As Workbook
Dim LastRow As Long
Set wb = Workbooks("2017 Capacity Planner.xlsm")
Worksheets("Dashboard").Select
x = (Format(Date, "Short Date"))
Set rng = Worksheets("Dashboard").Rows(10).Find(What:=x, LookIn:=xlValues)
If Not rng Is Nothing Then
rng.Parent.Activate
rng.Select
End If
End Sub
Related
I am having the error "Copy Method of Worksheeet class failed" on this line:
.Sheets("Blank Forecast Sheet").Copy After:=.Sheets("Button Sheet")
I've looked around and couldn't find any solution. This code is ,as can be seen, supposed to add new sheets that I will rename, once, I get this problem solved.
Sub addnewsheet()
Dim wbook As Workbook
Set wbook = Application.ActiveWorkbook
Dim newsheet As Worksheet
Dim datasheet As Worksheet
Dim m As String
Dim y As Integer
m = Format(Date, "mmmm")
y = Format(Date, "yyyy")
With wbook
.Sheets("Blank Forecast Sheet").Copy After:=.Sheets("Button Sheet")
End With
End Sub
Use ThisWorkbook instead of wbook and .Worksheets instead of .Sheets:
Sub addnewsheet()
Dim newsheet As Worksheet
Dim datasheet As Worksheet
Dim m As String
Dim y As Integer
m = Format(Date, "mmmm")
y = Format(Date, "yyyy")
With ThisWorkbook
.Worksheets("Blank Forecast Sheet").Copy After:=.Worksheets("Button Sheet")
End With
End Sub
i have been assigned to use the .match function in vba, to compare 2 different columns in 2 different workbooks.
here is my code so far.. how do i use the match function to my goal ?
Sub Ob_match()
Dim swb As Workbook, dwb As Workbook
Dim sws As Worksheet, dws As Worksheet
Dim oCell As Range, oMatch As Range
Set swb = ActiveWorkbook
Set sws = swb.Sheets("Item")
Set dwb = Workbooks.Open(swb.Path & "\EPC_EndItem.xlsm", ReadOnly:=True)
Set dws = dwb.Sheets("Data")
If Not oMatch Is Nothing Then
oCell.Offset(0, 1) = "Y"
Else
oCell.Offset(0, 1) = ""
End If
Next oCell
MsgBox "Processing completed"
End Sub
To run this code you should be on your your first workbook and second work-book should be open in background, I find this as an easier method than to call workbook using it's address, you may change that if you like
Sub vl()
Dim lastrow As Long
Sheets("Items").Select
lastrow = Range("B" & Rows.Count).End(xlUp).Row
Range("C2:C" & lastrow).Formula = "=IF(VLOOKUP(RC2,[Book2]Data!C4,1,FALSE), ""OK"","""")"
End Sub
Here I assumed that Name of your second book is Book2.
Change it to whatever it is in the code.
Hope this helps :)
why does this code run fine:
Sub SelectRange()
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim sourceSheetSum As Worksheet
Set sourceBook = ActiveWorkbook
Set sourceSheet = sourceBook.Sheets("Tabelle1")
ActiveWorkbook.Names.Add _
Name:="ggg", _
RefersTo:="=Sheet1!A4:L37"
sourceSheet.Select
sourceSheet.Range("A4:L37").Select
End Sub
However if I change
sourceSheet.Range("A4:L37").Select
to:
sourceSheet.Range("ggg").Select
I receive a run-time error 1004
Try the code below, it will create "ggg" named range from cells "A4:L37" in "Tabelle1" sheet.
Afterwards, it sets another Range MyNamedRange to the named Range("ggg") - this step is not necessary, I just like to work with variables for Range.
At the end, it selects MyNamedRange.
Code
Sub SelectRange()
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim sourceSheetSum As Worksheet
Dim MyNamedRange As Range
Set sourceBook = ActiveWorkbook
Set sourceSheet = sourceBook.Sheets("Tabelle1")
' create the named range "ggg"
sourceBook.Names.Add _
Name:="ggg", _
RefersTo:="=" & sourceSheet.Name & "!A4:L37"
Set MyNamedRange = Range("ggg") ' <-- set the Range to your NamedRange "ggg"
sourceSheet.Activate '<-- activate the sheet first
MyNamedRange.Select '<-- select the Named Range
End Sub
Try this:
SourceSheet.Range(Names.Item("ggg")).Select
I would appreciate your help with the macro I am trying to create.
I have an xls file with a bunch of worksheets, some of which named "1", "2", "3", and so forth. I would like to create a macro that loops only through those 'number-named' worksheets, hence NOT according to the index as in the code below. (Sheet "1" is not the first sheet in the workbook). Before the loop I need to define both the cell range and sheets.
Below is my (wrong) attempt.
Sub Refresh ()
Dim i As Integer
Dim rng As Range
Set rng = Range("A10:TZ180")
For i = 1 To 30
Sheets(i).Activate
rng.Select
rng.ClearContents
Application.Run macro:="xxx"
Next i
End Sub
dim w as worksheet
for each w in activeworkbook.worksheets
if isnumeric(w.name) then
w.range("A10:TZ180").clearcontents
xxx()
end if
next
If the macro "xxx()" requires a selected range you just need to add a select statement. (Borrowing from GSerg)
Dim w As Worksheet
For Each w In ActiveWorkbook.Worksheets
If IsNumeric(w.Name) Then
w.Range("A10:TZ180").ClearContents
w.Range("A10:TZ180").Select
Application.Run macro:="xxx"
End If
Next
To clear up your misunderstanding about assigning a range see the following:
Sub Refresh()
Dim ws As Worksheet
Dim rng As Range
Dim i As Integer
For Each ws In ActiveWorkbook.Worksheets
If IsNumeric(ws.Name) Then
'you must activate the worksheet before selecting a range on it
ws.Activate
'note the qualifier: ws.range()
Set rng = ws.Range("A10:TZ180")
'since the range is on the active sheet, we can select it
rng.Select
rng.ClearContents
Application.Run macro:="xxx"
End If
Next
End Sub
Sub test2()
Dim ws As Worksheet
Dim rg As Range
Dim arrSheets As Variant
arrSheets = Array("Sheet1", "Sheet2", "Sheet3")
Dim x As Long
For x = LBound(arrSheets) To UBound(arrSheets)
Set ws = Worksheets(arrSheets(x))
ws.Activate
'...
Next
End Sub
Sub test3()
Dim ws As Worksheet
Dim x As Long
For x = 1 To 20
Set ws = Worksheets(CStr(x))
ws.Activate
'...
Next
End Sub
try this
Sub main()
Dim shtNames As Variant, shtName As Variant
shtNames = Array(1, 2, 3, 4) '<== put your actual sheets "number name"
For Each shtName In shtNames
With Worksheets(CStr(shtName))
.Range("A10:TZ180").ClearContents
.Range("A10:TZ180").Select
Application.Run macro:="MacroToRun"
End With
Next shtName
End Sub
Sub MacroToRun()
MsgBox "hello from cells '" & Selection.Address & "' in sheet '" & ActiveCell.Parent.Name & "'"
End Sub
I'm looking to copy just one worksheet from a workbook to another and then change the name. Nothing hugely fancy.
I keep getting a type-mismatch error and I don't know why. I've used code from others which they say works but just doesn't for me.
Any ideas.
Private Sub cmdStockLog_Click()
week = ThisWorkbook.Sheets("Stock Sheet").Range("F1")
Dim wb As Workbook
Set wb = Workbooks.Open(ThisWorkbook.Path & "\Data\FOH Stock.xlsx")
ThisWorkbook.Sheets("Stock Sheet").Copy After:=Workbooks(wb).Sheets(Worksheets.Count)
ActiveSheet.Name = "WK" & week
End Sub
Try this code:
Private Sub cmdStockLog_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim week
week = ThisWorkbook.Sheets("Stock Sheet").Range("F1")
Set wb = Workbooks.Open(ThisWorkbook.Path & "\Data\FOH Stock.xlsx")
ThisWorkbook.Sheets("Stock Sheet").Copy After:=wb.Sheets(wb.Sheets.Count)
Set ws = wb.Sheets(wb.Sheets.Count) ' new sheet
ws.Name = "WK" & week
End Sub