Having Run-time-error 1004 Application defined or object defined error - vba

I have a simple formula in vba, like this :
Sub button_fu()
Windows("H1.xlsm").Activate
Sheets("jan").Activate
Range("J3").Select
Range("J3").Formula = "=IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,8,False))=1,""terhubung"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,9,False))=1,""unreach"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,10,False))=1,""reject"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,11,False))=1,""workload"","""")"
End Sub
anyone hepl me, how the problem solved it?

You are missing some closing brackets in your formula:
Sub button_fu()
Windows("H1.xlsm").Activate
Sheets("jan").Activate
Range("J3").Select
Range("J3").Formula = "=IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,8,False))=1,""terhubung"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,9,False))=1,""unreach"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,10,False))=1,""reject"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,11,False))=1,""workload"",""""))))"
End Sub
P.S. Should "FOLLOW UP H1.xlsx" be "H1.xlsm", or do you have another workbook?

Related

How to use IsText with VBA

I am learning VBA and am trying to execute the code:
Sub example()
If IsText(ActiveCell) Then
MsgBox "Is Text"
Else: MsgBox "Not Text"
End If
End Sub
It does not work, and gives me the error, "Compile Error: Sub or Function not defined" with the "IsText" highlighted. The macro works fine if I exchange "IsText" for "IsNumeric". Thanks for the help.
IsText is a worksheet function. Use worksheetfunction to apply it within vba.
If worksheetfunction.IsText(ActiveCell) Then

Application-defined or object-defined error with FormulaR1C1

I've got some code that isn't working, and have basically simplified it as much as possible for the sake of this post. I'm trying to run this macro and get an error saying
Application-defined or object-defined error
Am I missing something obvious?
Sub Test()
Range("B1").FormulaR1C1 = _
"=VLOOKUP(RC[-1],R2C6:R4C7,2,FALSE),0))"
End Sub
Thanks!
Your VLOOKUP has an extra parameter hanging off the right side.
Sub Test()
Range("B1").FormulaR1C1 = _
"=VLOOKUP(RC[-1], R2C6:R4C7, 2, FALSE)"
End Sub

vba - clearing cells in excel with a button

I'm looking to make a button where I can clear all the cells in a range (e.g. B6:M10000) as well as a single cell (ie C3)
This is what I have so far but it doesn't seem to work.
Sub ClearContents()
Range("B6:M100000" & "C3").Select
Selection.ClearContents
End Sub
Thanks in advance .
Sub ClearContents()
Range("B6:M100000, C3").ClearContents
End Sub

Print Button In Excel 2016 Using Macros?

I'm currently trying to create a Print Button on one of my worksheets. I need it to print that worksheet as well as another one. Both of the names are "Budget Sheet" and "Listed Commitments Sheet" without the quotation marks.
I created the button without hassle, but I know very little about Macros so I still need the code. I've tried multiple solutions, but nothing seems to work. I've recently tried to use this Code, but it hasn't worked. What am I doing wrong? What code could I possibly use instead?
Private Sub CommandButton1_Click()
Function PrintMultipleSheets()
Sheets(Array("Budget Sheet", "Listed Commitments Sheet")).PrintOut
End Function
End Sub
It comes up with an error that says "Compile Error: Expected End Sub".
The code doesn't compile, because you can't have a Function inside a Sub.
Get ride of the line Function PrintMultipleSheets() and get rid of End Function. It should work I think. You'll end up with:
Private Sub CommandButton1_Click()
Sheets(Array("Budget Sheet", "Listed Commitments Sheet")).PrintOut
End Sub
Just a simple loop with a print call:
Sub forEachWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call printSheet(ws)
Next
End Sub
Function pasteContents(ws as Worksheet)
ActiveSheet.PrintOut
End Function

worksheet_calculate() is not working

I have added a private sub worksheet_calculate() in a sheet called Main. I have a value in column AP with formulas derived from other sheets and if that number is greater than value in X I want to display a message as a warning that it's over, but the code is not working any suggestions why?
Private Sub Worksheet_Calculate()
If Sheets("Main").Range("AP7").value > Sheets("Main").Range("x7").value Then
MsgBox "You Are Over Pieces Suggested"
End If
End Sub
Try this.
Private Sub Worksheet_Calculate()
If Range("AP7").Value > Range("X7").Value Then
MsgBox "You Are Over Pieces Suggested."
End If
End Sub
EDITED####
Edited the original code to run as a Worksheet_Calculate rather than a Change.
Working on trying to set the ranges to columns for you now.
EDIT#########
I flippin love a challenge. Try This.
Private Sub Worksheet_Calculate()
Set Target = Range("AP:AP").SpecialCells(xlCellTypeFormulas)
If Target Is Nothing Then Exit Sub
For Each c In Target
If c > Range("X" & c.Row) Then
MsgBox "You Are Over Pieces Suggested - Cell " & "AP" & c.Row
End If
Next
End Sub
Consider using Data Validation on cell AP7 using a "Custom" formula of: =AP7<=$X$7
Fill in the Error Alert tab on the validation menu: Stop; "You Are Over Pieces Suggested". I think this might achieve what you want without any macros. In fact, it can prevent an invalid number from being entered in the first place.