This macro runs on the click of a button. I receive an error.
Run-time error '91':
Object variable or With block variable not set
I click Debug and it leads me to this highlighted area.
Selection.Find(What:=DateString, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Here is the entire function
Function GetBalance(Month As Integer) As Currency
'This function is called by the Calculate_Balance subroutine. It
'finds the appropriate month's balance on an employee sheet and sends
'it back to the calling routine.
Dim DateString As String
Dim RangeString As String
Dim Balance
Dim BalDate As Date
Dim strCurrMonth As String
Dim strCurrYear As String
Dim strFirstDayCurrMonth As String
strCurrMonth = CStr(DatePart("m", Date))
strCurrYear = CStr(DatePart("yyyy", Date))
strFirstDayCurrMonth = strCurrMonth & "/1/" & strCurrYear
dtmFirstDayCurrMonth = CDate(strFirstDayCurrMonth)
DateString = Month & "/1/"
Columns("A:A").Select
Range("A6").Activate
Selection.Find(What:=DateString, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
CurrRow = ActiveCell.Row
BalanceRow = CurrRow - 1 'Move up 1 row to get last balance for this month
RangeStr = "E" & BalanceRow
DateRangeStr = "A" & BalanceRow
BalDate = Range(DateRangeStr).Value
If BalDate <= dtmFirstDayCurrMonth Then
Balance = Range(RangeStr).Value
Else
Balance = 0
End If
GetBalance = Balance
End Function
The Find() function returns a Range object so with your code if nothing is found, you will get a error as it can not 'activate' nothing. Change the code to something like:
Dim rng As Range
Set rng = Selection.Find(What:=DateString, After:=ActiveCell, LookIn:=xlFormulas, SearchDirection:=xlNext, MatchCase:=False)
If Not (rng Is Nothing) Then
rng.Activate
End If
Related
i am new to VBA. Task here is to find the address of a cell with respect to a variable.
i am using the find option and receiving error
Object Variable or With block variable not set
Using Find option in VBA. searching data is already available in the sheet. when I do it manually its working.
Sub updateddata()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim currentdate As Date
Dim empid As String
Dim attendance As String
Dim namerange As Range
Dim columnadd As String
Dim rowadd As Range
Dim pasterange As Range
Worksheets("MARK ATTENDANCE").Select
Range("b6").Select
Set namerange = Range(Selection, ActiveCell.End(xlDown))
Range("G5").Select
currentdate = Left(Range("g5").Value, 10)
attendance = ActiveCell.Offset(1, 0).Value
For Each cell In namerange
empid = cell.Value
If attendance = "" Then
ActiveCell.Offset(1, 0).Select
attendance = ActiveCell.Value
Else
Worksheets("Daily Attendance Tracker").Select
Range("1:1").Select
columnadd = Selection.Find(What:=currentdate, After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Address
Range("B2:B37").Select
rowadd = Selection.Find(What:=Trim(empid), After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Address
End If
Next
End Sub
The code below is supposed to take the value for net in each month, copies it, search for net name in range1(another worksheet) and pastes value in the cell corresponding to that row and column "AA".
This part of code is having issue:
Set Netrng = Range("AA" & Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Rows.row)
The error is -
object variable or with block variable not set.
what am I doing wrong?
Sub test()
Dim Range2 As Range
Dim lRow As Long
Dim Count As Long
Dim Net As String
Dim Line As Range
Dim Netrng As Range
Dim First As Range
Dim Range1 As Range
Dim wb As Worksheet
Set First = ActiveCell
Set wb = ActiveSheet
Set Range1 = wb.Range(First, First.End(xlDown))
ActiveWindow.ActivatePrevious
ActiveSheet.PivotTables("PivotTable1").PivotFields("Client Code").CurrentPage _
= "BUN"
ActiveSheet.Range("B5").Activate
lRow = Cells(Rows.Count, 1).End(xlUp).row - 6
Set Range2 = Range(ActiveCell.Offset(2, -1), ActiveCell.Offset(lRow, -1))
Set Months = Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 2))
Count = 1
While Count <= Range2.Count
Set Line = Range2.Rows(Count)
Net = Line.Value
Line.Offset(0, 1).Copy
ActiveWindow.ActivatePrevious
Set Netrng = Range("AA" & Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).row)
Netrng.Offset(0, 4).PasteSpecial Paste:=xlPasteValues
Netrng.Value = 0
ActiveWindow.ActivatePrevious
Line.Offset(0, 2).Copy
ActiveWindow.ActivatePrevious
Netrng.Offset(0, 8).PasteSpecial Paste:=xlPasteValues
ActiveWindow.ActivatePrevious
Count = Count + 1
Wend
End Sub
As is, the code is assuming that the Find is successful, which may not always be the case.
To test:
Dim foundRng as Range
Set foundRng = Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not FoundRng is Nothing Then
Set Netrng = Range("AA" & foundRng.Row)
...
End If
Other recommendations:
Avoid using Select and Activate. (and ActiveCell, ActiveWindow, anything Active).
Fully qualify which Workbook and Worksheet each Range is on (helpful reading in the answer on avoiding Select).
While...Wend is old-fashioned. Use a For Each loop.
I am trying to move the first instances of each value of the column "firstname" from one sheet to another. I am able to move the first instance of the first cell value from the firstname column, but I am not able to move the next and so on till the end. Below is the code I am working with at the moment.
Is there any problem with the for loops?
Option Explicit
Private Const RAW_DATA_SHEET = "UserDetails"
Private Const REPORT_SHEET = "Report"
Private Const USER_NAME_COL = "User Name"
Private Const FIRST_NAME_COL = "First Name"
Private Const LAST_NAME_COL = "Last Name"
Private Sub CommandButton1_Click()
Dim firstname As String
Dim firstnameActive As Range
Dim sheet As Worksheet
Dim sheetre As Worksheet
Dim userNameHeader As Range
Dim firstnameHeader As Range
Dim userToFindRow As Range
Dim user As String
Dim curcell As Range
Application.ScreenUpdating = False
Set sheetre = Sheets("Report")
Set sheet = Sheets("UserDetails")
For Each firstnameHeader In sheet.Range("B2" & sheet.Range("B" & sheet.Cells.Rows.Count).End(xlUp).row)
Set curcell = sheetre.Range("A3")
' Find the columns of "User Name"
Set userNameHeader = sheet.Rows(1).Find(What:=USER_NAME_COL, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'note: SearchOrder:=xlByColumns
Set firstnameHeader = sheet.Rows(1).Find(What:=FIRST_NAME_COL, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'note: SearchOrder:=xlByColumns
' Use to find the row number of the provided user
Set userToFindRow = sheet.Columns(userNameHeader.Column).Find(What:=user, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'note: SearchOrder:=xlByRows
For Each userToFindRow In sheet.Range("E2" & sheet.Range("E" & sheet.Cells.Rows.Count).End(xlUp).row)
' Get the value of "firstname" cell for provided user
Set firstnameActive = sheet.Cells(userToFindRow.Column, firstnameHeader.Column)
If Not IsEmpty(firstnameActive) Then
curcell.Value = firstnameActive
Set curcell = curcell.Offset(1)
End If
Next userToFindRow
Next firstnameHeader
Application.ScreenUpdating = True
End Sub
why the loop is not working?
Sathak, I think you need to change your For Each Loop. Syntax of For each is we tend to search in defined range. You need to add :B & :E in both for each range to make loop work.
Check the modified code and try to use this For Each loop in your code:
First For each loop:
For Each firstnameHeader In sheet.Range("B2:B" & sheet.Range("B" & sheet.Cells.Rows.Count).End(xlUp).Row)
Second For each loop:
For Each userToFindRow In sheet.Range("E2:E" & sheet.Range("E" & sheet.Cells.Rows.Count).End(xlUp).Row)
I got this code off here earlier today and just tried to adapt it. I'm receiving Run Time Error 13 on the line Set Rng = and I'm not quite sure why(?) Any help appreciated.
Sub PlayMacro()
Dim Prompt As String
Dim RetValue As String
Dim Rng As Range
Dim RowCrnt As Long
Prompt = ""
With Sheets("Claims")
Do While True
RetValue = InputBox(Prompt & "Give me a value to look for")
'RetValue will be empty if you click cancel
If RetValue = "" Then
Exit Do
End If
' I do not wish to active the cell containing the required value.
' I want to know where it is.
Set Rng = .Range("Table1").Find(What:=RetValue, After:=.Range("Table1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
' The entered value could not be found
Prompt = "I could not find """ & RetValue & """"
Else
' The entered value was found
RowCrnt = Rng.Row
Prompt = "I found """ & RetValue & """ on row " & RowCrnt
End If
Prompt = Prompt & vbLf
Loop
End With
End Sub
try to delete After:=.Range("Table1"), in the line Set Rng = .Range("Table1").Find(... :
Set Rng = .Range("Table1").Find(What:=RetValue, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
1
2
3
4
.
.
So I have a sequence of numbers running from 1-20. I have the number "1" on top selected and I would like to search the entire column and find the number "9". The code works when I don't name the range "rng"; it finds the number and selects. But the code stops working when I name the range of number. What's wrong with the range function? could it be that if I define Dim rng as Range that when I later define the "Set rng=" I cannot have the ".Select" or ".Copy" extension on the end?
Sub macro2()
Dim rng As Range
Set rng = Range(ActiveCell, ActiveCell.End(xlDown)).Select
rng.Find(10).Select
End Sub
Also, If I want to sum the entire column from 1-20, on the last cell below the number "20" should I use the following code? because the application object doesn't seem to do it. Thank you!
rng.End(xlDown).Offset(1, 0).Select
Application.WorksheetFunction.Sum (rng.Value)
To look for 10 in the active column you could try this (which ends up selecting the first 10 - although Select in vba isn't normally needed other than taken the user to location at code end)
test that the found range exists (ie you can find 10 before proceeding)
you should also use xlWhole to avoid matching 100 if the current default for [lookAt] is xlPart
using search [After] as Cells(1, ActiveCell.Column , and [Search Direction] as xlNext finds the first value looking down.
code
Sub QuickFind()
Dim rng1 As Range
Set rng1 = ActiveCell.EntireColumn.Find(10, Cells(1, ActiveCell.Column), xlFormulas, xlWhole, , xlNext)
If Not rng1 Is Nothing Then
Application.Goto rng1
Else
MsgBox "10 not found"
End If
End Sub
Part 2
Sub Other()
Dim rng1 As Range
Set rng1 = Range(Cells(1, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp))
rng1.Cells(rng1.Cells.Count).Offset(1, 0) = Application.WorksheetFunction.Sum(rng1.Value)
End Sub
Try this, I hope this will help u to find the specific row no as well as column name too. In code you can use
strRw = FindColumn(Sheet name, "Value which need to be found", True, "Cell Name",Row number)
sourceCOL = colname(FindColumn(Shee Name, "Value which need to be found", False, , 4))
Below is main function of find
Public Function FindColumn(colnocountWS As Worksheet, srcstr As String, Optional rowflag As Boolean, Optional bycol As String, Optional strw As Integer, Optional stcol As Integer) As Integer
Dim srcrng As Range 'range of search text
Dim srcAddr As String 'address of search text
Dim stcolnm As String
colnocountWS.Activate
If stcol <> 0 Then stcolnm = colname(stcol)
If stcol = 0 Then stcolnm = "A"
If strw = 0 Then strw = 1
colnocountWS.Range(stcolnm & strw).Select
If ActiveSheet.Range(stcolnm & strw) = srcstr Then
ActiveSheet.Range(stcolnm & strw).Select
FindColumn = 1
Else
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
'ByPart
If srcrng Is Nothing Then
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
End If
If srcrng Is Nothing Then
FindColumn = 0
Exit Function
Else
srcAddr = srcrng.Address
colnocountWS.Range(srcAddr).Select
FindColumn = ActiveCell.Column
If rowflag = True Then FindColumn = ActiveCell.Row
End If
End If
End Function
'this function find column name
Public Function colname(iFinalCol1 As Integer) As String
Dim colnm As String
On Error GoTo gg
If Mid(Cells(1, iFinalCol1).Address, 3, 1) = "$" Then
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 1)
Else
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 2)
End If
gg: colname = colnm
End Function