[英]Getting different results when seemingly trying to get the same range
I'm having a problem which could also be a bug, either way, I don't know how to resolve it and would appreciate any help :) 我遇到一个问题,也可能是一个错误,无论哪种方式,我都不知道如何解决,并且希望得到任何帮助:)
i2 = 2
plz1 = Worksheets("Sheet2").Range("B" & i2)
plz2 = Worksheets("Sheet2").Range("C" & i2)
B2 is 89999 and C2 is 90000. So when running this code plz1 is 89999 and plz 2 is "90000" (notice the quotation marks). B2是89999,C2是90000。因此,运行此代码时,plz1是89999,plz 2是“ 90000”(请注意引号)。
So: 所以:
plz1 = 89999
plz2 = "90000"
This is quite an issue since I want to create a range: 这是一个很大的问题,因为我想创建一个范围:
Range(plz1 & ":" & plz2)
I don't know if I am just doing a really obvious stupid mistake or if it's a bug... 我不知道我是在做一个非常明显的愚蠢错误还是一个错误...
I would really appreciate any advice I can get! 我真的很感激我能得到的任何建议! :)) :))
Thanks, NiceRice 谢谢,NiceRice
Maybe you could try this instead? 也许您可以尝试一下呢?
Sub Combined_range()
i2 = 2
Sheets("Sheet2").Range ("B" & i2 & ":C" & i2)
End Sub
Not sure if you mean to construct a range of numbers or a range of rows. 不确定要构造数字范围还是行范围。
Here is how you can deal with either a text 90000 or "90000" in a cell. 这是在单元格中处理文本90000或“ 90000”的方法。 Also, then have a testNumber and see if it is between plz1 and plz2. 另外,还有一个testNumber,看看它是否在plz1和plz2之间。
Option Explicit
Sub test()
Dim i2 As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2") 'Change as appropriate
Dim plz1 As Long
Dim plz2 As Long
i2 = 2
'Get rid of any "" if present in string
If InStr(1, ws.Range("C" & i2), Chr(34)) > 0 Then
ws.Range("C" & i2) = Application.WorksheetFunction.Substitute(ws.Range("C" & i2), Chr(34), "")
End If
On Error GoTo NotConverted
plz1 = CLng(ws.Range("B" & i2))
plz2 = CLng(ws.Range("C" & i2))
Dim myRange As Range
Set myRange = ws.Range(plz1 & ":" & plz2)
MsgBox myRange.Address
Dim testNumber As Long
testNumber = 1238
If testNumber >= plz1 And testNumber <= plz2 Then
MsgBox "testNumber is between " & plz1 & " and " & plz2
End If
Exit Sub
NotConverted:
MsgBox "The contents of either cell B" & i2 & " or C" & i2 & " could not be converted to a number."
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.