Problem with Cells and Offset

pragmath

New Member
Joined
Sep 1, 2014
Messages
13
My question is quite simple but it drives me nuts!
Cell AN10, i.e. row 10 and column 40 contains the value 8. I am reading it into the integer Time using the VBA code

Time = Cells(4,3).Offset(6,37)

However the result is Time = 3, wich happens to be the value in cell AO10, i.e. cell(10,41). I have tried

Time = Cells(4,col)Offset(6,40 - col)

for values of col between 1 and 40 and I get the correct value 8 except for col = 3 and col = 38.
Is it a VBA bug or am I missing something very, very basic?

Thanks for any an help.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Welcome to the MrExcel board!

The basic structure look okay to me but Time is a vba Keyword (refer vba Help Time function and Time statement) so I would advise against using it as a variable name .
 
Upvote 0
Hi and welcome to the board.

I tried your code and it works fine:
Code:
Sub test()

    Dim lTime As Long


    lTime = Sheet1.Cells(4, 3).Offset(6, 37)
    
End Sub

The problems I can see are:

  • You're using Time as a variable as it's a reserved word in VBA - it returns the system time and when I tried it I got a data type mismatch.
  • Your code doesn't explicitly state the sheet it's looking at - are you sure it's looking at the right sheet?

In the Immediate window this returns $AN$10:
Code:
?thisworkbook.worksheets("Sheet1").Cells(4, 3).Offset(6, 37).address
 
Upvote 0
That's exactly the problem, and likewise for cells AL4:AM4 that are merged. Merging cells is very dangerous, but sometimes this is needed to have nice headings.

Thanks an awful lot.
 
Upvote 0
For the record, you can often avoid merging cells, and still accomplish the same aesthetic effects, but using Center across Selection located on the Alignment tab of the Format Cells dialog box, under Horizontal.
 
Upvote 0
I use the Center across selection option in VBA but I didn't know it is available in Excel. I will now use it systematically.
Thanks for the info
 
Upvote 0

Forum statistics

Threads
1,224,392
Messages
6,178,313
Members
452,839
Latest member
grdras

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