Autofill in Excel VBA returns error 1004 - vba

I am trying to get a raw Excel file into a customized format. I added a picture below, so its easier to explain. I will address the requirements as steps too.
1) I need to get rid off all columns which include "Importo" or "Prezzo"
2) I need to extract the date from the remaining columns (Quantitá). First, I insert an empty row on top and then i apply right(cell,7).
So far, so good.
Then I want to autofill the remaining columns, but i get a 1004 error. In the example code I tried from J:O, but really id need it from J to the last column. I post the code (which works until the last row).
I was actually wondering if Autofilling is best practise here, maybe indexing though would be better?
Sub delete_col()
Dim A As Range
Do
Set A = Rows(1).Find(What:="Importo", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
Do
Set A = Rows(1).Find(What:="Prezzo", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
Rows("1:1").Select
ActiveCell.EntireRow.Insert
ActiveCell.Range("J1").Select
ActiveCell.FormulaR1C1 = "=RIGHT(R\[1\]C,7)"
Selection.AutoFill Destination:=ActiveCell.Range("J1:O1"), Type:=xlFillDefault
End Sub

I suppose that in the line ActiveCell.FormulaR1C1 = "=RIGHT(R\[1\]C,7)", the \ is a kind of typo, which should be deleted.
Concerning the 1004 error, the easiest way to go around it, while doing AutoFill, is something like this:
Sub TestMe()
Range("A1:O1") = Range("J1")
End Sub
Thus, every value in Range("A1:O1") will be set with the value from Range("J1").
However, your code uses a lot of Select, Activate and ActiveCell. Try to avoid these, because they are not considered good practices in VBA and may lead to different errors. How to avoid using Select in Excel VBA

Related

Excel, VBA: Referencing other Sheets when a Value changes in 1 sheet (Intersect)

I have been trying to get this to work, but I couldn't so far, and after searching I couldn't quite find a solution online either, so here it goes.
I have 3 sheets I'm using.
"wsPunting" (The one where the value changes)
"wsDetail" (The Sheet with complete data of everything)
"wsData" (The Sheet where I store certain data that I grab with macros)
Now, what I'm trying to do, is that when a Value changes in cell B2 in wsPunting (B2 is Data Validation made with a macro, not sure if this is valuable info, but better put it in here just in case), I filter my data in wsDetail, grab Column "O3:O", Remove Duplicates, and Assign that into a Data Validation in Cell B5 in wsPunting.
I got this to work already when I had <20 values entered. Now, when I pasted the actual Data that I was going to use in wsDetail, I kept getting "Type Mismatch" on the Intersect.
I've tried a few things already that I though might perhaps fix it, but I just can't seem to find it.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
On Error GoTo Booboo
Dim rngFSU As Range
Dim vFSU As Range
Dim wsPunting As Worksheet, wsData As Worksheet, wsDetail As Worksheet
Set wsPunting = ActiveWorkbook.Sheets("Puntingsblad")
Set wsData = ActiveWorkbook.Sheets("Data")
Set wsDetail = ActiveWorkbook.Sheets("Detail")
Set rngFSU = wsPunting.Range("$B$2")
Set vFSU = wsPunting.Range(Target.Address)
'The next line is where it keep dropping the error
If Not Intersect(rngFSU, vFSU) Then
wsDetail.Range("A2", wsDetail.Range("A3").SpecialCells(xlCellTypeLastCell)).AutoFilter Field:=1, Criteria1:=Range("B2").Value
wsDetail.Range("O3", wsDetail.Range("O3").SpecialCells(xlCellTypeLastCell)).Copy
wsData.Range("B2").PasteSpecial xlPasteValues
wsData.Range("B2", wsData.Range("B1").End(xlDown)).RemoveDuplicates Columns:=Array(1)
With wsPunting.Range("B5").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=Data!" & wsData.Range("B2", wsData.Range("B1").End(xlDown)).Address
.IgnoreBlank = True
End With
wsDetail.Range("A2", wsDetail.Range("A3").SpecialCells(xlCellTypeLastCell)).AutoFilter Field:=1
End If
Booboo:
MsgBox err.Description
End Sub
This is actually my last resort, concidering I usually try fixing it myself to learn faster. But I've been stuck on this one for so long already, I just can't anymore atm.
Cheers in advance.
If Not Intersect(rngFSU, vFSU) Then
Intersect(rngFSU, vFSU) is an object of type Range, you are trying to cast it into a boolean.
This leads to an error. I understand that you want to check if there is no intersection, in which case the Range returned by Intersect is Nothing. So try this:
If Intersect(rngFSU, vFSU) Is Nothing Then

Do While ActiveCell <> Range

I have this VBA excel macro code
Sub fillcells()
Range("J14").Select
Do While ActiveCell <> Range("J902")
ActiveCell.Copy
ActiveCell.Offset(6, 0).Select
ActiveCell.PasteSpecial
Loop
End Sub
At first it was working fine but now sometimes when I try to run the macro the loop suddenly stops at cell J242, other times is arising an error 'mismatch type' and sometimes the macro just select cell J14 without doing the loop
Not sure what you want to do, but (as noted in the comments to your OP), don't use .Select/.Activate. The following should do what (I think) you wanted:
Sub fillcells()
Dim i& ' Create a LONG variable to count cells
For i = 14 To 901 Step 6
Cells(i, 10).Offset(6, 0).FormulaR1C1 = Cells(i, 10).FormulaR1C1
Loop
End Sub
This will loop from cell J14 to J901, copy/paste* to a cell 6 rows offset.
* Note I didn't actually copy/paste. Since your original code used PasteSpecial, I'm assuming you just want the values pasted. In this case, you can set the two ranges/cells equal.
Just an addition to what #BruceWayne already said: whenever you have this typical phenomenon that something happens only "sometimes" it is often a case of using keywords such as Active or Current or Selection. These are not specific but change each time that you call the macro. Whatever you have selected is the starting point. You might even start clicking around and thus change Selection while the macro is running. In short, you should start coding explicitly and don't allow VBA / Excel to assume / make the decision for you.
Let's start with Range("J14").Select. This line of code asks VBA to make already two assumptions:
If you have several Excel files open. Which Excel file should it start with?
Within the file there might be several sheets. On which of these sheets should J14 be selected?
Explicit coding means that you (hopefully at all times) be very specific what you are referring to. So, instead of just stating Range("J14") you should use:
ThisWorkbook.Worksheets("SheetNameYouWantToReferTo").Range("J14")
But is pointed out in the other answer, this is not even necessary in this case. Rather loop the rows as shown and use:
ThisWorkbook.Worksheets("SheetNameYouWantToReferTo").Cells(i, 10).Offset(6, 0).Formula = ThisWorkbook.Worksheets("SheetNameYouWantToReferTo").Cells(i, 10).Offset(i, 10).Formula
Since this is a bit lengthy you can shorting it by using a With statement:
With ThisWorkbook.Worksheets("SheetNameYouWantToReferTo")
.Cells(i, 10).Offset(6, 0).Formula = .Cells(i, 10).Formula
End With

VBA AutoFilter hiding all rows - including the ones matching criteria

I'm applying VBA AutoFilter to some results in an excel sheet. It seems to compile properly, but when I check the results, the AutoFilter is hiding both the rows that match and that do not match the criteria I applied.
If I manually select the autofilter that was applied, i see that the criteria that I coded is correctly input and, by just clicking enter, the criteria matching rows show.
I'm using a Brazilian Portuguese version of Excel, not sure if that might be the issue.
Here's what I've tried:
Sub FilterOff(ByVal thisSheet)
thisSheet.Activate
With thisSheet
.AutoFilterMode = False
.Range("A1:F1").AutoFilter
.Range("A1:F1").AutoFilter Field:=4, Criteria1:=">0.01", _
Operator:=xlOr, Criteria2:="<-0.01"
.Range("A1:F1").AutoFilter Field:=5, Criteria1:=">100"
End With
End Sub
I was experiencing something similar in one of my macros. I had a table that I was trying to autofilter. I could do it manually, but not in VBA, even when I was exactly replicating what the recording function gave me. I also could not copy+paste as values in VBA, but I could manually.
What worked for me was to save and close the workbook, then reopen it and apply the autofilter. Specifically, I used this:
tempWb.SaveAs ("dir\temp.xlsx")
tempWb.Close (0)
Set rptWb = Workbooks.Open("dir\temp.xlsx")
Set rptWs = rptWb.Sheets(1)
rptWs.Range(rptWs.Cells(1, 1), rptWs.Cells(lstRow, lstCol)).AutoFilter Field:=20, Criteria1:="=NO RECORD"
and it worked.
Update: I think the underlying issue was that I had calculation set to manual. After I set calculation to automatic, the problems went away.
I did something like this and it worked
Range("A1:B6").AutoFilter
ActiveSheet.Range("$A$1:$B$6").AutoFilter Field:=1, Criteria1:="=10", _
Operator:=xlOr, Criteria2:="=30"
ActiveSheet.Range("$A$1:$B$6").AutoFilter Field:=2, Criteria1:="100"

How can I autofilter when I'm not sure which column it will be?

I'm trying to automate the processing of various reports, which I just need to filter and count the rows of. I currently have a PowerShell script that opens each report and runs a few macros. It is just about in a working state, but I'd now like to make it a bit more intelligent and catch some of the fails, so I might be asking a few questions but I'll stick to the one problem on each.
The reports have similar, but not identical layouts. I am looking for a particular column name to then autofilter. I have a very basic and bodged together macro that currently does this, and works (most of the time) for example, sometimes the column I want is A or B:
If Worksheets(1).Range("A1") Like "*word" Then
Worksheets(1).Range("A1").AutoFilter Field:=1, Criteria1:="=criteria1", Operator:=xlOr, Criteria2:="=criteria2"
ElseIf Worksheets(1).Range("B1") Like "*word" Then
Worksheets(1).Range("A1").AutoFilter Field:=2, Criteria1:="=criteria", Operator:=xlOr, Criteria2:="=criteria2"
Hopefully that gives you the current picture.
I now want to instead, do a search for the field header I am looking for, then filter that column, so if the report format in future changes my macro won't break. Something similar to:
ColNum = Application.Match("*header", Range("A:Z"), 0)
ColNumInt = CInt(ColNum)
If ColNumInt > 0 Then
ActiveSheet.Range("A1").AutoFilter Field:=ColNumInt, Criteria1:="=criteria1*", Operator:=xlAnd
End If
But this gives an error "AutoFilter method of Range class failed", Googlefu says to turn off filters but they're already off. So I'm a bit stuck.
This part will always fail:
ColNum = Application.Match("*header", Range("A:Z"), 0)
since match only works on one row or column. So your code is actually returning Error 2042, which is then converted to 2042 by CInt. I guess you don't have that many columns of data, hence the autofilter fails. Use:
ColNum = Application.Match("*header", Range("A1:Z1"), 0)
If Not IsError(ColNum) Then
...
End If
This should work for you.
Sub Button1_Click()
Dim r As Range
Dim c As Integer
Set r = Range("A1:B1").Find(what:="*word*", lookat:=xlWhole)
c = r.Column
ActiveSheet.AutoFilterMode = 0
Columns(c).AutoFilter Field:=1, Criteria1:="*criteria1*"
End Sub

Inserting, searching, copying, pasting across 2 spreadsheets in Excel

I thought I'd be able to figure this out based off an analysis of similar code we had written for something else, but code isn't my forte.
Diving into VBA without guidance has proved to be too daunting for me to proceed.
Forgive my terminology if I use the wrong language, I'm happy to learn and be corrected.
This shouldn't be difficult for someone that knows what they're doing, I just don't at all.
I'm trying to create a macro enabled workbook that does the following:
Open "Data.csv" from a folder called "Data" in the same directory as the macro. We'll call this workbook A - wbA.
Insert a column on wbA after Column C titled "Group Image Name." This Column D is where we want data to end up.
Open "Groups.csv" from a folder called "Groups" in the same directory as the macro. We'll call this workbook B - wkB.
(This next part needs to be a loop that starts at C1 on wbA and proceeds down the column until the end of the spreadsheet)
Copy value from selected cell in Column C on wbA
Search Column C on wbB for copied value. When found, move selection over to corresponding cell in Column A. (If C2, then A2)
Copy contents of the column A value from wbB to column D cell on wbA that corresponds to the original starting point on wbA.
Basically in plain language: Search for Column C contents from wbA on Column C of wbB. When found, move to Column A of same cell # on wbB, copy contents, and paste into cell # of Column D on wbA that corresponds to cell # of starting point from Column C.
I hope that's clear; please feel free to ask for more details if necessary. Thanks for anyone's help in advance!
Here is my terrible code I'm working with at the moment:
Sub OpenDataAddGroupImageTitleColumn()
ChDir "C:\[RealCodeHasCorrectFilepath..]\Desktop\TEST"
Workbooks.Open Filename:="C:\[RealCodeHasCorrectFilepath..]\Desktop\TEST\DATA.csv"
Columns("D:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells.Select
Cells.EntireColumn.AutoFit
Range("D1").Select
ActiveCell.FormulaR1C1 = "Group Image Title"
Range("C2").Select
'Variables for storing Row, Column Location, and Value here
Dim GROUPNAME As String
Dim RowLocationX As Long
Dim ColumnLocationZ As Integer
GROUPNAME = ActiveCell.Value
RowLocationX = ActiveCell.Row
ColumnLocationZ = ActiveCell.Column
Workbooks.Open Filename:="C:\[RealCodeHasCorrectFilepath..]\Desktop\GROUPS.csv"
Columns("C:C").Select
Selection.Find(What:="GROUPNAME", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
End Sub
The current snag I can't figure out after much googling is this: Is it possible to search for my variable GROUPNAME using the Find/Replace feature? Or should I be taking a different approach using something involving copying to clipboard?
The first problems that I see are:
1) You don't put functions in the middle of sub routines. If you want a function, you put it on its own as such:
Sub MySub()
'Do Stuff
x = myFunction()
debug.print x
End Sub
Function MyFunction() As String
'Do Stuff
MyFunction = "Test"
End Function
2) The code you have provided won't compile for reason 1 and also because you have ended the Sub with "End Function" instead of End Sub. Try running the code and you will get error messages. Then you can research the error message and try to fix it. Further, if you don't get error messages, you can step through the code to make sure it is working the way you intend it to.
3) The very first line isn't going to work. You need to use the ENTIRE path of the file, even if it is in the same folder as the file you currently have open. There are ways to get the directory of the file you currently have open (google will surely show you many of them), and you could just append the filename to the directory of the file you have open.
4) You want a loop, but you haven't put in any looping structions. Google "Excel VBA loop through cells" and you can find many examples to use.
5) I think the biggest issue that you are having is that you are overwhelmed because you are trying to do everything all at once. I would suggest solving one problem at a time and then putting the code together. For instance, you want to open two files. Start by writing code just to open one file. Then try to get a value out of the file into the open workbook. Then open another file. Then get data out of that file. Then test looping through cells in the current workbook and checking for desired criteria and pasting the results if they match. Then combine all of these things into coherent code.
Nobody writes efficient code the first time they try, so even if you end up with really long code that isn't efficient, you'll learn a lot and have something. Even experienced programmers can get stuck or write code that doesn't work the first or even tenth time. If you get stuck, ask a specific question about a specific error message or specific issue that you can't resolve.