VBA Find first empty cell in row

PBG

New Member
Joined
Sep 15, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am trying to find the first empty cell in a row using FIND or MATCH, preferably MATCH. Below are the various ways I have attempted to write the code. in most of the examples I have tried either "" or " " to search. Also with using Find I have tried the search direction of xlprevious and xlnext. The different variations result in finding the first cell in the range, which contains text. Or it does not find an empty cell given the result is 0.

CID = Sheet1.Range("1:1").Find(" ", LookAt:=xlWhole)
CID = Sheet1.Range("1:1").Find("", LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlColumns, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
CID = Sheet1.Range("1:1").Find("", LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlColumns, _
SearchDirection:=xlPrevious, MatchCase:=False).Row '
CID = WorksheetFunction.Match("ORCID", Sheet1.Range("1:1"), 0)
CID = WorksheetFunction.Match(True, IsEmpty(Sheet1.Range("1:1")), 0)

The reason for specifically using FIND or Match is Sheet1, used for this example, is a report which is downloaded weekly from a server. The server report has a range of cells without any empty cells in the range. The headers for each column are in row 1 and the code normally searches for the headers. The server report occasionally changes without communication of the change and the column header can be changed or the entire column is removed from the report. The users of the report do not have knowledge of VBA but have knowledge of Excel. I am retiring soon and attempting to develop instructions on how to modify the code should the column header in the server report change. In the case of the column header changing the new column header can replace the old column header. But if the column is removed from the server report, which occasionally happens, I hope and thought was to direct the user(s) to remove the text from in between the " " to search for the first empty cell in the row. Which in my tested does is not possible or I have written the code wrong. My thought was if the code finds the first empty column then it would copy empty cells, given the code places the contents of the first used row to the last used row in a column, into an array which is later paste into the report being developed. This empty column can be hidden by the user.

The following line of code represents the current method I am using for searching the server report for the headers - CID = WorksheetFunction.Match("ORCID", Sheet1.Range("1:1"), 0). ORCID is declared as a string and is assigned to one of the headers. It appears at the beginning of the sub in this manner - ORCID = "Case_ID". The instructions indicate to replace the Case_ID with the new header appearing in the server report which contains the information for the report being produced.

I have reviewed various responses to similar posts in a number of forums. My question has not been posted in any other forum.

Thank you for your time and assistance. Any assistance or suggestions are appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You don't need to actually select the cell like I did. I just included it to illustrate the method.

VBA Code:
Sub FindFirstEmptyColumn()
Dim c As Range
Set c = Range("A1").End(xlToRight).Offset(, 1)
c.Select
End Sub
 
Upvote 0
Scott,

Thank you for your suggestion. I am wanting to use either FIND or MATCH given it would be difficult for the users to make that type of modification to the code given they have no knowledge of VBA.
 
Upvote 0
You can try either for the first blank or empty cell
VBA Code:
Rows("1:1").Find("", , xlValues, xlWhole, xlByColumns, xlNext).Select
or
VBA Code:
Rows("1:1").SpecialCells(4).Cells(1).Select

but IMHO I don't believe either are easier to maintain for someone "given they have no knowledge of VBA" than post 2 by @Scott Huish
 
Upvote 0
Mark858,

Thank you for your response. Row 1 contains only text headers, no numbers or zeros. At this time there are 31 columns, but again that changes. In testing Rows("1:1").Find("", , xlValues, xlWhole, xlByColumns, xlNext).Select , of course it was modified to my needs, the result was 1 (column) which contains a text header. When I place a space between " " the results are 0. Again, there are no 0s in the header row (1) only text. Currently the system report contains 31 columns. Any other thoughts on how to modify using FIND or MATCH?
 
Upvote 0
What are you actually trying to do find the first blank cell or find the first cell after the last used used cell in the row?
of course it was modified to my needs, the result was 1 (column) which contains a text header.
I don't get this result with the Find code (Specialcells will show an error in the 1st example as there are no blank cells in the used range) that I posted, with
Book1
ABCDEFGHI
1Order IDOrder DateSiteSP NameCategory NameShip ViaQuantityOrder Total
2109-19-F6119/03/2014Off SiteLaura CallahanGrains/CerealsRoad6164.5818
3108-2-N6302/02/2014On SiteMargaret PeacockBeveragesSea20383.3364
4103-22-N3522/10/2012On SiteRobert KingBeveragesRail7145.0422
5109-31-F8931/03/2014Off SiteAndrew FullerDairy ProductsSea15398.7264
6109-30-N8430/03/2014On SiteNancy DavolioBeveragesRoad20343.3908
Sheet1

It selects I1

With
Book1
ABCDEFGH
1Order IDOrder DateSiteCategory NameShip ViaQuantityOrder Total
2109-19-F6119/03/2014Off SiteLaura CallahanGrains/CerealsRoad6164.5818
3108-2-N6302/02/2014On SiteMargaret PeacockBeveragesSea20383.3364
4103-22-N3522/10/2012On SiteRobert KingBeveragesRail7145.0422
5109-31-F8931/03/2014Off SiteAndrew FullerDairy ProductsSea15398.7264
6109-30-N8430/03/2014On SiteNancy DavolioBeveragesRoad20343.3908
7104-13-N7413/03/2013On SiteSteven BuchananProduceRoad12349.6266
8107-24-N5124/11/2013On SiteJanet LeverlingSeafoodAir301034.5386
Sheet1

It selects D1

Neither code returns a number in my original code. I would look at your modification or your cell contents

Edit:
When I place a space between " " the results are 0.
It won't find anything because you are looking for a space character and not an empty string, I don't know what modification you made because .Column can't return a zero
 
Last edited:
Upvote 0
Thank you Mark.

I will review my code for any errors next week when I return to work.

Appreciate your time.
 
Upvote 0
What are you actually trying to do find the first blank cell or find the first cell after the last used used cell in the row?
If your answer to the question above is the 2nd part then you need to go about it with a different method to any of the codes posted
 
Upvote 0
Solution
Thank you for your time on this Mark.

My answer would be the 2nd part and I believe I have figured out a different method.

P
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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