我一直在寻找一个小时左右的时间,我似乎无法在这个问题上找到任何东西。我可能写错了所以我没有得到我想要的结果。我遇到了.offset(,99)的问题。似乎每当我在其中放置一个相同的数字,如99,88,11或其他任何组合时,都会引发运行时错误13 type mismatch 。
但是当我将其更改为98或任何其他非类似组合时,它可以正常工作。如果它具有多次相同的数字,是否有某些方法需要对其进行编程?
Sub Scope()
Dim ws As Excel.Worksheet
Set ws = ThisWorkbook.Sheets("Future Ongoing Vetting") ' change to name of your sheet
Dim x As Long
x = 2
Do Until ws.Cells(x, 7) = ""
With ws.Cells(x, 5)
.Value = "• Customer name: " & .Offset(, 29) & _
Chr(10) & "• Customer Bus Org: " & .Offset(, 30) & _
Chr(10) & "• Internal Circuit ID: " & .Offset(, 2) & _
Chr(10) & "• Customer prem address: " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 16) & ", " & .Offset(, 17) & _
Chr(10) & "• Customer demarc: " & .Offset(, 18) & " " & .Offset(, 20) & ", " & .Offset(, 19) & " " & .Offset(, 21) & _
Chr(10) & "• MRR: " & .Offset(, 68) & _
Chr(10) & "• Current Off Net MRC: $" & .Offset(, 10) & _
Chr(10) & "• Margin Percent: " & .Offset(, 89) & _
Chr(10) & "• Bandwidth: " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb )" & _
Chr(10) & "• Customer term end date: ""TEXT(.Offset(, 32),""mmm-dd-yyyy"")" & _
Chr(10) & "• New Vendor: " & .Offset(, 106) & _
Chr(10) & "• New MRC: $" & .Offset(, 102) & _
Chr(10) & "• New NRC: $" & .Offset(, 103) & _
Chr(10) & "• New Install Interval: " & .Offset(, 105) & _
Chr(10) & "• New Term: " & .Offset(, 104) & _
Chr(10) & Chr(10) & "Planner Notes: This project is replacing the existing " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb ) based solution from ( " & .Offset(, 5) & " ) with a new " & .Offset(, 99) '& " ( " & .Offset(, 100) & "Mb ) based solution from ( " & .Offset(, 106) & " )."
' Chr(10) & "RFA # " & .Offset(, 107) & " install notes: " & _
' Chr(10) & "Please install ( " & .Offset(, 31) & " ) Ethernet " & .Offset(, 99) & " ( " & .Offset(, 100) & "Mb ) circuit with ( " & .Offset(, 106) & " ) from ( " & .Offset(, 101) & " ) to ( [Customer Prem] " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 17) & " )."
' Chr(10) & "This new circuit will be used to replace existing customer circuit ECCKT: ""&RC[1]&"", ""&RC[109]&"", ""&RC[110]&"", ICCKT: "" & RC[2] & ""." & _
' Chr(10) & "The customer prem address is ( ""&RC[12]&"" ""&RC[13]&"" ""&RC[14]&"", ""&RC[15]&"", ""&RC[16]&"", ""&RC[17]&"" ) and customer demarc is ( ""&RC[18]&"" ""&RC[20]&"", ""&RC[19]&"" ""&RC[21]&"" )."
End With
x = x + 1
Loop
End Sub
答案 0 :(得分:1)
您的代码编写方式使调试非常困难。我建议用以下方式替换那个极长的陈述:
Dim s As String 'at the top of the module, and then in the loop ...
s = "• Customer name: " & .Offset(, 29)
s = s & Chr(10) & "• Customer Bus Org: " & .Offset(, 30)
s = s & Chr(10) & "• Internal Circuit ID: " & .Offset(, 2)
s = s & Chr(10) & "• Customer prem address: " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 16) & ", " & .Offset(, 17)
s = s & Chr(10) & "• Customer demarc: " & .Offset(, 18) & " " & .Offset(, 20) & ", " & .Offset(, 19) & " " & .Offset(, 21)
s = s & Chr(10) & "• MRR: " & .Offset(, 68)
s = s & Chr(10) & "• Current Off Net MRC: $" & .Offset(, 10)
s = s & Chr(10) & "• Margin Percent: " & .Offset(, 89)
s = s & Chr(10) & "• Bandwidth: " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb )"
s = s & Chr(10) & "• Customer term end date: ""TEXT(.Offset(, 32),""mmm-dd-yyyy"")"
s = s & Chr(10) & "• New Vendor: " & .Offset(, 106)
s = s & Chr(10) & "• New MRC: $" & .Offset(, 102)
s = s & Chr(10) & "• New NRC: $" & .Offset(, 103)
s = s & Chr(10) & "• New Install Interval: " & .Offset(, 105)
s = s & Chr(10) & "• New Term: " & .Offset(, 104)
s = s & Chr(10) & Chr(10) & "Planner Notes: This project is replacing the existing " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb ) based solution from ( " & .Offset(, 5) & " ) with a new " & .Offset(, 99) '& " ( " & .Offset(, 100) & "Mb ) based solution from ( " & .Offset(, 106) & " )."
.Value = s
将不必要的长语句拆分成一系列较短的语句将使您能够更好地确定类型不匹配错误的来源。
答案 1 :(得分:1)
我更喜欢调用函数来构建字符串。您将能够隔离代码并在没有的立即窗口中调试它。
注意:我使用ws.Rows(5).Cells
来设置行中单元格的引用。通过这种方式;我可以通过它的实际列号而不是Cells(RowNumber, 5)
的偏移量来引用每列。它还允许我使用引用该行上的单元格的简写方法。例如r(1)
引用第1列r(10)
引用Rows(RowNumber)
的第10列。
Function getDescription(ws As Worksheet, RowNumber As Long) As String
Dim r As Range
Dim Data(14)
Set r = ws.Rows(5).Cells
Data(0) = "• Customer name: " & r(34)
Data(1) = "• Customer Bus Org: " & r(35)
Data(2) = "• Internal Circuit ID: " & r(7)
Data(3) = "• Customer prem address: " & r(17) & " " & r(18) & " " & r(19) & ", " & r(20) & ", " & r(21) & ", " & r(22)
Data(4) = "• Customer term end date: " & Chr(34) & Format(r(37), "mmm-dd-yyyy") & Chr(34)
Data(5) = "• Customer demarc: " & r(23) & " " & r(25) & ", " & r(24) & " " & r(26)
Data(6) = "• MRR: " & r(73)
Data(7) = "• Current Off Net MRC: $" & r(15)
Data(8) = "• Margin Percent: " & r(94)
Data(9) = "• Bandwidth: " & r(11) & " ( " & r(12) & "Mb )"
Data(10) = "• New Vendor: " & r(111)
Data(11) = "• New MRC: $" & r(107)
Data(12) = "• New NRC: $" & r(108)
Data(13) = "• New Install Interval: " & r(110)
Data(14) = "• New Term: " & r(109)
getDescription = Join(Data, Chr(10))
End Function