Меню

Invalid or unqualified reference vba ошибка word

snipe, спасибо, j исправил.
про объявление переменных учту, но сейчас главное что бы программа работала корректно, оформлять код буду позже.

Так, всем интересующимся рассказываю, как должен работать макрос:
действующие столбцы: B, G, H.
просматриваем столбец B на одинаковые значения (они, повторяясь, сначала увеличиваются, потом уменьшаются).
считаем среднее арифметическое соответствующих значений в столбца G (у строк которых значение столбца B одинаковое). Далее вывод результата среднего арифметического в столбец H.

после нескольких дней одиночного мозгового штурма у меня получился вот такой код…

Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Option Base 1
Sub M2()
 
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim arrA() As Single
Dim s As String
 
Sheets("ID_1").Select
 
s = "(" & Format(Cells(2, 7), "#,###0.000") & ")"
j = 1
ReDim Preserve arrA(1)
arrA(j) = Cells(2, 2)
n = 1
 
For i = 2 To 10
For j = LBound(arrA) To UBound(arrA)
If Cells(i, 2) <> arrA(j) Then
    ReDim Preserve arrA(j + 1)
    arrA(j) = Cells(i, 2)
Else:
    s = s & "+" & "(" & Format(Cells(i + 1, 7), "#,###0.000") & ")"
    n = n + 1
End If
    
Cells(15 + i, 2) = i
Cells(15 + i, 3) = j
Cells(15 + i, 4) = arrA(j)
Cells(15 + i, 5) = s
    
Cells(i, 8) = "(" & s & ")" & "/" & n
 
Next j
Next i
 
End Sub

объясняю. использовал в качестве фильтра (что б не повторялись значения столбца B) динамический массив. т.е. сначала в нём хранится значение первой значимой ячейки столбца B. потом, при нахождении в столбце B ещё одного нового значения — кол-во эл-тов массива увеличивается и последний элемент принимает новое значение из столбца B. далее снова проверка на схожесть.

наверное, можно было и без массива обойтись, просто как-то отсортировать столбец B с зависимыми значениями столбца G.
ещё была идея сделать двумерный массив типа String, первой мерой которого будет являться тот самый исключающий массив, а второй мерой — строка вычисления среднего арифметического.

не получилось корректно реализовать заполнение исключающего массива и вывод среднего арифметического

как-то так. возможно, ещё как предлагал в теории Hugo121, проще переписать код с другим алгоритмом
буду благодарен а любую помощь и дельные советы.

черновой вариант листа с данными приложил. ещё прикреплён скрин с ответом. так должны выглядеть данные в столбце H после использования макроса (можно и сразу значение среднего арифметического).

вообще макрос будет работать на 4-х страницах, по ~2000 строк в каждой. а сейчас просто тест, 1 страница с десятью строками данных.

I’m trying to create excel template (the volume of data will be different from case to case) and it looks like this:

enter image description here

In every even row is «Customer» and I would like to put in every odd row «Ledger». Basically it should put «Ledger» to every odd row until there are data in column C. I have this code:

'========================================================================
' INSERTING LEDGERS for every odd row (below Customer)
'========================================================================

Sub Ledgers()

    Dim rng As Range
    Dim r As Range
    Dim LastRow As Long

    LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set rng = .Range("C5:C" & LastRow)

    For i = 1 To rng.Rows.Count
        Set r = rng.Cells(i, -2)
        If i Mod 2 = 1 Then
            r.Value = "Ledger"
        End If

    Next i

End Sub

But it gives me an error msg Invalid or unqualified reference. Could you advise me, where I have the error, please?

Many thanks!

Pᴇʜ's user avatar

Pᴇʜ

55.8k9 gold badges49 silver badges72 bronze badges

asked Sep 8, 2017 at 12:35

Srpic's user avatar

1

If a command starts with . like .Cells it expects to be within a with statement like …

With Worksheets("MySheetName")
    LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set rng = .Range("C5:C" & LastRow)
End With

So you need to specify the name of a worksheet where the cells are expected to be in.

Not that it would be a good idea to use Option Explicit at the top of your module to force that every variable is declared (you missed to declare i As Long).

Your code could be reduced to …

Option Explicit 

Public Sub Ledgers()
    Dim LastRow As Long
    Dim i As Long

    With Worksheets("MySheetName") 
        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

        'make sure i starts with a odd number
        'here we start at row 5 and loop to the last row
        'step 2 makes it overstep the even numbers if you start with an odd i
        'so there is no need to proof for even/odd
        For i = 5 To LastRow Step 2 
            .Cells(i, "A") = "Ledger" 'In column A
            '^ this references the worksheet of the with-statement because it starts with a `.`
        Next i
    End With
End Sub

answered Sep 8, 2017 at 12:39

Pᴇʜ's user avatar

PᴇʜPᴇʜ

55.8k9 gold badges49 silver badges72 bronze badges

0

Just loop with a step 2 to get every other row in your indexer variable.

Sub Ledgers()
    Dim rng As Range
    Dim LastRow As Long

    LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
    Set rng = ActiveSheet.Range("C5:C" & LastRow)

    For i = 1 To LastRow step 2
        rng.Cells(i, 1) = "Ledger" 'In column A
    Next i
End Sub

answered Sep 8, 2017 at 12:43

Rik Sportel's user avatar

Rik SportelRik Sportel

2,6511 gold badge13 silver badges23 bronze badges

3

  • #2

Sorry the line of code highlighted when i get the error is

Code:

Set toprow = .Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _

  • #3

Hello,

When I run this code I get a compile error that says «Invalid or unqualified reference» when I looked into it the error seems to be saying that I am not using a With statement properly. But I am not using a with statement at all.

Has anyone got any suggestions?

Code:

Sub deletingdata()
 
Dim i           As Integer
Dim j           As Integer
Dim WS          As Worksheet
Dim toprow      As Range
Dim lastrow     As Range
Dim AFcolumn    As Range
 
i = 0
For Each WS In Worksheets
    i = i + 1
    Sheets(WS.Name).Activate
    
    If i <> 1 Then
    
        Range("D:D").Cells.Replace What:="nan", Replacement:="", LookAt:=xlPart, SearchOrder _
            :=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        
        On Error Resume Next
        Columns("D:D").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        ActiveSheet.UsedRange
        Set toprow = .Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
                            SearchDirection:=xlNext, MatchCase:=False).Offset(0, 3).Row
                        
        Set AFcolumn = .Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
                            SearchDirection:=xlNext, MatchCase:=False).Column
                        
        lastrow = Range("AFCol & 65536").End(xlUp).Row
        For j = lastrow To toprow Step -1
        If Cells(j, AFcolumn).Value <> "00*555" Then Cells(j, o).Delete shift:=xlUp
        Next j
        
    End If
    
End Sub

Thanks in advance!

I believe that after your End If and before your End Sub you need a Next.

  • #4

Hello,

Thanks for the reply, looking at the macro now I see that you are correct for it to run properly I do need another next.

However, the bad news is I still have the same error appearing when I run this macro with the same line of data highlighted.

Any other suggestions?

  • #5

Lastrow should be dimmed as Long
Also, shouldn’t this line be

Code:

lastrow = Range("AFCol & 65536").End(xlUp).Row

Code:

lastrow = Cells(Rows.Count, "AFCol").End(xlUp).Row

AND
I think AFCol should be AFColumn !
If you choose Option Explicit in your VBA preferences it would eliminate or identify a lot of syntax errors for you.

Last edited: Sep 24, 2010

  • #6

Thanks for the help guys, Michael I will look into option explicit.

I’m sorry to say though I am still getting the same fault code, if anyone is able to suggest how to fix it, that would be much appreciated.

It says «Compile error: Invalid or unqualified reference»

and the line

Code:

Set toprow = .Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False).Offset(0, 3).Row

is highlighted

anyway here is the rest of the code…

Code:

Sub DeletingExcessData()
Dim i           As Integer
Dim j           As Integer
Dim WS          As Worksheet
Dim toprow      As Range
Dim lastrow     As Long
Dim AFcolumn    As Range
i = 0
For Each WS In Worksheets
    i = i + 1
    Sheets(WS.Name).Activate
    
    If i <> 1 Then
    
        Range("D:D").Cells.Replace What:="nan", Replacement:="", LookAt:=xlPart, SearchOrder _
            :=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        
        On Error Resume Next
        Columns("D:D").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        ActiveSheet.UsedRange
        Set toprow = .Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
                            SearchDirection:=xlNext, MatchCase:=False).Offset(0, 3).Row
                        
        Set AFcolumn = .Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
                            SearchDirection:=xlNext, MatchCase:=False).Column
                        
        lastrow = Cells(Rows.Count, "AFCol").End(xlUp).Row
        For j = lastrow To toprow Step -1
        If Cells(j, AFcolumn).Value <> "00*555" Then Cells(j, o).Delete shift:=xlUp
        Next j
        
    End If
    
    Next
    
End Sub

Thanks again for the help people!

  • #7

The most probable reason for this error is because the Find isn’t, well, finding anything.

Also why are toprow and AFColumn declared as Ranges? Isn’t just the integer value you want.

And why is ActiveSheet.UsedRange just kind of sitting in the middle, it must be feeling left ouy.:)

Try this, it compiles and runs but I don’t know if it does what you want — nothing to test it on.

Code:

Option Explicit
 
Sub deletingdata()
Dim I As Long
Dim J As Long
Dim ws As Worksheet
Dim rngFnd As Range
Dim toprow As Long
Dim lastrow As Long
Dim AFcolumn As Long
 
    For Each ws In Worksheets
 
        I = I + 1
 
        If I <> 1 Then
            With ws
                .Range("D:D").Cells.Replace What:="nan", Replacement:="", LookAt:=xlPart, SearchOrder _
                                                                                          :=xlByColumns, MatchCase:=False
                '.Columns("D:D").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
                Set rngFnd = .UsedRange.Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
                                             SearchDirection:=xlNext, MatchCase:=False)
 
                If Not rngFnd Is Nothing Then
                    toprow = rngFnd.Offset(0, 3).Row
                End If
 
                Set rngFnd = .UsedRange.Find(What:="ActiveFaults", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
                                             SearchDirection:=xlNext, MatchCase:=False)
 
                If Not rngFnd Is Nothing Then
                    AFcolumn = rngFnd.Column
                End If
 
                lastrow = .Cells(Rows.Count, AFcolumn).End(xlUp).Row
 
                For J = lastrow To toprow Step -1
                    If .Cells(J, AFcolumn).Value <> "00*555" Then
                        .Cells(J, 0).Delete shift:=xlUp
                    End If
                Next J
 
            End With
 
        End If
 
    Next ws
 
End Sub

  • #8

Wow you guys are running rings around me :rolleyes:

Thanks for your help Norie, it doesn’t do exactly what I am after, but as you said you do not have any data.

Anyway I think that I should be able to play around with it to make it do what I need with some slight alterations.

Thanks for everyone’s contributions!

  • #9

Surely the .Find commands are the unqualified references? The compiler is complaining that there isn’t a With statement so it doesn’t know what to stick in front of .Find to qualify it completely.

Should it be just plain Find?

  • #10

Ruddles

Did you try Find just on it’s own?

Won’t work I’m afraid — Sub or Function not defined…

Though you are right about the Find not referring to anything.

That would have kicked off a compile error, I thought the OP was getting a run-time error.

That’s why I said the Find wasn’t finding anything.

I think the code I posted dealt with both problems, the one you point out and the possible problem I spotted.:)

If a command starts with . like .Cells it expects to be within a with statement like …

With Worksheets("MySheetName")
    LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set rng = .Range("C5:C" & LastRow)
End With

So you need to specify the name of a worksheet where the cells are expected to be in.

Not that it would be a good idea to use Option Explicit at the top of your module to force that every variable is declared (you missed to declare i As Long).

Your code could be reduced to …

Option Explicit 

Public Sub Ledgers()
    Dim LastRow As Long
    Dim i As Long

    With Worksheets("MySheetName") 
        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

        'make sure i starts with a odd number
        'here we start at row 5 and loop to the last row
        'step 2 makes it overstep the even numbers if you start with an odd i
        'so there is no need to proof for even/odd
        For i = 5 To LastRow Step 2 
            .Cells(i, "A") = "Ledger" 'In column A
            '^ this references the worksheet of the with-statement because it starts with a `.`
        Next i
    End With
End Sub

08-23-2005, 07:05 PM


#1

Re: Another invalid or unqualified reference

«davegb» <davegb@safebrowse.com> wrote in message
news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> Still working on this code:
>
> Sub CtyMatch()
> Dim strOrig, strOutcomes As String
> Dim rCell, rTOCtyLst As Range
> Dim iOrigCityNo, iEndRow As Integer
>
> strOrig = ActiveSheet.Range(«A2»)
> iOrigCityNo = Left(strOrig, 2)
> iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
>
> But am getting an «invalid or unqualified reference» error on the last
> line, .Cells being highlighted. I copied this line from another thread
> here so I’m stumped as to why it doesn’t recognize «.Cells» here but
> does elsewhere in a nearly identical situation.
> Any suggestions?
> Thanks.
>


08-23-2005, 07:05 PM


#2

Re: Another invalid or unqualified reference

I’ve only glanced at your code but try

with activesheet
Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
end with

this qualifies the dot that precedes your Cells to the sheet. Alternatively
just remove those dots if, and only if you are working with the active
sheet.

It would be a good idea to qualify your variables
Dim strOrig as string, strOutcomes As String
Dim rCell as range, rTOCtyLst As Range
Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows

Regards,
Peter T

«davegb» <davegb@safebrowse.com> wrote in message
news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…


> Still working on this code:
>
> Sub CtyMatch()
> Dim strOrig, strOutcomes As String
> Dim rCell, rTOCtyLst As Range
> Dim iOrigCityNo, iEndRow As Integer
>
> strOrig = ActiveSheet.Range(«A2»)
> iOrigCityNo = Left(strOrig, 2)
> iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
>
> But am getting an «invalid or unqualified reference» error on the last
> line, .Cells being highlighted. I copied this line from another thread
> here so I’m stumped as to why it doesn’t recognize «.Cells» here but
> does elsewhere in a nearly identical situation.
> Any suggestions?
> Thanks.
>


08-23-2005, 07:05 PM


#3

Another invalid or unqualified reference

Still working on this code:

Sub CtyMatch()
Dim strOrig, strOutcomes As String
Dim rCell, rTOCtyLst As Range
Dim iOrigCityNo, iEndRow As Integer

strOrig = ActiveSheet.Range(«A2»)
iOrigCityNo = Left(strOrig, 2)
iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))

But am getting an «invalid or unqualified reference» error on the last
line, .Cells being highlighted. I copied this line from another thread
here so I’m stumped as to why it doesn’t recognize «.Cells» here but
does elsewhere in a nearly identical situation.
Any suggestions?
Thanks.


08-23-2005, 08:05 PM


#4

Re: Another invalid or unqualified reference

The range should be dot qualified also

with activesheet
Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
end with

HTH

RP
(remove nothere from the email address if mailing direct)

«Peter T» <peter_t@discussions> wrote in message
news:%23FMgURDqFHA.820@TK2MSFTNGP09.phx.gbl…


> I’ve only glanced at your code but try
>
> with activesheet
> Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> end with
>
> this qualifies the dot that precedes your Cells to the sheet.

Alternatively
> just remove those dots if, and only if you are working with the active
> sheet.
>
> It would be a good idea to qualify your variables
> Dim strOrig as string, strOutcomes As String
> Dim rCell as range, rTOCtyLst As Range
> Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
>
> Regards,
> Peter T
>
> «davegb» <davegb@safebrowse.com> wrote in message
> news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> > Still working on this code:
> >
> > Sub CtyMatch()
> > Dim strOrig, strOutcomes As String
> > Dim rCell, rTOCtyLst As Range
> > Dim iOrigCityNo, iEndRow As Integer
> >
> > strOrig = ActiveSheet.Range(«A2»)
> > iOrigCityNo = Left(strOrig, 2)
> > iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> >
> > But am getting an «invalid or unqualified reference» error on the last
> > line, .Cells being highlighted. I copied this line from another thread
> > here so I’m stumped as to why it doesn’t recognize «.Cells» here but
> > does elsewhere in a nearly identical situation.
> > Any suggestions?
> > Thanks.
> >

>
>


08-23-2005, 08:05 PM


#5

Re: Another invalid or unqualified reference

Peter T wrote:
> I’ve only glanced at your code but try
>
> with activesheet
> Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> end with
>
> this qualifies the dot that precedes your Cells to the sheet. Alternatively
> just remove those dots if, and only if you are working with the active
> sheet.
>
> It would be a good idea to qualify your variables
> Dim strOrig as string, strOutcomes As String
> Dim rCell as range, rTOCtyLst As Range
> Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
>

Why not interger with rows if there’s never going to be more than 80 of
them?


08-23-2005, 08:05 PM


#6

Re: Another invalid or unqualified reference

Hi Bob,

Is it strictly necessary to qualify the Range with a dot?

Sub test()
Dim ws As Worksheet
Dim r1 As Range, r2 As Range
Set ws = Worksheets(«Sheet3»)

Worksheets(«Sheet1»).Activate

With ws
Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
End With

MsgBox r1.Parent.Name & vbCr & _
r2.Parent.Name

End Sub

Regards,
Peter T

«Bob Phillips» <bob.phillips@notheretiscali.co.uk> wrote in message
news:#p9aofDqFHA.2628@TK2MSFTNGP10.phx.gbl…


> The range should be dot qualified also
>
> with activesheet
> Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
> end with
>
> —
>
> HTH
>
> RP
> (remove nothere from the email address if mailing direct)
>
>
> «Peter T» <peter_t@discussions> wrote in message
> news:%23FMgURDqFHA.820@TK2MSFTNGP09.phx.gbl…
> > I’ve only glanced at your code but try
> >
> > with activesheet
> > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > end with
> >
> > this qualifies the dot that precedes your Cells to the sheet.

> Alternatively
> > just remove those dots if, and only if you are working with the active
> > sheet.
> >
> > It would be a good idea to qualify your variables
> > Dim strOrig as string, strOutcomes As String
> > Dim rCell as range, rTOCtyLst As Range
> > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> >
> > Regards,
> > Peter T
> >
> > «davegb» <davegb@safebrowse.com> wrote in message
> > news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> > > Still working on this code:
> > >
> > > Sub CtyMatch()
> > > Dim strOrig, strOutcomes As String
> > > Dim rCell, rTOCtyLst As Range
> > > Dim iOrigCityNo, iEndRow As Integer
> > >
> > > strOrig = ActiveSheet.Range(«A2»)
> > > iOrigCityNo = Left(strOrig, 2)
> > > iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > >
> > > But am getting an «invalid or unqualified reference» error on the last
> > > line, .Cells being highlighted. I copied this line from another thread
> > > here so I’m stumped as to why it doesn’t recognize «.Cells» here but
> > > does elsewhere in a nearly identical situation.
> > > Any suggestions?
> > > Thanks.
> > >

> >
> >

>
>


08-23-2005, 08:05 PM


#7

Re: Another invalid or unqualified reference

«davegb» <davegb@safebrowse.com> wrote in message
news:1124839642.454267.232320@z14g2000cwz.googlegroups.com…
>
> Peter T wrote:
> > I’ve only glanced at your code but try
> >
> > with activesheet
> > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > end with
> >
> > this qualifies the dot that precedes your Cells to the sheet.

Alternatively
> > just remove those dots if, and only if you are working with the active
> > sheet.
> >
> > It would be a good idea to qualify your variables
> > Dim strOrig as string, strOutcomes As String
> > Dim rCell as range, rTOCtyLst As Range
> > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> >

> Why not interger with rows if there’s never going to be more than 80 of
> them?

You didn’t say there would never be more than 80 rows, you said you were
having problems and that might have been another reason, apart from the
obvious. If you are 100% certain you will never refer to a row over 32k then
yes you could use Integer. But one day it might bite you.

In 32 bit vba there’s no advantage to using an Integer vs a Long.

Regards,
Peter T


08-23-2005, 09:05 PM


#8

Re: Another invalid or unqualified reference

Not always. But it doesn’t hurt when you do and will save debugging time when
you actually need it.

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rTOCtyLst As Range
With Worksheets(«sheet2»)
Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, 2))
End With
End Sub

Try putting this code behind Sheet1’s module.

The version without the dot (range()) will fail. When you add the dot
(.range(), it’ll work ok.

Unqualified ranges in that worksheet module will refer to the worksheet that
owns the code.

In xl2003’s help, it says:

When used without an object qualifier, this property is a shortcut for
ActiveSheet.Range (it returns a range from the active sheet; if the active sheet
isn�t a worksheet, the property fails).

But that doesn’t look true to me.

I think that excel treats this unqualified Range as application.range.

All these examples have sheet1 the activesheet:

Because this in a general module works fine:

With Worksheets(«sheet2»)
Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, 2))
End With

But this fails:

With Worksheets(«sheet2»)
Set rTOCtyLst = activesheet.Range(.Cells(1, 1), .Cells(1, 2))
End With

But this works ok:

With Worksheets(«sheet2»)
Set rTOCtyLst = application.Range(.Cells(1, 1), .Cells(1, 2))
End With

=======
And for all the extra time it takes to type that dot, I think it’s money well
spent!

Peter T wrote:


>
> Hi Bob,
>
> Is it strictly necessary to qualify the Range with a dot?
>
> Sub test()
> Dim ws As Worksheet
> Dim r1 As Range, r2 As Range
> Set ws = Worksheets(«Sheet3»)
>
> Worksheets(«Sheet1»).Activate
>
> With ws
> Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
> Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> End With
>
> MsgBox r1.Parent.Name & vbCr & _
> r2.Parent.Name
>
> End Sub
>
> Regards,
> Peter T
>
> «Bob Phillips» <bob.phillips@notheretiscali.co.uk> wrote in message
> news:#p9aofDqFHA.2628@TK2MSFTNGP10.phx.gbl…
> > The range should be dot qualified also
> >
> > with activesheet
> > Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
> > end with
> >
> > —
> >
> > HTH
> >
> > RP
> > (remove nothere from the email address if mailing direct)
> >
> >
> > «Peter T» <peter_t@discussions> wrote in message
> > news:%23FMgURDqFHA.820@TK2MSFTNGP09.phx.gbl…
> > > I’ve only glanced at your code but try
> > >
> > > with activesheet
> > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > end with
> > >
> > > this qualifies the dot that precedes your Cells to the sheet.

> > Alternatively
> > > just remove those dots if, and only if you are working with the active
> > > sheet.
> > >
> > > It would be a good idea to qualify your variables
> > > Dim strOrig as string, strOutcomes As String
> > > Dim rCell as range, rTOCtyLst As Range
> > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > >
> > > Regards,
> > > Peter T
> > >
> > > «davegb» <davegb@safebrowse.com> wrote in message
> > > news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> > > > Still working on this code:
> > > >
> > > > Sub CtyMatch()
> > > > Dim strOrig, strOutcomes As String
> > > > Dim rCell, rTOCtyLst As Range
> > > > Dim iOrigCityNo, iEndRow As Integer
> > > >
> > > > strOrig = ActiveSheet.Range(«A2»)
> > > > iOrigCityNo = Left(strOrig, 2)
> > > > iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > >
> > > > But am getting an «invalid or unqualified reference» error on the last
> > > > line, .Cells being highlighted. I copied this line from another thread
> > > > here so I’m stumped as to why it doesn’t recognize «.Cells» here but
> > > > does elsewhere in a nearly identical situation.
> > > > Any suggestions?
> > > > Thanks.
> > > >
> > >
> > >

> >
> >

Dave Peterson


08-23-2005, 09:05 PM


#9

Re: Another invalid or unqualified reference

Hi Dave,

Good point about use in worksheet module.


> And for all the extra time it takes to type that dot, I think it’s money

well
> spent!

My resources can just about stretch to an extra dot, as you say might repay
if ever copying code from a normal module to a worksheet module !!

Regards,
Peter T

«Dave Peterson» <petersod@verizonXSPAM.net> wrote in message
news:430BBC1B.B43CE2D2@verizonXSPAM.net…


> Not always. But it doesn’t hurt when you do and will save debugging time

when
> you actually need it.
>
> Option Explicit
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> Dim rTOCtyLst As Range
> With Worksheets(«sheet2»)
> Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, 2))
> End With
> End Sub
>
> Try putting this code behind Sheet1’s module.
>
> The version without the dot (range()) will fail. When you add the dot
> (.range(), it’ll work ok.
>
> Unqualified ranges in that worksheet module will refer to the worksheet

that
> owns the code.
>
> In xl2003’s help, it says:
>
> When used without an object qualifier, this property is a shortcut for
> ActiveSheet.Range (it returns a range from the active sheet; if the active

sheet
> isn’t a worksheet, the property fails).
>
> But that doesn’t look true to me.
>
> I think that excel treats this unqualified Range as application.range.
>
> All these examples have sheet1 the activesheet:
>
> Because this in a general module works fine:
>
> With Worksheets(«sheet2»)
> Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, 2))
> End With
>
> But this fails:
>
> With Worksheets(«sheet2»)
> Set rTOCtyLst = activesheet.Range(.Cells(1, 1), .Cells(1, 2))
> End With
>
> But this works ok:
>
> With Worksheets(«sheet2»)
> Set rTOCtyLst = application.Range(.Cells(1, 1), .Cells(1, 2))
> End With
>
> =======
> And for all the extra time it takes to type that dot, I think it’s money

well
> spent!
>
>
> Peter T wrote:
> >
> > Hi Bob,
> >
> > Is it strictly necessary to qualify the Range with a dot?
> >
> > Sub test()
> > Dim ws As Worksheet
> > Dim r1 As Range, r2 As Range
> > Set ws = Worksheets(«Sheet3»)
> >
> > Worksheets(«Sheet1»).Activate
> >
> > With ws
> > Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
> > Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> > End With
> >
> > MsgBox r1.Parent.Name & vbCr & _
> > r2.Parent.Name
> >
> > End Sub
> >
> > Regards,
> > Peter T
> >
> > «Bob Phillips» <bob.phillips@notheretiscali.co.uk> wrote in message
> > news:#p9aofDqFHA.2628@TK2MSFTNGP10.phx.gbl…
> > > The range should be dot qualified also
> > >
> > > with activesheet
> > > Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > end with
> > >
> > > —
> > >
> > > HTH
> > >
> > > RP
> > > (remove nothere from the email address if mailing direct)
> > >
> > >
> > > «Peter T» <peter_t@discussions> wrote in message
> > > news:%23FMgURDqFHA.820@TK2MSFTNGP09.phx.gbl…
> > > > I’ve only glanced at your code but try
> > > >
> > > > with activesheet
> > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > > end with
> > > >
> > > > this qualifies the dot that precedes your Cells to the sheet.
> > > Alternatively
> > > > just remove those dots if, and only if you are working with the

active
> > > > sheet.
> > > >
> > > > It would be a good idea to qualify your variables
> > > > Dim strOrig as string, strOutcomes As String
> > > > Dim rCell as range, rTOCtyLst As Range
> > > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > > >
> > > > Regards,
> > > > Peter T
> > > >
> > > > «davegb» <davegb@safebrowse.com> wrote in message
> > > > news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> > > > > Still working on this code:
> > > > >
> > > > > Sub CtyMatch()
> > > > > Dim strOrig, strOutcomes As String
> > > > > Dim rCell, rTOCtyLst As Range
> > > > > Dim iOrigCityNo, iEndRow As Integer
> > > > >
> > > > > strOrig = ActiveSheet.Range(«A2»)
> > > > > iOrigCityNo = Left(strOrig, 2)
> > > > > iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> > > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > > >
> > > > > But am getting an «invalid or unqualified reference» error on the

last
> > > > > line, .Cells being highlighted. I copied this line from another

thread
> > > > > here so I’m stumped as to why it doesn’t recognize «.Cells» here

but
> > > > > does elsewhere in a nearly identical situation.
> > > > > Any suggestions?
> > > > > Thanks.
> > > > >
> > > >
> > > >
> > >
> > >

>
> —
>
> Dave Peterson


08-23-2005, 09:05 PM


#10

Re: Another invalid or unqualified reference

No, it’s not essential, until you get in a sheet module:

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim r1 As Range, r2 As Range
Set ws = Worksheets(«Sheet3»)

Worksheets(«Sheet1»).Activate

With ws
Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
End With

MsgBox r1.Parent.Name & vbCr & _
r2.Parent.Name

End Sub

So for continuity, it would make sense to get in the habit.


Regards,
Tom Ogilvy

«Peter T» <peter_t@discussions> wrote in message
news:%23GmTgvDqFHA.3752@TK2MSFTNGP10.phx.gbl…


> Hi Bob,
>
> Is it strictly necessary to qualify the Range with a dot?
>
> Sub test()
> Dim ws As Worksheet
> Dim r1 As Range, r2 As Range
> Set ws = Worksheets(«Sheet3»)
>
> Worksheets(«Sheet1»).Activate
>
> With ws
> Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
> Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> End With
>
> MsgBox r1.Parent.Name & vbCr & _
> r2.Parent.Name
>
> End Sub
>
> Regards,
> Peter T
>
> «Bob Phillips» <bob.phillips@notheretiscali.co.uk> wrote in message
> news:#p9aofDqFHA.2628@TK2MSFTNGP10.phx.gbl…
> > The range should be dot qualified also
> >
> > with activesheet
> > Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
> > end with
> >
> > —
> >
> > HTH
> >
> > RP
> > (remove nothere from the email address if mailing direct)
> >
> >
> > «Peter T» <peter_t@discussions> wrote in message
> > news:%23FMgURDqFHA.820@TK2MSFTNGP09.phx.gbl…
> > > I’ve only glanced at your code but try
> > >
> > > with activesheet
> > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > end with
> > >
> > > this qualifies the dot that precedes your Cells to the sheet.

> > Alternatively
> > > just remove those dots if, and only if you are working with the active
> > > sheet.
> > >
> > > It would be a good idea to qualify your variables
> > > Dim strOrig as string, strOutcomes As String
> > > Dim rCell as range, rTOCtyLst As Range
> > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > >
> > > Regards,
> > > Peter T
> > >
> > > «davegb» <davegb@safebrowse.com> wrote in message
> > > news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> > > > Still working on this code:
> > > >
> > > > Sub CtyMatch()
> > > > Dim strOrig, strOutcomes As String
> > > > Dim rCell, rTOCtyLst As Range
> > > > Dim iOrigCityNo, iEndRow As Integer
> > > >
> > > > strOrig = ActiveSheet.Range(«A2»)
> > > > iOrigCityNo = Left(strOrig, 2)
> > > > iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > >
> > > > But am getting an «invalid or unqualified reference» error on the

last
> > > > line, .Cells being highlighted. I copied this line from another

thread
> > > > here so I’m stumped as to why it doesn’t recognize «.Cells» here but
> > > > does elsewhere in a nearly identical situation.
> > > > Any suggestions?
> > > > Thanks.
> > > >
> > >
> > >

> >
> >

>
>


08-24-2005, 05:05 AM


#11

Re: Another invalid or unqualified reference

Hi Tom,

I guess Dave’s comments and my follow-up were not visible when you posted,
but thanks also.

Dave mentioned that if Range is not qualified, either with a dot to some
sheet or by default to the sheet if in a worksheet module, it defaults to
the Application.

So in a sheet module could use like this:

‘ in Sheet1 module
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim r1 As Range, r2 As Range
Set ws = Worksheets(«Sheet3»)

Worksheets(«Sheet1»).Activate

With ws
Set r1 = Application.Range(.Cells(1, 1), .Cells(2, 2))
Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
End With

MsgBox r1.Parent.Name & vbCr & _
r2.Parent.Name

End Sub

This makes sense. In VB6 both Range & Cells always need to be qualified,
even if working on the active sheet (& even if a ref to Excel has been set
in the vb6 project ref’s)

Set xlApp = the current Excel instance
Set ws1 = .ActiveSheet ‘ ref’d back to wb & app

Set rng = xlApp.Range(ws1.Cells(1, 1), ws1.Cells(2, 2))
or
Set rng = ws1.Range(ws1.Cells(1, 1), ws1.Cells(2, 2))

Regards,
Peter T

«Tom Ogilvy» <twogilvy@msn.com> wrote in message
news:uhSvwNEqFHA.272@TK2MSFTNGP15.phx.gbl…


> No, it’s not essential, until you get in a sheet module:
>
> Private Sub CommandButton1_Click()
> Dim ws As Worksheet
> Dim r1 As Range, r2 As Range
> Set ws = Worksheets(«Sheet3»)
>
> Worksheets(«Sheet1»).Activate
>
> With ws
> Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
> Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> End With
>
> MsgBox r1.Parent.Name & vbCr & _
> r2.Parent.Name
>
> End Sub
>
> So for continuity, it would make sense to get in the habit.
>
> —
> Regards,
> Tom Ogilvy
>
>
> «Peter T» <peter_t@discussions> wrote in message
> news:%23GmTgvDqFHA.3752@TK2MSFTNGP10.phx.gbl…
> > Hi Bob,
> >
> > Is it strictly necessary to qualify the Range with a dot?
> >
> > Sub test()
> > Dim ws As Worksheet
> > Dim r1 As Range, r2 As Range
> > Set ws = Worksheets(«Sheet3»)
> >
> > Worksheets(«Sheet1»).Activate
> >
> > With ws
> > Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
> > Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> > End With
> >
> > MsgBox r1.Parent.Name & vbCr & _
> > r2.Parent.Name
> >
> > End Sub
> >
> > Regards,
> > Peter T
> >
> > «Bob Phillips» <bob.phillips@notheretiscali.co.uk> wrote in message
> > news:#p9aofDqFHA.2628@TK2MSFTNGP10.phx.gbl…
> > > The range should be dot qualified also
> > >
> > > with activesheet
> > > Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > end with
> > >
> > > —
> > >
> > > HTH
> > >
> > > RP
> > > (remove nothere from the email address if mailing direct)
> > >
> > >
> > > «Peter T» <peter_t@discussions> wrote in message
> > > news:%23FMgURDqFHA.820@TK2MSFTNGP09.phx.gbl…
> > > > I’ve only glanced at your code but try
> > > >
> > > > with activesheet
> > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > > end with
> > > >
> > > > this qualifies the dot that precedes your Cells to the sheet.
> > > Alternatively
> > > > just remove those dots if, and only if you are working with the

active
> > > > sheet.
> > > >
> > > > It would be a good idea to qualify your variables
> > > > Dim strOrig as string, strOutcomes As String
> > > > Dim rCell as range, rTOCtyLst As Range
> > > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > > >
> > > > Regards,
> > > > Peter T
> > > >
> > > > «davegb» <davegb@safebrowse.com> wrote in message
> > > > news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> > > > > Still working on this code:
> > > > >
> > > > > Sub CtyMatch()
> > > > > Dim strOrig, strOutcomes As String
> > > > > Dim rCell, rTOCtyLst As Range
> > > > > Dim iOrigCityNo, iEndRow As Integer
> > > > >
> > > > > strOrig = ActiveSheet.Range(«A2»)
> > > > > iOrigCityNo = Left(strOrig, 2)
> > > > > iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> > > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > > >
> > > > > But am getting an «invalid or unqualified reference» error on the

> last
> > > > > line, .Cells being highlighted. I copied this line from another

> thread
> > > > > here so I’m stumped as to why it doesn’t recognize «.Cells» here

but
> > > > > does elsewhere in a nearly identical situation.
> > > > > Any suggestions?
> > > > > Thanks.
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>


08-24-2005, 09:05 AM


#12

Re: Another invalid or unqualified reference

Why introduce application in the middle of a With statement. While it may
work, it doesn’t make sense to me when the simple inclusion of the period
would suffice.

Obviously, if Daves contribution were visible, I wouldn’t have bothered.


Regards,
Tom Ogilvy

«Peter T» <peter_t@discussions> wrote in message
news:uKG6QeIqFHA.2240@tk2msftngp13.phx.gbl…


> Hi Tom,
>
> I guess Dave’s comments and my follow-up were not visible when you posted,
> but thanks also.
>
> Dave mentioned that if Range is not qualified, either with a dot to some
> sheet or by default to the sheet if in a worksheet module, it defaults to
> the Application.
>
> So in a sheet module could use like this:
>
> ‘ in Sheet1 module
> Private Sub CommandButton1_Click()
> Dim ws As Worksheet
> Dim r1 As Range, r2 As Range
> Set ws = Worksheets(«Sheet3»)
>
> Worksheets(«Sheet1»).Activate
>
> With ws
> Set r1 = Application.Range(.Cells(1, 1), .Cells(2, 2))
> Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> End With
>
> MsgBox r1.Parent.Name & vbCr & _
> r2.Parent.Name
>
> End Sub
>
> This makes sense. In VB6 both Range & Cells always need to be qualified,
> even if working on the active sheet (& even if a ref to Excel has been set
> in the vb6 project ref’s)
>
> Set xlApp = the current Excel instance
> Set ws1 = .ActiveSheet ‘ ref’d back to wb & app
>
> Set rng = xlApp.Range(ws1.Cells(1, 1), ws1.Cells(2, 2))
> or
> Set rng = ws1.Range(ws1.Cells(1, 1), ws1.Cells(2, 2))
>
> Regards,
> Peter T
>
>
> «Tom Ogilvy» <twogilvy@msn.com> wrote in message
> news:uhSvwNEqFHA.272@TK2MSFTNGP15.phx.gbl…
> > No, it’s not essential, until you get in a sheet module:
> >
> > Private Sub CommandButton1_Click()
> > Dim ws As Worksheet
> > Dim r1 As Range, r2 As Range
> > Set ws = Worksheets(«Sheet3»)
> >
> > Worksheets(«Sheet1»).Activate
> >
> > With ws
> > Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
> > Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> > End With
> >
> > MsgBox r1.Parent.Name & vbCr & _
> > r2.Parent.Name
> >
> > End Sub
> >
> > So for continuity, it would make sense to get in the habit.
> >
> > —
> > Regards,
> > Tom Ogilvy
> >
> >
> > «Peter T» <peter_t@discussions> wrote in message
> > news:%23GmTgvDqFHA.3752@TK2MSFTNGP10.phx.gbl…
> > > Hi Bob,
> > >
> > > Is it strictly necessary to qualify the Range with a dot?
> > >
> > > Sub test()
> > > Dim ws As Worksheet
> > > Dim r1 As Range, r2 As Range
> > > Set ws = Worksheets(«Sheet3»)
> > >
> > > Worksheets(«Sheet1»).Activate
> > >
> > > With ws
> > > Set r1 = Range(.Cells(1, 1), .Cells(2, 2))
> > > Set r2 = .Range(.Cells(1, 1), .Cells(2, 2))
> > > End With
> > >
> > > MsgBox r1.Parent.Name & vbCr & _
> > > r2.Parent.Name
> > >
> > > End Sub
> > >
> > > Regards,
> > > Peter T
> > >
> > > «Bob Phillips» <bob.phillips@notheretiscali.co.uk> wrote in message
> > > news:#p9aofDqFHA.2628@TK2MSFTNGP10.phx.gbl…
> > > > The range should be dot qualified also
> > > >
> > > > with activesheet
> > > > Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > > end with
> > > >
> > > > —
> > > >
> > > > HTH
> > > >
> > > > RP
> > > > (remove nothere from the email address if mailing direct)
> > > >
> > > >
> > > > «Peter T» <peter_t@discussions> wrote in message
> > > > news:%23FMgURDqFHA.820@TK2MSFTNGP09.phx.gbl…
> > > > > I’ve only glanced at your code but try
> > > > >
> > > > > with activesheet
> > > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > > > end with
> > > > >
> > > > > this qualifies the dot that precedes your Cells to the sheet.
> > > > Alternatively
> > > > > just remove those dots if, and only if you are working with the

> active
> > > > > sheet.
> > > > >
> > > > > It would be a good idea to qualify your variables
> > > > > Dim strOrig as string, strOutcomes As String
> > > > > Dim rCell as range, rTOCtyLst As Range
> > > > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > > > >
> > > > > Regards,
> > > > > Peter T
> > > > >
> > > > > «davegb» <davegb@safebrowse.com> wrote in message
> > > > > news:1124836141.416754.102520@f14g2000cwb.googlegroups.com…
> > > > > > Still working on this code:
> > > > > >
> > > > > > Sub CtyMatch()
> > > > > > Dim strOrig, strOutcomes As String
> > > > > > Dim rCell, rTOCtyLst As Range
> > > > > > Dim iOrigCityNo, iEndRow As Integer
> > > > > >
> > > > > > strOrig = ActiveSheet.Range(«A2»)
> > > > > > iOrigCityNo = Left(strOrig, 2)
> > > > > > iEndRow = ActiveSheet.Range.Cells(Rows.Count, «B»).End(xlUp).Row
> > > > > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > > > >
> > > > > > But am getting an «invalid or unqualified reference» error on

the
> > last
> > > > > > line, .Cells being highlighted. I copied this line from another

> > thread
> > > > > > here so I’m stumped as to why it doesn’t recognize «.Cells» here

> but
> > > > > > does elsewhere in a nearly identical situation.
> > > > > > Any suggestions?
> > > > > > Thanks.
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>


08-24-2005, 10:05 AM


#13

Re: Another invalid or unqualified reference

«Tom Ogilvy» <twogilvy@msn.com> wrote in message
> Why introduce application in the middle of a With statement. While it

may
> work, it doesn’t make sense to me when the simple inclusion of the period
> would suffice.

It was highly contrived but based on the previous example to further
re-enforce what you and Dave had noted, particularly with regards to what
Range defaults to can be changed. Whilst very familiar to you perhaps not
to everyone. I wouldn’t use that particular scenario.


> Obviously, if Daves contribution were visible, I wouldn’t have bothered.

I always appreciate anyone having taken the trouble to respond to any
question I have raised.

Regards,
Peter T


08-24-2005, 11:05 AM


#14

Re: Another invalid or unqualified reference

And I didn’t mean to give the impression that application.range is something I
generally use.

Peter T wrote:


>
> «Tom Ogilvy» <twogilvy@msn.com> wrote in message
> > Why introduce application in the middle of a With statement. While it

> may
> > work, it doesn’t make sense to me when the simple inclusion of the period
> > would suffice.

>
> It was highly contrived but based on the previous example to further
> re-enforce what you and Dave had noted, particularly with regards to what
> Range defaults to can be changed. Whilst very familiar to you perhaps not
> to everyone. I wouldn’t use that particular scenario.
>
> > Obviously, if Daves contribution were visible, I wouldn’t have bothered.

>
> I always appreciate anyone having taken the trouble to respond to any
> question I have raised.
>
> Regards,
> Peter T

Dave Peterson


08-24-2005, 11:05 AM


#15

Re: Another invalid or unqualified reference

«Dave Peterson» <petersod@verizonXSPAM.net> wrote in message
> And I didn’t mean to give the impression that application.range is
> something I generally use.

I didn’t take it that way but your comments were interesting nevertheless.

My first reply to Tom probably expanded on the topic beyond the point of
being useful !

Regards,
Peter T


08-24-2005, 11:05 AM


#16

Re: Another invalid or unqualified reference

Peter T wrote:
> «davegb» <davegb@safebrowse.com> wrote in message
> news:1124839642.454267.232320@z14g2000cwz.googlegroups.com…
> >
> > Peter T wrote:
> > > I’ve only glanced at your code but try
> > >
> > > with activesheet
> > > Set rTOCtyLst = Range(.Cells(1, 1), .Cells(1, iEndRow))
> > > end with
> > >
> > > this qualifies the dot that precedes your Cells to the sheet.

> Alternatively
> > > just remove those dots if, and only if you are working with the active
> > > sheet.
> > >
> > > It would be a good idea to qualify your variables
> > > Dim strOrig as string, strOutcomes As String
> > > Dim rCell as range, rTOCtyLst As Range
> > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > >

> > Why not interger with rows if there’s never going to be more than 80 of
> > them?

>
> You didn’t say there would never be more than 80 rows, you said you were
> having problems and that might have been another reason, apart from the
> obvious. If you are 100% certain you will never refer to a row over 32k then
> yes you could use Integer. But one day it might bite you.
>
> In 32 bit vba there’s no advantage to using an Integer vs a Long.
>
> Regards,
> Peter T

Thanks for the reply! I doubt that my state will ever have 32,000+
counties!
Seriously, does this mean that «integer» as a data type is obsolete? Is
there any use for it anymore?


08-24-2005, 12:05 PM


#17

Re: Another invalid or unqualified reference

<snip
> > > > It would be a good idea to qualify your variables
> > > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > > >
> > > Why not interger with rows if there’s never going to be more than 80

of
> > > them?

> >
> > You didn’t say there would never be more than 80 rows, you said you were
> > having problems and that might have been another reason, apart from the
> > obvious. If you are 100% certain you will never refer to a row over 32k

then
> > yes you could use Integer. But one day it might bite you.
> >
> > In 32 bit vba there’s no advantage to using an Integer vs a Long.
> >
> > Regards,
> > Peter T

>
> Thanks for the reply! I doubt that my state will ever have 32,000+
> counties!
> Seriously, does this mean that «integer» as a data type is obsolete? Is
> there any use for it anymore?
>

It’s a good point and fears have been expressed about the Integer data type
becoming obsolete. Though I doubt it will in the foreseeable future.

My understanding, gleaned from others, is in 32 bit vba Integer types are
internally converted to Long’s before any work is done. In other words an
additional process and overhead.

That being the case there seems no point ever to use an Integer, except
perhaps when a particular inbuilt function specifically expects an Integer.
But even these functions still seem to work fine if a Long is received.

Regards,
Peter T


08-24-2005, 12:05 PM


#18

Re: Another invalid or unqualified reference

Peter T wrote:
> <snip
> > > > > It would be a good idea to qualify your variables
> > > > > Dim iOrigCityNo as long, iEndRow As Long ‘ not Integer with rows
> > > > >
> > > > Why not interger with rows if there’s never going to be more than 80

> of
> > > > them?
> > >
> > > You didn’t say there would never be more than 80 rows, you said you were
> > > having problems and that might have been another reason, apart from the
> > > obvious. If you are 100% certain you will never refer to a row over 32k

> then
> > > yes you could use Integer. But one day it might bite you.
> > >
> > > In 32 bit vba there’s no advantage to using an Integer vs a Long.
> > >
> > > Regards,
> > > Peter T

> >
> > Thanks for the reply! I doubt that my state will ever have 32,000+
> > counties!
> > Seriously, does this mean that «integer» as a data type is obsolete? Is
> > there any use for it anymore?
> >

>
> It’s a good point and fears have been expressed about the Integer data type
> becoming obsolete. Though I doubt it will in the foreseeable future.
>
> My understanding, gleaned from others, is in 32 bit vba Integer types are
> internally converted to Long’s before any work is done. In other words an
> additional process and overhead.
>
> That being the case there seems no point ever to use an Integer, except
> perhaps when a particular inbuilt function specifically expects an Integer.
> But even these functions still seem to work fine if a Long is received.
>
> Regards,
> Peter T

Interesting series of replies to my original message. I have some
beginner questions.
I gather that «qualifying» means to show what object is the parent of
the current object? If I’m working with a range, is it’s parent the
activesheet or the application? Is that what it means?
What is the difference between «Range» and «.Range»? I don’t understand
why one works and the other doesn’t in different situations.

Dave P. wrote:
Try putting this code behind Sheet1’s module.

What does this mean? There are other references above to «Sheet
modules» and «regular modules». What are they? What’s the difference?
Why would a «.range» be required in one but a «range» be ok in another?

And why does

with activesheet
Set rTOCtyLst = .Range(.Cells(1, 1), .Cells(1, iEndRow))
end with

work better than

set rTOCtyLst = Activesheet.range(.Cells(1,1), .Cells(1, iEndRow))

?

Got lots of questions here, but am trying to figure out if there are
discernable patterns to VBA or if it’s just memorizing thousands of
rules that aren’t in the books!
I appreciate all the help.


08-24-2005, 02:05 PM


#19

Re: Another invalid or unqualified reference


> Interesting series of replies to my original message. I have some
> beginner questions.

One by one —


> I gather that «qualifying» means to show what object is the parent of
> the current object? If I’m working with a range, is it’s parent the
> activesheet or the application? Is that what it means?

Application.Workbooks(«myBook»).WorkSheets(«mySheet»).Range(«A1»)

This tree-like path always exists. Assuming code is in a normal module (see
later), if no reference is made back to parents, VBA works with the
activesheet in the active workbook. So, if you want to work with Range(«A1»)
on the activesheet, you don’t need to reference (qualify) to it’s parent
sheet or workbook. Similarly with

Set rng = Range(Cells(1,1),Cells(2,2))

in this line, assuming code is in a normal module, the important implied
reference is to «Cells» in the active sheet. Here, Cells do not and should
not have a preceding dot unless the line is embraced with «With
mySheet…End With». That was the problem in your OP. Why — because the
preceding dot is expected to link to a written reference to the Cells’
parent sheet.


> What is the difference between «Range» and «.Range»? I don’t understand
> why one works and the other doesn’t in different situations.

Again, if you include a preceding dot it links to a ref, that you need to
write, to whatever parent you want the range to be «in».
But —
With mySheet
set rng = Range(.Cells(1,1),.Cells(2,2))
End With

Range does not need the preceeding dot as the reference to the parent sheet
is linked with the dots that precede Cells. But I agree with all the
recommendations to include it.


> Dave P. wrote:
> Try putting this code behind Sheet1’s module.
>
> What does this mean? There are other references above to «Sheet
> modules» and «regular modules». What are they? What’s the difference?
> Why would a «.range» be required in one but a «range» be ok in another?

Right-click a sheet-tab, view-code and you wll go straight into a sheet
module. Typical code in a sheet module are sheet events and worksheet
ActiveX control’s code. But you can also write your own routines there (but
don’t until you understand what you are doing).

Unlike unqualified references in normal modules that default to the
activesheet & workbook, any unqualified code refers to the Worksheet of that
sheet module (whether or not it is active). Therefore if code is not
intended to refer to that sheet you need to explicitly refer whatever other
sheet.

‘in a sheet module
With mySheet
set rng = .Range(.Cells(1,1),.Cells(2,2))
End With

Unlike the similar code higher up, the dot preceeding Range is definately
required. Otherwise there is a conflict between Range (referring to the
sheet module) and Cells (referring) to mySheet.

Hope this takes you a bit further,
Peter T


08-24-2005, 06:05 PM


#20

Re: Another invalid or unqualified reference

Just this portion:

With mySheet
set rng = Range(.Cells(1,1),.Cells(2,2))
End With

Range does not need the preceeding dot as the reference to the parent sheet
is linked with the dots that precede Cells. But I agree with all the
recommendations to include it.

======
It still depends on where that code is located.

======
And for me, Integer as a variable type is gone.

<<snipped>>

Dave Peterson


08-24-2005, 06:05 PM


#21

Re: Another invalid or unqualified reference

I had attempted to qualify that portion with —
«Assuming code is in a normal module (see later)» etc

Then later I tried to make the clear distinction that the dot ref is
definitely required in a sheet module, if Range does not pertain to that
sheet.

But it did mean scrolling down to see it <g>

Regards,
Peter T

«Dave Peterson» <petersod@verizonXSPAM.net> wrote in message
news:430CE12A.DFEE947A@verizonXSPAM.net…


> Just this portion:
>
> With mySheet
> set rng = Range(.Cells(1,1),.Cells(2,2))
> End With
>
> Range does not need the preceeding dot as the reference to the parent

sheet
> is linked with the dots that precede Cells. But I agree with all the
> recommendations to include it.
>
> ======
> It still depends on where that code is located.
>
> ======
> And for me, Integer as a variable type is gone.
>
>
> <<snipped>>
> —
>
> Dave Peterson


08-24-2005, 10:05 PM


#22

Re: Another invalid or unqualified reference

Hey, how did that part get added after I read it!!!! <bg>.

Sorry about missing your final paragraph.

Peter T wrote:


>
> I had attempted to qualify that portion with —
> «Assuming code is in a normal module (see later)» etc
>
> Then later I tried to make the clear distinction that the dot ref is
> definitely required in a sheet module, if Range does not pertain to that
> sheet.
>
> But it did mean scrolling down to see it <g>
>
> Regards,
> Peter T
>
> «Dave Peterson» <petersod@verizonXSPAM.net> wrote in message
> news:430CE12A.DFEE947A@verizonXSPAM.net…
> > Just this portion:
> >
> > With mySheet
> > set rng = Range(.Cells(1,1),.Cells(2,2))
> > End With
> >
> > Range does not need the preceeding dot as the reference to the parent

> sheet
> > is linked with the dots that precede Cells. But I agree with all the
> > recommendations to include it.
> >
> > ======
> > It still depends on where that code is located.
> >
> > ======
> > And for me, Integer as a variable type is gone.
> >
> >
> > <<snipped>>
> > —
> >
> > Dave Peterson

Dave Peterson


08-25-2005, 11:05 AM


#23

Re: Another invalid or unqualified reference

Peter T wrote:
> > Interesting series of replies to my original message. I have some
> > beginner questions.

>
> One by one —
>
> > I gather that «qualifying» means to show what object is the parent of
> > the current object? If I’m working with a range, is it’s parent the
> > activesheet or the application? Is that what it means?

>
> Application.Workbooks(«myBook»).WorkSheets(«mySheet»).Range(«A1»)
>
> This tree-like path always exists. Assuming code is in a normal module (see
> later), if no reference is made back to parents, VBA works with the
> activesheet in the active workbook. So, if you want to work with Range(«A1»)
> on the activesheet, you don’t need to reference (qualify) to it’s parent
> sheet or workbook. Similarly with
>
> Set rng = Range(Cells(1,1),Cells(2,2))
>
> in this line, assuming code is in a normal module, the important implied
> reference is to «Cells» in the active sheet. Here, Cells do not and should
> not have a preceding dot unless the line is embraced with «With
> mySheet…End With». That was the problem in your OP. Why — because the
> preceding dot is expected to link to a written reference to the Cells’
> parent sheet.
>
> > What is the difference between «Range» and «.Range»? I don’t understand
> > why one works and the other doesn’t in different situations.

>
> Again, if you include a preceding dot it links to a ref, that you need to
> write, to whatever parent you want the range to be «in».
> But —
> With mySheet
> set rng = Range(.Cells(1,1),.Cells(2,2))
> End With
>
> Range does not need the preceeding dot as the reference to the parent sheet
> is linked with the dots that precede Cells. But I agree with all the
> recommendations to include it.
>
> > Dave P. wrote:
> > Try putting this code behind Sheet1’s module.
> >
> > What does this mean? There are other references above to «Sheet
> > modules» and «regular modules». What are they? What’s the difference?
> > Why would a «.range» be required in one but a «range» be ok in another?

>
> Right-click a sheet-tab, view-code and you wll go straight into a sheet
> module. Typical code in a sheet module are sheet events and worksheet
> ActiveX control’s code. But you can also write your own routines there (but
> don’t until you understand what you are doing).
>
> Unlike unqualified references in normal modules that default to the
> activesheet & workbook, any unqualified code refers to the Worksheet of that
> sheet module (whether or not it is active). Therefore if code is not
> intended to refer to that sheet you need to explicitly refer whatever other
> sheet.
>
> ‘in a sheet module
> With mySheet
> set rng = .Range(.Cells(1,1),.Cells(2,2))
> End With
>
> Unlike the similar code higher up, the dot preceeding Range is definately
> required. Otherwise there is a conflict between Range (referring to the
> sheet module) and Cells (referring) to mySheet.
>
> Hope this takes you a bit further,
> Peter T

Thanks to everyone for your replies. It’s definitely helping me to
figure out VBA. Thanks, Peter, for you detailed explanations of the
why’s and how’s.
One more question, for now. Are ActiveX controls for controlling other
apps?


08-25-2005, 01:05 PM


#24

Re: Another invalid or unqualified reference

«davegb» <davegb@safebrowse.com> wrote in message
<<snip>>

> Thanks to everyone for your replies. It’s definitely helping me to
> figure out VBA. Thanks, Peter, for you detailed explanations of the
> why’s and how’s.
> One more question, for now. Are ActiveX controls for controlling other
> apps?

Worksheet ActiveX controls are applied to a sheet from the Controls Toolbox
menu (Main menu, View, Toolbars). They respond to «Events» such as Click,
from these you can do or control what you want. The event code is in the
sheet module. Drag a button onto the sheet, while still in Design mode*
select View code* (* icons on the menu).

There are also controls from the «Forms» menu which don’t respond to events,
other than OnAction when assigned to a macro.

There is often confusion between the two types. If you can’t find what you
need to know about these by searching the groups or in help, it would be
better to start a new topic. In .Misc for general info and here for
specifics about programming.

There are of course 000’s of other ActiveX controls, it’d be nice to find
one that makes a decent cup of coffee.

Regards,
Peter T


08-31-2005, 02:05 PM


#25

Re: Another invalid or unqualified reference

Peter T wrote:
> «davegb» <davegb@safebrowse.com> wrote in message
> <<snip>>
>
> > Thanks to everyone for your replies. It’s definitely helping me to
> > figure out VBA. Thanks, Peter, for you detailed explanations of the
> > why’s and how’s.
> > One more question, for now. Are ActiveX controls for controlling other
> > apps?

>
> Worksheet ActiveX controls are applied to a sheet from the Controls Toolbox
> menu (Main menu, View, Toolbars). They respond to «Events» such as Click,
> from these you can do or control what you want. The event code is in the
> sheet module. Drag a button onto the sheet, while still in Design mode*
> select View code* (* icons on the menu).
>
> There are also controls from the «Forms» menu which don’t respond to events,
> other than OnAction when assigned to a macro.
>
> There is often confusion between the two types. If you can’t find what you
> need to know about these by searching the groups or in help, it would be
> better to start a new topic. In .Misc for general info and here for
> specifics about programming.
>
> There are of course 000’s of other ActiveX controls, it’d be nice to find
> one that makes a decent cup of coffee.
>
> Regards,
> Peter T

Thanks again. As for the cup of coffee, I’m allergic to the stuff. But
a good marguerita…


0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Invalid object name sql ошибка
  • Invalid normal index 3ds max ошибка