Couple of macro questions

chuckles1066

Banned
Joined
Dec 20, 2004
Messages
372
I'm slowly getting my head around VBA and hopefully one day will be able to give something back to this great forum.

In the meantime, a couple of quickies:

(a) whats the code to copy or identify the complete range of data in a sheet?

(b) if I have a macro that assumes that another file is open, how do I generate an error message if the file isn't in fact open? And how do I stop the macro from stopping unceremoniously?

TIA.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This will select the used range
Code:
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
 
Upvote 0
This is one way to test if a workbook is open...

Code:
    Dim wb As Workbook
    
    On Error Resume Next                [COLOR="Green"]' Suspend error checking[/COLOR]
        Set wb = Workbooks("Test.xls")  [COLOR="Green"]' Name of open workbook to test for[/COLOR]
    On Error GoTo 0                     [COLOR="Green"]' Resume Error checking[/COLOR]
    
    If Not wb Is Nothing Then
        MsgBox "The workbook ""Test.xls"" is open."
    Else
        MsgBox "The workbook ""Test.xls"" is not open."
    End If
 
Upvote 0
This will select the used range
Code:
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

What if I want to simply identify that Sheet2 has data in cells A2:I42000?

Or cells A2:B3?

I just need a one-off bit of code that identifies last row and last column of data.
 
Upvote 0
This is one way to test if a workbook is open...

Code:
    Dim wb As Workbook
    
    On Error Resume Next                [COLOR="Green"]' Suspend error checking[/COLOR]
        Set wb = Workbooks("Test.xls")  [COLOR="Green"]' Name of open workbook to test for[/COLOR]
    On Error GoTo 0                     [COLOR="Green"]' Resume Error checking[/COLOR]
    
    If Not wb Is Nothing Then
        MsgBox "The workbook ""Test.xls"" is open."
    Else
        MsgBox "The workbook ""Test.xls"" is not open."
    End If

Will that work if the open file is a text file (albeit opened in Excel)?
 
Upvote 0
What if I want to simply identify that Sheet2 has data in cells A2:I42000?

Or cells A2:B3?

I just need a one-off bit of code that identifies last row and last column of data.

There are a couple of methods to determine the last cell on a sheet, in a row\column, within a range, etc. Each method has its own advantages, disadvantages, and limits. Here is a good site with example code on the various methods.
http://www.ozgrid.com/VBA/ExcelRanges.htm

Also, if you search this forum for something like last used cell, you will find several replies to this question.
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,487
Members
452,917
Latest member
MrsMSalt

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