searching for header

ian0886

New Member
Joined
Dec 10, 2016
Messages
42
Hi,

I'm trying to find the column from the header and using the result, to format the entire column. below is what i've writen.

Code:
Dim lnRow As Long

lnRow = Worksheets("ADSS").Cells("1:1").Find(What:="Party", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
               
Range("inrow:inrow").NumberFormat = "0"

When i'm running it i face an error 'invalid procedure or argument', i can't seem to figure out why.

Thanks in advance
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello ian0886,

The Find method returns a Range object. If Find is successful then a range object is returned. If not then the special Object variable Nothing is returned.

The problem is you are trying to set a variable that has been typed as Long to an Object that is a Range. A better practice is to type the variable as a Range.

Columns are vertical and Rows are horizontal across the worksheet.

Code:
    Dim Cell    As Range
    Dim Headers As Range

        Set Headers = Worksheets("ADSS").Range("A1", Cells(1, Columns.Count).End(xlToLeft))
        
        Set Cell = Headers.Find(What:="Party", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)

        If Cell Is Nothing Then
            MsgBox "Header was Not Found."
            Exit Sub
        End If

        Cell.EntireColumn.NumberFormat = "0"
 
Upvote 0
Sorry, but i'm trying to do the same but instead now i wish to find the blank spaces and subsequently replace all with comma.

Code:
Set Cell2 = Headers.Find(What:="Amount", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)

Cell2.EntireColumn.Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


selction.Style = "Comma"

Why won't it work the same way. sorry i've just starting learning VBA
 
Upvote 0
Hello ian0886,

In VBA, it is rarely necessary to either Select or Activate an Object. This code should work...
Code:
Set Cell2 = Headers.Find(What:="Amount", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)


Cell2.EntireColumn.Replace What:=" ", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top