VBA error code 1004 using a SUMIF formula - vba

I need your help.
I am writing this small piece of code in VBA to use in a an RPA proces.
I have tested my formula in Excel and it works, but everytime when i try to run it from VBA it crashes with error-code 1004 and tells me the problemn is in code .Range("C29").Formula = "=SUMIF(Sheet1!$A$19:$A$1000;$B29;Sheet1!$G$19:$G$1000)".
Changed the formula to simplere formulas and that works fine.
Anybody else who knows this issue?
Sub FillDown()
Dim strFormulas '(1 To 3) As Variant
With ThisWorkbook.Sheets("MainSheet")
.Range("C29").Formula = "=SUMIF(Sheet1!$A$19:$A$1000;$B29;Sheet1!$G$19:$G$1000)"
.Range("C29:C501").FillDown
End With
End Sub

Check your SUMIF formular. It contains error.Is your idea "=SUMIF(Sheet1!$A$19:$A$1000, $B29, Sheet1!$G$19:$G$1000)"??

Sub FillDown2()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("MainSheet")
ws.Range("C29").FormulaLocal = "=SUMIF(Sheet1!$A$19:$A$1000;$B29;Sheet1!$G$19:$G$1000)"
ws.Range("C29:C501").FillDown
End Sub
So the problemn was based in the settings of my worksheet.
By adding the formulalocal and at least i now dont have the bug anymore, on to the next problemn xD

Related

vba: Do same code in all sheets

Simple question. The following code works fine:
Dim hoja as worksheet
For Each hoja In Workbooks("Origen.xlsx").Worksheets
Msgbox hoja.name
next
However, the following code does NOT work. Can anybody say why and make it work?
Dim hoja as worksheet
For Each hoja In Workbooks("Origen.xlsx").Worksheets
Range("a1:a2").copy Destination:=Range("a3:a4")
next
The second code only does the copy/paste thing in one of the sheets, but not in all.
Please can you help? I'm so frustrated with such a simple thing.
Thanks!
The Range object, if not qualified, always refers to active sheet.
If you want to copy paste each time on different sheet, use the code below:
Dim hoja as worksheet
For Each hoja In Workbooks("Origen.xlsx").Worksheets
hoja.Range("a1:a2").copy Destination:=hoja.Range("a3:a4")
next
Try hoja.Range instead of simply Range.

Excel hangs when runs VBA in Module

Excel rookie here. I'm trying a very simple task: copying and pasting cells. The Excel will go into "non responding" once the VBA runs. The code was running at one point, but as I developed more lines, Excel stopped responding. I comment out the rest of the line and only run the lines shown below, Excel still hangs. Any ideas? Thanks!
Also, I'm writing the codes in the module in Excel.
Sub EDRII()
Application.ScreenUpdating = False
Dim EDR As Worksheet, Lookup As Worksheet, FA As Worksheet
Set EDR = Sheets("for edr II")
Set Lookup = Sheets("Lookup")
Set FA = Sheets("FA_Segment_Region")
Sheets(EDR).Activate
Range("B6:X10").Copy Range("B5:X9")
Application.ScreenUpdating = True
End Sub
Excel xlsm
You have defined EDR to be a Worksheet and assigned it to be a reference to the sheet called "for edr II".
When you try to use it later, in the line saying
Sheets(EDR).Activate
you are using EDR in a spot where VBA is expecting a String or an Integer (or Long).
That line should simply say
EDR.Activate
It's also a good idea to avoid Activate and Select whenever possible so, rather than activating the sheet, you could just use
EDR.Range("B6:X10").Copy EDR.Range("B5:X9")
for the Copy statement.
Here's some pointers:
Indent your code so it's easier to follow (minor point in your example)
Fully qualify your references (ThisWorkbook.Worksheets(), EDR.Range(), etc)
Rather than copying and pasting, use .Value
Your code was breaking down because you were trying to activate EDR with Sheets(EDR).Activate which should just be EDR.Activate. Either way, when you take into account the points above you see that you don't need to Activate anything anyway!
Sub EDRII()
Application.ScreenUpdating = False
Dim EDR As Worksheet, Lookup As Worksheet, FA As Worksheet
Set EDR = ThisWorkbook.Worksheets("for edr II")
Set Lookup = ThisWorkbook.Sheets("Lookup")
Set FA = ThisWorkbook.Sheets("FA_Segment_Region")
EDR.Range("B5:X9").Value = EDR.Range("B6:X10").Value
Application.ScreenUpdating = True
End Sub

Using the CurrentRegion property in VBA

I want to write a simple VBA macro to copy the data in a range that corresponds to the block of cells around an active cell in Sheet1 and Paste it in Sheet2. (preferably in the same address as of in Sheet1).
The code I have written is:
Option Explicit
Dim Cello As Range
Sub CopyCurrentRegion2()
Set Cello = Worksheets("Sheet1").Range(ActiveCell.Address)
Cello.CurrentRegion.Copy Sheets("Sheet2").Range(Cello)
End Sub
Please correct this prog. It is giving run time error: 1004.
Consider:
Sub CopyStuff()
With ActiveCell.CurrentRegion
.Copy Sheets("Sheet2").Range(.Address)
End With
End Sub

Excel 2010 VBA: .Names.Add does not work after .Hyperlinks.Add

I have run into a problem in Excel 2010 VBA on Windows 7 64-bit version which I have not been able to solve. The issue can easily be recreated by pasting the code below in a module in a new workbook and run it.
What I want to do is to loop through a number of sheets and add a defined name and a hyperlink on each sheet.
Sub Test()
Dim i As Integer
Dim ws As Worksheet
Dim defName As String
For i = 1 To 2
Set ws = Sheets(i)
defName = "Name_" & ws.Name
ws.Names.Add Name:=defName, RefersToR1C1:="=OFFSET(Sheet3!R1C1,0,0,1)"
ws.Hyperlinks.Add Anchor:=ws.Range("A1"), _
Address:="", SubAddress:="=Sheet3!A1"
Next i
End Sub
Running the code gives the following error on the second iteration, on the ws.Names.Add call: Run-time error '1004: The formula you typed contains an error.
Doing any of the following makes the error disappear:
Change the for iteration to "i = 1 To 1" or "i = 2 To 2"
Put a debug breakpoint inside the for loop and pressing F5 when it has stopped
Change the cell reference to
ws.Names.Add Name:=defName, RefersToR1C1:="=Sheet3!R1C1", i.e. removing the OFFSET command
Adding DoEvents to the first line of the for loop or setting Application.EnableEvents = False does not solve the problem.
Does anyone know the cause of this error or how to get around it? I am thankful for any help.
Edit: The issue occurs no matter what the hyperlink links to. Changing the hyperlink to the following does not solve the issue
ws.Hyperlinks.Add Anchor:=ws.Range("A1"), Address:="http://www.google.com"
Edit2: Managed to recreate the issue with an even simpler code:
Sub Test()
With Sheets(1)
.Hyperlinks.Add Anchor:=.Range("A1"), Address:="http://www.google.com"
.Names.Add Name:="myDefName", RefersToR1C1:="=OFFSET(Sheet1!R1C1,0,0,1)"
End With
End Sub
I solved this by separating the for loops into two loops, one for the .names.add calls and one for the hyperlinks.add calls. This way all the names get defined before the first hyperlink is created.
Sorry if it is not correct of me to post this as an answer.

Spell check an Excel sheet in VBA

I have scoured the Internet and I have found a handful of possible solutions to this issue, but I wanted to ask here as well.
The goal is to click a button, and spell check an entire sheet.
Here's some code
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
Also, I found this:
Sub SpellCheck()
Dim Checkword As String, Result As Boolean
Checkword = Selection.Value
Result = Application.CheckSpelling(Checkword)
Selection.Offset(0, 1) = Result
End Sub
Any ideas? Neither is working for me. Thanks!
You can check the whole workbook by doing something like:
Sub SpellCheck()
For Each sh In Worksheets
Sheets(sh.Name).Cells.CheckSpelling
Next
End Sub
this will cycle through each sheet in the entire book and run a spellcheck on each one. What I can't figure out yet is how to make the spell checker actually move to the position of the spelling error. So, with the above you just get a list of spelling errors with no context with which to asses them.
I noticed I just had a typo in my code.
Below works:
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
But, if anyone knows how to do the entire workbook, I'd be interested in that. Thanks.
This code will work on selected cells .This will highlight if any spell mistakes in a cell
Dim Myrange As Range
Selection.SpecialCells(xlVisible).Select
For Each Myrange In Selection
If Application.CheckSpelling(word:=Myrange.Value) = False Then
Myrange.Font.Color = vbRed
End If
Next
OK, so you can use the following command to invoke the toolbar's spellchecker which does move you to the position of the spelling error as long as you have screen updating enabled at the time.
Application.CommandBars("Tools").Controls("Spelling...").Execute
You can use this command embedded in the loop above to loop through the sheets in the workbook and invoke this command on each new sheet.
Cap
This uses a code snippet from a previous answer to restrict the area used for spellchecking to a specific region. Something I needed to do in a small project. This give the full functionallity of the spellchecker with errors shown in context. The rowNumber is calculated elsewhere.The selection range can be fully controlled elsewhere in your code to suit your particular need. Thought this might help others searching this posting.
With Sheets("Sheet1")
slic = CStr(rowNumber)
.Range("AL3:AN" & slic).Select
Application.CommandBars("Tools").Controls("Spelling...").Execute
End With
Thanks to previous posters this solved a problem form me. I am most grateful.