Question : i get the below error for the code here in the snippet. Excel macro that gets the drive letters of each machine.

Hi,

i get the below error for the code here in the snippet.

It selects this line
LastRow = ActiveSheet.UsedRange.Rows.Count


---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Function call on left-hand side of assignment must return Variant or Object
---------------------------
OK   Help  
---------------------------

Any ideas why this error may occur

Regards
Sharath
Code Snippet:
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:
39:
Sub Get_Drive_Letter()
 Application.DisplayAlerts = False
strto = "BM"
strfrom = "Q"
 
Application.ScreenUpdating = False
Dim objWMIService As Object
Dim myrange As Range
LastRow = ActiveSheet.UsedRange.Rows.Count
For Each cell In Range(strfrom & "3:" & strfrom & LastRow).Cells
    Set objWMIService = Nothing
    Results = ""
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & cell.Value & "\root\cimv2")
    If Err.Number <> 0 Then
        Range(strto & cell.Row).Value = Err.Number & ":  " & Err.Description
        Err.Clear
        On Error GoTo 0
    Else
            Set colSettings = objWMIService.ExecQuery _
            ("Select * from Win32_logicaldisk")
            For Each objlogicaldisk In colSettings
                If objlogicaldisk.DriveType = 3 Then
                    If Results = "" Then
                        Results = Left(objlogicaldisk.Name, 1)
                    Else
                        Results = Results & ", " & Left(objlogicaldisk.Name, 1)
                    End If
                End If
            Next
        objlogicaldisk = ""
        Range(strto & cell.Row).Value = Results
    End If
   If cell.Row Mod 50 = 0 Then ActiveWorkbook.Save
Next cell
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Open in New Window Select All

Answer : i get the below error for the code here in the snippet. Excel macro that gets the drive letters of each machine.

You don't need it - it's the fact that it exists somewhere that is causing the problem, because you are trying to assign a value to a function which is designed to return a value.
Just add this line to your code above after line 1:
Dim LastRow as Long

and you should be OK.
Random Solutions  
 
programming4us programming4us