VBA Compile Error occurring - vba

The code below is meant to run when the Workbook is first opened.
Sub Auto_Open()
Dim LastRow As Integer
LastRow = Sheet6.UsedRange.Rows.Count
ActiveWorkbook.RefreshAll
Sheet6.AutoFill Destination:=Range("Y2:Y" & LastRow)
End Sub
It automatically runs a Refresh All to update any queries or formula in the WorkBook and then autofills the list of data in column Y of sheet6 to the last row of data that can be found in the WorkSheet.
When I go to run the code I get a 'Compile Error: Method of data member not found' which highlights.
.Autofill
What I don't understand is that this works perfectly well on an identical spreadsheet, not just this one.
I have also tried the following code which doesn't work on this sheet but does on the other.
Sub Auto_Open()
ActiveWorkbook.RefreshAll
Sheet6.AutoFill_ListSource
End Sub
ListSource is the name of the table in column Y that I am trying to autofill.

Change:
Sheet6.AutoFill Destination:=Range("Y2:Y" & LastRow)
to:
Sheet6.Range("Y2").AutoFill Destination:=Sheet6.Range("Y2:Y" & LastRow)
Note: a "safer" way to get the last row, will be using the Find function:
Dim LastCell As Range
Dim LastRow As Long
With Sheet6
Set LastCell = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
searchorder:=xlByRows, searchdirection:=xlPrevious, MatchCase:=False)
If Not LastCell Is Nothing Then
LastRow = LastCell.Row
Else
MsgBox "Error! worksheet is empty", vbCritical
Exit Sub
End If
End With

Related

Search Todays date in column and select cell

I found a code but I don't why it is not working, I'm barely new to VBA. Please help me..
What I am trying to achieve is I need to Search the day today from another wb.
Here's my complete code:
Sub Sample
Sheets("Database").Select
Dim i as Workbook
Dim c as Workbook
Set i = Workbooks("Workbook1.xlsm")
Set c = Workbooks.Open(FileName:=Range("U2").Value)
'U2 contains the link or path of the file.
ThisWorkbook.Activate
Sheets("Summary").Activate
Windows("Workbook1").Activate
Sheets("Database").Select
Workbooks(2).Activate
Sheets("Summary").Select
Dim FindString As Date
Dim Rng As Range
FindString = CLng(Date)
With Sheets("Summary").Range("A:A")
Set Rng = .Find(What:=FindString, After:=.Cells(.Cells.Count), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlbyColumns, SearchDirection;=xlNext,MatchCase;=False)
If Not Rng Is nothing then
Application.Goto Rng, True
Else
Msgbox "Nothing Then"
End if
End with
End Sub
The other workbook that was recently opened contains Summary Sheet that has Dates on Column A:A
If you're getting a syntax error - the two parameters are defined with semi-colons ";" instead of colons ":"
SearchDirection;=xlNext,MatchCase;=False
becomes
SearchDirection:=xlNext,MatchCase:=False
Fix your syntax before testing - Use Debug | Compile
Activating and Selecting is not necessary and harmful
There are also some syntax errors
You may want to re-start from the following code:
Sub Sample
Dim dbSheet as Worksheet
Dim Rng As Range
Set dbSheet = Workbooks("Workbook1.xlsm").Sheets("Database") 'set the “database” worksheet
With Workbooks.Open(FileName:=dbSheet.Range("U2").Value).Sheets("Summary") 'open the workbook whose link is in “database” sheet cell U2 and reference its “Summary” sheet
With .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)) ' reference referenced sheet column A cells from row 1 down to last not empty row
Set Rng = .Find(What:=CLng(Date), After:=.Cells(.Rows.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) ' try finding current date starting from the cell at the top of referenced range
End With
End With
If Not Rng Is nothing then
Application.Goto Rng, True
Else
Msgbox "Nothing Then"
End if
End Sub
This code is untested but from the explanations in comments you can tweak it to reach the goal

Auto Populate Formula to end of series in excel

I am having an issue with a piece of VBA in excel and am looking for assistance.
I require a piece of code to auto populate a formula in excel through a series of data, the series will vary in length and will occupy columns C:I.
I have been using this piece of code without issue for quiet a while:
Sub Auto_Fill_Formula()
Sheets("Sheet1").Select
Dim LstRow As Long
With Sheets("Sheet1")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A2:A" & LastRow).Formula = "Formula added here"
Application.CutCopyMode = False
End With
End Sub
However as the formula is being added to the leftmost column it only populates the first cell, cell A2.
How can I modify this code to work when the leftmost column is empty?
Thank you,
Wayne
You can use Find to find the last used row in C:I by searching backwards from row 1. You should also use Option Explicit to pick up typos in variable names (LstRow).
Sub Auto_Fill_Formula()
Sheets("Sheet1").Select
Dim LastRow As Long, r As Range
With Sheets("Sheet1")
Set r = .Range("C:I").Find(What:="*", After:=.Range("C1"), Lookat:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
If Not r Is Nothing Then
LastRow = r.Row
.Range("A2:A" & LastRow).Formula = "Formula added here"
End If
End With
End Sub

Copy and Paste Excel Macro for only rows with data

I need help in figuring out a macro to copy and paste only values from rows that contain data (and not a large set range) to another worksheet.
The worksheet named DataEntry is where downtime is entered in columns A - J (could have 2 rows of data or could have 50) and pastes only the values (and not formulas) into another worksheet named DataCombined to column A and on the next available row.
Please help!
Your question is kind of confusing, but this may be where you want to start:
(If you could please give an example sheet with your desired output, that would help)
Sub copyRange()
Dim dataSht As Worksheet, comboSht As Worksheet
Set dataSht = Sheets("DataEntry")
Set comboSht = Sheets("DataCombined")
Dim rngToCopy As Range
Set rngToCopy = dataSht.Range(Cells(2, 1), Cells(getLastFilledRow(dataSht), "J"))
rngToCopy.Copy
comboSht.Range("A" & (getLastFilledRow(comboSht) + 1)).PasteSpecial xlPasteValues
rngToCopy = dataSht.Range(Cells(2, 1), Cells(getLastFilledRow(dataSht), "J")).SpecialCells(xlCellTypeConstants)
rngToCopy.ClearContents
End Sub
'Gets the last row of the worksheet that contains data
Public Function getLastFilledRow(sh As Worksheet) As Integer
On Error Resume Next
getLastFilledRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Run time Error '13' Type Mismatched on using .Find Function in Userform

Here's my code:
Option Explicit
Private Sub CBu_Login_Click()
Dim ws As Worksheet, rng As Range, lrow As Long, find_value As String
Dim cel As Range
Set ws = ThisWorkbook.Sheets("UserName")
lrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("A2:A" & lrow)
find_value = Me.TB_Username.Value
Set cel = rng.Find(What:=find_value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cel Is Nothing Then
If Me.TB_Password.Value = cel.Offset(0, 1).Value Then
UF_Encoding.L_User.Caption = "Welcome " & cel.Offset(0, 2).Value & "!" & " You are logged in."
UF_Encoding.TB_Operator.Text = cel.Offset(0, 2).Value
UF_Encoding.Show
Me.Hide
Else
MsgBox "Invalid Username/Password"
End If
Else
MsgBox "Invalid Username/Password"
End If
End Sub
This code is giving me a Type Mismatched error on the .Find part.
The code is in a Command Button.
Also, this works sometimes and then suddenly will throw up the Mismatched Error.
Please help why it is throwing the error and how to correct it.
I don't want to resort to looping since i have many users.
Avoid the use of ActiveCell, unless there's an absolutely necessary reason to incorporate it.
Please see THIS LINK
Simply change
After:=ActiveCell
to
After:=ws.Range("A2")
One possible solution. I've commented everything, so that you can follow the specific steps.
Sub Hide_Me()
Dim rngHeadings As Range 'the range where your headings are
Dim arrHideColumns() As Variant 'the array, where you list all headings that should be hidden
Dim Item As Variant 'a common variable for the for-each-loop
Dim rngResult As Range 'the range for the search result
'Assign the range, where your headings are
Set rngHeadings = Worksheet1.Range("C3:E3")
'List the headings you want to be hidden
arrHideColumns = Array("Address", "Phone Number")
'Loop through your list
For Each Item In arrHideColumns
'Use the .FIND method of the range-object
'Please read more about this method here: https://learn.microsoft.com/en-us/office/vba/api/excel.range.find
Set rngResult = rngHeadings.Find(Item)
'If there is a result
If Not rngResult Is Nothing Then
'Hide the column
rngResult.EntireColumn.Hidden = False
End If
Next
End Sub

Prevent duplicate copy paste in excel

Hi all the noob is back again. I am doing a copy paste job of data from one worksheet into another which is hidden but there is a danger that data will be duplicated if not checked against what has already been pasted. So far, what I have done is to insert a code in the worksheet I am copying to, to stop the duplication but the complication I now have is that the validation is checking every bit of data throughout the column from start to end and this is about 5000< entries. Column B has the report date which is the same for all entries belonging to the same month end. So it will have say 5000 entries with 30/1/13....another with 28/02/13 etc. Ideally, I want to only check once in Column B where the report date is entered and if the date matches to what I want to copy, then reject the entire copy paste process instead of validating each individual entry in the copy range. Here is the code I am working with. I hope I'm making sense & thank you very much for helping.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Dim ans As String
Const myCol As Long = 2
If Intersect(Target, Columns(myCol)) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In Intersect(Target, Columns(myCol))
If Application.CountIf(Columns(myCol), r.Value) > 1 Then
MsgBox (r.Value & " already exsists")
r.ClearContents
End If
Next
Application.EnableEvents = True
End Sub
thats my code including the remove duplicates but it aint working. I have tried it
Sub LoadData_toTable()
Dim ws1LRow As Long, ws2LRow As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Application.ScreenUpdating = False
Set ws1 = ThisWorkbook.Sheets("RAW DATA")
Set ws2 = ThisWorkbook.Sheets("DATA INPUT")
With ws1
ws1LRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row + 1
End With
With ws2
ws2LRow = .Range("G" & .Rows.Count).End(xlUp).Row
.Range("A2:AR" & ws2LRow).Copy
ws1.Range("A" & ws1LRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End With
With ws1
ws1.Range("A:A").RemoveDuplicates
End With
For Each WS In ThisWorkbook.Worksheets
For Each PT In WS.PivotTables
PT.RefreshTable
Next PT
Next WS
MsgBox "Loading month's data complete!"
End Sub
A very-long-calculation-time-solution would be to create an array containing only data not equal to that already in the sheet (you would have to iterate each copied element and compare to each element in the hidden sheet).
Otherwise you could format the duplicated data in some way, then iterate and remove all the formatted data except the first (which you would re-format as before). Example:
Selection.NumberFormat = "0.0 ""double""" '<--- this could be made also with colors
Selection.FormatConditions.AddUniqueValues
Selection.FormatConditions(Selection.FormatConditions.count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
ExecuteExcel4Macro "(2,1,""0.0 ""double"""")"
Selection.FormatConditions(1).StopIfTrue = False
Then inside iteration
cells(x,y).select
if counted_formatted_data = 1 then
Selection.NumberFormat = "0.0 " '<--- back to the previous formatting
else
selection.delete
end if
Of course it is better if you do this without selecting anything.