Compile error: Expected Function or variable - excel

I have this macro (written in Excel 2016/Windows) that acts a very simple reservation tool, checking if an asset is currently booked or free. Depending on this, it either writes when the booked period will end or when the next booked period will start in another worksheet:
Sub Schaltfläche1_Klicken()
Worksheets("Tabelle10").Activate
With Columns(4)
.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
ActiveCell.Offset(0, -3).Select
If Selection.Value = "TODAY AM" Then
Sheets("HTML Output").Range("B3").Value = "Desk booked from this afternoon. Next availability"
ActiveCell.Offset(0, 3).Select
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Select
ActiveCell.Offset(0, -2).Select
Selection.Copy
Sheets("HTML Output").Range("C3").PasteSpecial xlPasteValues
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("HTML Output").Range("D3").PasteSpecial xlPasteValues
ElseIf Selection.Value = "TODAY PM" Then
Sheets("HTML Output").Range("B3").Value = "Desk booked from this afternoon. Next availability"
ActiveCell.Offset(0, 3).Select
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Select
ActiveCell.Offset(0, -2).Select
Selection.Copy
Sheets("HTML Output").Range("C3").PasteSpecial xlPasteValues
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("HTML Output").Range("D3").PasteSpecial xlPasteValues
ElseIf Selection.Value = "TOMORROW AM" Or Selection.Value = "TOMORROW PM" Or Selection.Value = "FUTURE" Then
Sheets("HTML Output").Range("B3").Value = "Desk free until (including)"
ActiveCell.Offset(-1, 1).Select
Selection.Copy
Sheets("HTML Output").Range("C3").PasteSpecial xlPasteValues
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("HTML Output").Range("D3").PasteSpecial xlPasteValues
End If
End Sub
This works perfectly fine in Office 2016 (Windows 10) but results in a Compile error: Expected Function or variable when I try to run it in Office 2011 for Mac or Office 2015 for Mac.
Can anyone point me in the right directions as for the reason(s) for this or tell me how to change the code to make it work?
Thanks in advance!
Jascha

The error handling in VBA Excel 2011 is not as great as it's Window's counterpart.
You were getting that error because you were using With/End With with ActiveCell.Offset(0, 1).Select
The best way to reproduce that error is paste this code in a module
Sub Schaltfläche1_Klicken()
With ActiveCell.Offset(0, 1).Select
End With
End Sub
Note: You and I didn't get that error later because you modified your post which we both tested :)
Interesting Read

Related

VBA Runtime Error '1004': Application-defined or Object-defined error" setting cell contents

I am encountering the Error 1004 when setting cell contents, specifically when setting the cell contents in the line
ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"""
The same code is used in multiple sheets and some work whereas others encounter this error. Basically I'm trying to add the total of rows with a reference code beginning with "21BG", then print a description
"Total increase in GBP £TOTAL"
or
"Total decrease in GBP £TOTAL"
depending on the total.
The macro is as follows:
Sub AdjustGBP()
'
' Macro4 Macro
Range("B65536").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "21BG"
ActiveCell.Offset(-1, 1).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "11041202"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "Current deposits GBP"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "当座預金 GBP"
ActiveCell.Offset(0, 5).Select
Selection.Copy
If ActiveCell.Value > 0 Then
ActiveCell.Offset(0, -2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlSubtract
ActiveCell.Offset(0, -2).Select
' ActiveCell.FormulaR1C1 = "Total increase in GBP XXX 2007"
ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"""
Else
ActiveCell.Offset(0, -1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlSubtract
ActiveCell.Offset(0, -3).Select
' ActiveCell.FormulaR1C1 = "=""Total Decrease in " & "MENU!R11C10""
ActiveCell.FormulaR1C1 = "=""Total Decrease in GBP ""&MENU!R11C10&"" 2021"""
End If
ActiveCell.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(ActiveCell.Offset(-3, -5), ActiveCell.Offset(0, 4)).Select
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
With Selection.Borders(xlInsideHorizontal)
.Weight = xlThin
End With
End Sub
This line
ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"""
would look as formula like
="Total increase in GBP "&MENU!R11C10"
so it has one " in the end that should not be there!
The line should look like
ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"
to get this formula
="Total increase in GBP "&MENU!R11C10
Don't forget to join strings using "&" operator.
ActiveCell.FormulaR1C1 = "=" & Chr(34) & "Total increase in GBP " & Chr(34) & "&MENU!R11C10"

Excel VBA how to express Do loop until activecell.value = value in specific cell

I try
Do
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=R[-1]C+0.01"
Loop Until ActiveCell.Value = "$C$1"
But it's not working
Pls. help
i try to add + 0.01 from first value (1) until equal last value (1.9)
Here is my all code
[Sub ExtractRC()
Range("A2:A" & Range("A2").End(xlDown).Row).Select
Cells(Rows.Count, "A").End(xlUp).Select
Selection.Copy
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Range("k2").Select
ActiveCell.FormulaR1C1 = Range("A2").Value
Do
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=R\[-1\]C+0.01"
Loop Until ActiveCell.Value = "$C$1"
End Sub][1]
P.S. Value in Column A can change

VBA Stop For/Next Statement at Certain Criteria

Image
Edit: to help clarify, I'd like to be able to populate B2:B6 through VBA so I can copy paste section A2:B6 down. My problem is that next month I will lose the August section and only have Sep to Dec, and so on as the year goes on.
This is my first time actually asking a question here so sorry in advance if I do something incorrectly. I'm very new to vba and need help getting this code to adjust itself and know when to stop.
My old code is this:
ActiveCell.FormulaR1C1 = "=RC[1]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-1]C[2]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-2]C[3]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-3]C[4]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-4]C[5]"
ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-5]C[6]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-6]C[7]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-7]C[8]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-8]C[9]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-9]C[10]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-10]C[11]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-11]C[12]"
'ActiveCell.Offset(1, 0).Range("A1").Select
Where all it does is transpose a year's worth of data into a singular column. I'm trying to end with something like:
If ActiveCell.Offset(0, 1).Value <> "Dec" Then
c As Long
For c = 1 To 12
ActiveCell.FormulaR1C1 = "=RC[&c&]"
ActiveCell.Offset(1, 0).Range("a1").Select
Next c
Where it will adjust the C# and stop after it reaches a certain value in the next column. Currently I just add or remove a ' in front of each pair of the old code to get it to stop where i need it to but i'd like it to be able to do it by itself.
Thanks!
Try this:
Dim rng As Range
Dim last_col As Integer
last_col = Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column
If last_col > ActiveCell.Column Then
Set rng = Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(ActiveCell.Row, last_col))
rng.Copy
ActiveCell.Offset(1, 0).PasteSpecial xlPasteAll, Transpose:=True
Set rng = Nothing
End If

VBA to look for information in between two dates that are written manually in a specific cell

I'm looking for a VBA code that will look in the 2nd row for specific dates.
Basically, I'm writing a "Begin" date (ex: 2018-07-01) and an "End" date (ex: 2018-07-31) and I want my code to look for everything in between, including those dates, and copy paste all the information in another excel sheet. What the function will do at the end is it'll look into many sheets for those dates and copy every bit of information that are below those dates, and paste them all into one main sheet.
What I'm attaching below is what I've done so far:
Sub Copie()
Sheets("1").Select
Range("B1:H1").Select
Selection.Copy
Sheets("Per Employe").Select
Range("A4").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J1:P1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Cells(1, 1).Select
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Per Employe").Select
Cells(4, 1).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("1").Select
Range("B2:H2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J2:P2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
End Sub
What I'm trying to add is the function to look up the dates and only copy/paste the dates that were asked for in the main excel sheet, which would be "Per Employe". Would anyone have any solution to this? The cells that contain the begin and end date would be D1 & F1.

making vba macro more efficient

This macro looks at a row, copies the content and pastes it into desired cells in certain sheets.
I´d like to make this macro code quicker because it takes too long.
The code loops over about 7000 rows.
Any help would be appreciated,
Here´s my code:
Sub Input_Template()
Application.ScreenUpdating = False
Sheets("Cost Gained").Select
Range("A1").Select
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.EntireRow.Hidden = False
ActiveCell.Offset(1, 0).Select
Loop
Do
'Qc Note
ActiveCell.Offset(0, 0).Select
Selection.Copy
Sheets("Debit Note").Select
Range("G8,C6").Select
ActiveSheet.PasteSpecial
Range("C6").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(R[2]C[4], ""DN"")"
'Supplier Name
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("G11").Select
ActiveSheet.PasteSpecial
'RTV Number
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 2).Select
Selection.Copy
Sheets("Debit Note").Select
Range("G16,C22").Select
ActiveSheet.PasteSpecial
'Cost
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 2).Select
Selection.Copy
Sheets("Debit Note").Select
Range("G9,G22,G24,G26,G27").Select
ActiveSheet.PasteSpecial
'Supplier Code
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 2).Select
Selection.Copy
Sheets("Debit Note").Select
Range("G10").Select
ActiveSheet.PasteSpecial
'PO Number
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 2).Select
Selection.Copy
Sheets("Debit Note").Select
Range("G7").Select
ActiveSheet.PasteSpecial
'Suppplier Email
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("G15").Select
ActiveSheet.PasteSpecial
'Address
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("C9").Select
ActiveSheet.PasteSpecial
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("C10").Select
ActiveSheet.PasteSpecial
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("C11").Select
ActiveSheet.PasteSpecial
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("C12").Select
ActiveSheet.PasteSpecial
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("C13").Select
ActiveSheet.PasteSpecial
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("C14").Select
ActiveSheet.PasteSpecial
Sheets("Cost Gained").Select
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Debit Note").Select
Range("C15").Select
ActiveSheet.PasteSpecial
Range("G9").NumberFormat = "$#,##0.00"
Range("G15").Select
Selection.Style = "Hyperlink"
This contains code to add bold around an area, change font to arial size 16.
But is very long so I have left it out.
'Save as pdf once finish one row, then save pdf in a location then continue until row 299.
Sheets("Debit Note").Select
ChDir "P:\Perkins\Quality\COPQ\J Benge COPQ\MACROS\Debit Notes\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"P:\Perkins\Quality\COPQ\J Benge COPQ\MACROS\Debit Notes\" & Range("G8").Value
'Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Sheets("Cost Gained").Select
ActiveCell.Select
ActiveCell.Offset(1, -17).Select
Do Until ActiveCell.EntireRow.Hidden = False
ActiveCell.Offset(1, 0).Select
Loop
Loop Until ActiveCell.Row = "299"
End Sub
You shoul get rid of the .Select and Selection. you don´t need them, they slow down code and can cause errors.
For Example:
Instead of
Sheets("Debit Note").Select
Range("G11").Select
ActiveSheet.PasteSpecial
You can write
Sheets("Debit Note").Range("G11").PasteSpecial
Just add these two lines at the beginning of Input_Template()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
And add these two lines before the End Sub
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
As you're not using any of the PasteSpecial paste types (such as xlPasteValues) then you could just use:
ThisWorkbook.Worksheets("Cost Gained").Cells(1, 2).Copy _
Destination:=ThisWorkbook.Worksheets("Debit Note").Cells(2, 1)
This copies from range B1 ( .Cells(1,2) - row 1, column 2) to A2 ( .cells(2,1) - row 2, column 1).

Resources