• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Object variable or with block variable not set - VBA

trprasad78

Member
Hi all,

I don't know what mistake i did , below code is working in my PC , IF try the same code with different system i am not getting result.

while debug [F8] code if i keep the mouse cursor on top of "aCell.Column"
it showing "Object variable or with block variable not set "

Please suggest

Code:
Sub UpdateU2()
'file name Project Cost.XLSX
'Store Cost code
Worksheets("Xs1").Select
Dim Ccode As String
Ccode = Range("d2").Value

    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
 
    Dim src As Workbook
 
    ' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
    Set src = Workbooks.Open("E:\XS DATA UPDATE\workingfile\PROJECT COST.xlsx", True, True)
 
    ' GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK.
    Dim iTotalRows As Integer
    iTotalRows = src.Worksheets("Project Cost").Range("a1:a" & Cells(Rows.Count, "A").End(xlUp).Row).Rows.Count
   '==========================================

    Dim strSearch As String
    Dim aCell As Range
 
 
    Dim ws As Worksheet
    Set ws = Worksheets("Project Cost")
 
    strSearch = "USD"
 
With ws
    Set aCell = ws.Rows(2).Find(What:=strSearch, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
End With

Dim USDColumnNumber As Long


USDColumnNumber = aCell.Column

  debug.print USDColumNumber
end sub

My main intention is to find the column number by searching the Text which the cell contain.
 
Last edited:

This issue comes when nothing is found,
see VBA help and its sample how to use Find method
'cause you forgot to check the result …​
 
Back
Top