[VBNet] Nhờ hướng dẫn sửa lỗi "Method not registered - unsupported signature"

Liên hệ QC
Tôi tuân thủ nội quy khi đăng bài

Mr.hieudoanxd

Thành viên thường trực
Tham gia
25/10/19
Bài viết
302
Được thích
141
Chào cả nhà!
Ngày hôm qua, em bắt đầu ngôn ngữ mới tìm hiểu xem như thế nào. Em có build vài hàm cơ bản như code em đính kèm.

Code trong Project
Mã:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <RootNamespace>ClassLibrary3</RootNamespace>
    <TargetFramework>net6.0-windows</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Excel-DNA" Version="1.8.0" />
    <PackageReference Include="ExcelDna.AddIn" Version="1.8.0" />
    <PackageReference Include="ExcelDna.Integration" Version="1.8.0" />
    <PackageReference Include="Microsoft.Office.Interop.Excel" Version="15.0.4795.1001" />
  </ItemGroup>

</Project>

Code trong Class
Mã:
Imports System.Data
Imports System.Net.Mime.MediaTypeNames
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Imports ExcelDna.Integration

Public Class myAddin
    Implements ExcelDna.Integration.IExcelAddIn
    Public Sub AutoOpen() Implements ExcelDna.Integration.IExcelAddIn.AutoOpen
        MsgBox("Xin chào!")
    End Sub
    Public Sub AutoClose() Implements ExcelDna.Integration.IExcelAddIn.AutoClose

    End Sub
End Class
Public Module SayHello
    <ExcelFunction(Description:="Hàm đầu tiên tạo được trong VBNet")>
    Public Function SayHello(ByVal name As String) As String
        Return "Hello " & name
    End Function
End Module
Public Module TínhTổng
    <ExcelFunction(Description:="Tính tổng các giá trị trong phạm vi và có tùy chọn giải thích")>
    Public Function TínhTổng(area_sum As Object, Optional ByVal is_explain As Boolean = False, Optional ByVal num_digits As Integer = 3) As Object
        Dim mSum As Double = 0, sSum As String = ""

        On Error Resume Next

        For Each r As Object In CType(area_sum, IEnumerable)
            If IsNumeric(r) AndAlso r <> 0 Then
                mSum += CDbl(r)
                sSum &= "+" & Math.Round(CDbl(r), num_digits)
            End If
        Next
        sSum = "=" & sSum.TrimStart("+"c)
        sSum = sSum.Replace("+-", "-").Replace("=+", "=")

        Return IIf(is_explain, sSum, mSum)
    End Function
End Module
Public Module DemMau
    <ExcelFunction(Description:="Đếm số ô theo màu nền hoặc màu chữ")>
    Public Function DemMau(area_count As Excel.Range, val_color As Excel.Range, Optional ByVal is_font_color As Boolean = False) As Integer
        Dim mColor As Long = 0, count_Font As Long = 0, count_Fill As Long = 0

        On Error Resume Next

        mColor = If(is_font_color, val_color.Font.Color, val_color.Interior.Color)

        For Each r As Excel.Range In area_count
            If r.Interior.Color = mColor Then count_Fill += 1
            If r.Font.Color = mColor Then count_Font += 1
        Next

        Return If(is_font_color, count_Font, count_Fill)
    End Function
End Module

Tuy nhiên khi build thì bị lỗi "Method not registered - unsupported signature" tại hàm "DemMau" không biết lỗi do đâu. Em có vài câu hỏi muốn nhờ các thành viên diễn đàn giúp đỡ
1. Lỗi trên do nguyên nhân gì? cách giải quyết ra sao?
2. Debug nó khi mình đã build ra và chạy trong excel như thế nào?
- Ngày đầu tiên đi học VBNet -
 
Em đã biết nguyên nhân lỗi "Method not registered - unsupported signature" ở việc khai báo ở hàm là
Mã:
area_count As Excel.Range, val_color As Excel.Range
Ở hàm không hỗ trợ Excel.range mà phải khai báo thành as Object. Sửa thành code ở dưới đây thì build không còn lỗi nữa. Tuy nhiên lại chạy không đúng kết quả. Em thì lại không biết debug. Nhờ các bác hướng dẫn cách Debug khi đã chạy excel với ạ
Mã:
Public Module DemMau
    <ExcelFunction(Description:="Đếm số ô theo màu nền hoặc màu chữ")>
    Public Function DemMau(area_count As Object, val_color As Object, Optional ByVal is_font_color As Boolean = False) As Integer
        Dim mColor As Long = 0, count_Font As Long = 0, count_Fill As Long = 0

        On Error Resume Next
        Dim mRng As Excel.Range = CType(val_color, Excel.Range)

        mColor = If(is_font_color, mRng.Font.Color, mRng.Interior.Color)

        For Each r As Object In CType(area_count, IEnumerable)
            If r.Interior.Color = mColor Then count_Fill += 1
            If r.Font.Color = mColor Then count_Font += 1
        Next

        Return If(is_font_color, count_Font, count_Fill)
    End Function
End Module
 
Web KT

Bài viết mới nhất

Back
Top Bottom